1.首先拿到百度地圖開發(fā)密鑰,進(jìn)入百度地圖開放平臺:百度地圖開放平臺
(1)選擇開發(fā)文檔>>web開發(fā)>>JavaScript API
![](/d/20211018/f25c7ec0ff75693843a238b8622174df.gif)
(2)需要申請密鑰才可使用,點(diǎn)擊申請密鑰(申請密鑰的IP白名單可以用‘ * '代替,所有網(wǎng)站都可用,但安全性很差,所有人都可以抓到這個ak來使用,也可以根據(jù)自己的應(yīng)用場景來設(shè)置)
![](/d/20211018/a1509c113777ebadcec4bad54c100088.gif)
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代碼(注意填寫上面申請的密鑰)
@{
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=webglv=1.0ak=你的密鑰">/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="請輸入名稱" 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)和地圖級別
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)存儲及范圍坐標(biāo)點(diǎn)查詢功能的文章就介紹到這了,更多相關(guān)百度地圖api地標(biāo)存儲范圍坐標(biāo)點(diǎn)查詢內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:- 詳解vue.js下引入百度地圖jsApi的兩種方法
- python通過百度地圖API獲取某地址的經(jīng)緯度詳解
- 基于百度地圖api清除指定覆蓋物(Overlay)的方法
- Python爬蟲實(shí)例_利用百度地圖API批量獲取城市所有的POI點(diǎn)
- 百度地圖API之百度地圖退拽標(biāo)記點(diǎn)獲取經(jīng)緯度的實(shí)現(xiàn)代碼