使用百度地圖api通過redis實(shí)現(xiàn)地標(biāo)存儲(chǔ)及范圍坐標(biāo)點(diǎn)查詢功能
1.首先拿到百度地圖開發(fā)密鑰,進(jìn)入百度地圖開放平臺(tái):百度地圖開放平臺(tái)
(1)選擇開發(fā)文檔>>web開發(fā)>>JavaScript API
(2)需要申請(qǐng)密鑰才可使用,點(diǎn)擊申請(qǐng)密鑰(申請(qǐng)密鑰的IP白名單可以用‘ * '代替,所有網(wǎng)站都可用,但安全性很差,所有人都可以抓到這個(gè)ak來(lái)使用,也可以根據(jù)自己的應(yīng)用場(chǎng)景來(lái)設(shè)置)
2,Controller代碼
public class MapController : Controller { //map視圖 public ActionResult Index() { return View(); } /// <summary> /// 存入坐標(biāo)點(diǎn) /// </summary> /// <param name="Longitude">經(jīng)度</param> /// <param name="Latitude">緯度</param> /// <param name="Name">名稱</param> /// <returns></returns> public ActionResult Set(string Longitude, string Latitude, string Name) { //初始化redis CSRedis.CSRedisClient cSRedis = new CSRedis.CSRedisClient("192.168.56.131:6380,password=123456,defaultDatabase = 0"); RedisHelper.Initialization(cSRedis); decimal Lng = decimal.Parse(Longitude); decimal Lat = decimal.Parse(Latitude); //存入坐標(biāo)點(diǎn) var flag = RedisHelper.GeoAdd("zhongguo", Lng, Lat, Name); if (flag == true) { return Json(new { code = 1, message = "成功" }); } else { return Json(new { code = 0, message = "提交失敗" }); } } /// <summary> /// 獲取半徑范圍內(nèi)的其他坐標(biāo) /// </summary> /// <param name="lng">經(jīng)度</param> /// <param name="lat">緯度</param> /// <param name="ran">半徑</param> /// <returns></returns> public ActionResult Get(string lng, string lat, string ran) { //初始化redis CSRedis.CSRedisClient cSRedis = new CSRedis.CSRedisClient("192.168.56.131:6380,password=123456,defaultDatabase = 0"); RedisHelper.Initialization(cSRedis); decimal Lng = decimal.Parse(lng); decimal Lat = decimal.Parse(lat); decimal Ran = decimal.Parse(ran); var K = CSRedis.GeoUnit.km; //獲取周邊坐標(biāo)點(diǎn) (string member, decimal dist, decimal longitude, decimal latitude)[] list_ = RedisHelper.GeoRadiusWithDistAndCoord("zhongguo", Lng, Lat, Ran, K); return Json(list_, JsonRequestBehavior.AllowGet); } }
3,view代碼(注意填寫上面申請(qǐng)的密鑰)
@{ Layout = null; } <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="utf-8"> <title>地圖展示</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <meta name="viewport" content="initial-scale=1.0, user-scalable=no"> <meta http-equiv="X-UA-Compatible" content="IE=Edge"> <style> body, html, #container { overflow: hidden; width: 100%; height: 100%; margin: 0; font-family: "微軟雅黑"; } .info { z-index: 999; width: auto; min-width: 22rem; padding: .75rem 1.25rem; margin-left: 1.25rem; position: fixed; top: 1rem; background-color: #fff; border-radius: .25rem; font-size: 14px; color: #666; box-shadow: 0 2px 6px 0 rgba(27, 142, 236, 0.5); } </style> <link href="~/Content/button.css" rel="external nofollow" rel="stylesheet" /> <script src="http://api.map.baidu.com/api?type=webgl&v=1.0&ak=你的密鑰"></script> <script src="~/Scripts/jquery-3.4.1.min.js"></script> <script> function bing() { var lng = $("#lng").val(); var lat = $("#lat").val(); var ran = $("#ran").val(); $.ajax({ url: "@Url.Action("Get", "Map")", data: { lng: lng, lat: lat, ran:ran }, type: "post", dataType: "json", success: function (data) { console.log(data) if (data != null) { for (var i = 0; i < data.length; i++) { var new_point = new BMapGL.Point(data[i]['Item3'], data[i]['Item4']); console.log(data[i]['Item3']) console.log(data[i]['Item4']) var marker = new BMapGL.Marker(new_point); // 創(chuàng)建標(biāo)注 map.addOverlay(marker); // 將標(biāo)注添加到地圖中 map.panTo(new_point); } } } }); } </script> </head> <body> <div id="container"></div> <input type="text" id="ti" name="name" value="" placeholder="請(qǐng)輸入名稱" class="info" /> <div id="re"> 經(jīng)度: <input type="text" id="lng" name="name" value="" /> 緯度: <input type="text" id="lat" name="name" value="" /> 范圍: <input type="text" id="ran" name="name" value="" /> <button id="reu" name="name" onclick="bing()" value="查詢">查詢</button> </div> <input type="hidden" name="GetLng" value="" /> <div id="allmap"></div> </body> </html> <script> var map = new BMapGL.Map('container'); // 創(chuàng)建Map實(shí)例 map.centerAndZoom(new BMapGL.Point(116.404, 39.915), 12); // 初始化地圖,設(shè)置中心點(diǎn)坐標(biāo)和地圖級(jí)別 map.enableScrollWheelZoom(true); // 開啟鼠標(biāo)滾輪縮放 </script> <script> var map = new BMapGL.Map('container'); map.centerAndZoom(new BMapGL.Point(116.404, 39.928), 15); map.enableScrollWheelZoom(true); map.addEventListener('click', function (e) { alert( + e.latlng.lng + ',' + e.latlng.lat); if (confirm("是否添加此地址")) { var Longitude = e.latlng.lng; var Latitude = e.latlng.lat; var Name = $("#ti").val(); $.ajax({ url: "@Url.Action("Set", "Map")", data: { Longitude: Longitude, Latitude: Latitude, Name: Name }, type: "post", dataType: "json", success: function (data) { if (data.code) { alert(data.message); } else { alert(data.message); } } }); } }); </script> <script type="text/javascript"> // 百度地圖API功能 </script>
到此這篇關(guān)于使用百度地圖api通過redis實(shí)現(xiàn)地標(biāo)存儲(chǔ)及范圍坐標(biāo)點(diǎn)查詢功能的文章就介紹到這了,更多相關(guān)百度地圖api地標(biāo)存儲(chǔ)范圍坐標(biāo)點(diǎn)查詢內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Redis實(shí)戰(zhàn)之Jedis使用技巧詳解
Jedis?是老牌的?Redis?的?Java?客戶端,提供了比較全面的?Redis?命令的操作支持,也是目前使用最廣泛的客戶端。這篇文章主要為大家詳細(xì)介紹了Jedis的使用技巧,需要的可以參考一下2022-12-12Redis的Sentinel解決方案介紹與運(yùn)行機(jī)制
這篇文章主要介紹了Redis的Sentinel解決方案介紹與運(yùn)行機(jī)制, Sentinel 是一款面向分布式服務(wù)架構(gòu)的輕量級(jí)流量控制組件,主要以流量為切入點(diǎn),從流量控制、熔斷降級(jí)、系統(tǒng)自適應(yīng)保護(hù)等多個(gè)維度來(lái)保障服務(wù)的穩(wěn)定性,需要的朋友可以參考下2023-07-07使用RediSearch實(shí)現(xiàn)在Redis中全文檢索
RediSearch?是?Redis?的一個(gè)插件,它為?Redis?數(shù)據(jù)庫(kù)添加了全文搜索和查詢功能,使開發(fā)人員能夠在?Redis?中高效地執(zhí)行全文檢索操作,下面我們就來(lái)看看是具體如何使用的吧2023-08-08