使用百度地圖api通過redis實現(xiàn)地標存儲及范圍坐標點查詢功能
1.首先拿到百度地圖開發(fā)密鑰,進入百度地圖開放平臺:百度地圖開放平臺
(1)選擇開發(fā)文檔>>web開發(fā)>>JavaScript API

(2)需要申請密鑰才可使用,點擊申請密鑰(申請密鑰的IP白名單可以用‘ * '代替,所有網(wǎng)站都可用,但安全性很差,所有人都可以抓到這個ak來使用,也可以根據(jù)自己的應(yīng)用場景來設(shè)置)

2,Controller代碼
public class MapController : Controller
{
//map視圖
public ActionResult Index()
{
return View();
}
/// <summary>
/// 存入坐標點
/// </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);
//存入坐標點
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)的其他坐標
/// </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;
//獲取周邊坐標點
(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=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)建標注
map.addOverlay(marker); // 將標注添加到地圖中
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實例
map.centerAndZoom(new BMapGL.Point(116.404, 39.915), 12); // 初始化地圖,設(shè)置中心點坐標和地圖級別
map.enableScrollWheelZoom(true); // 開啟鼠標滾輪縮放
</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實現(xiàn)地標存儲及范圍坐標點查詢功能的文章就介紹到這了,更多相關(guān)百度地圖api地標存儲范圍坐標點查詢內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用RediSearch實現(xiàn)在Redis中全文檢索
RediSearch?是?Redis?的一個插件,它為?Redis?數(shù)據(jù)庫添加了全文搜索和查詢功能,使開發(fā)人員能夠在?Redis?中高效地執(zhí)行全文檢索操作,下面我們就來看看是具體如何使用的吧2023-08-08

