在调用百度地图api进行web开发时遇到了一个需求,我们需要在网页中内嵌一个div 然后在div中调用百度地图的js显示我们所需要的地区。根据需求坐标在地图上添加若干个标记点,并批量的为各个标记点设置监听函数,使之显示我们所需要的信息
首先,创建一个测试用的工程来测试我们的需要,可以使用pycharm或者django自带的命令创建工程
django-admin startproject addressdemo
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no"/>
<style type="text/css">
body, html {
width: 100%;
height: 100%;
margin: 0;
font-family: "微软雅黑";
}
#allmap {
height: 700px;
width: 100%;
}
#r-result {
width: 100%;
font-size: 14px;
}
</style>
<title>经纬度定位</title>
</head>
<body>
<div id="allmap"></div>
<div id="r-result">
<input type="button" />
</div>
</body>
</html>
<script type="text/javascript"
src="http://api.map.baidu.com/api?v=2.0&ak=你的秘钥"></script>
注:
python manage.py makemigrations
python manage.py migrate
from django.shortcuts import render
import json
from addresstest.models import address_info
def test(request):
address_point = address_info.objects.all()
address_longitude = []
address_latitude = []
address_data = []
for i in range(len(address_point)):
address_longitude.append(address_point[i].longitude)
address_latitude.append(address_point[i].latitude)
address_data.append(address_point[i].data)
return render(request, 'address.html',
{'address_longitude': json.dumps(address_longitude),
'address_latitude': json.dumps(address_latitude), 'address_data': json.dumps(address_data)})
from django.conf.urls import url
from django.contrib import admin
from addresstest import views
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^address/',views.test),
]
<script type="text/javascript">
var map = new BMap.Map("allmap");
map.centerAndZoom(new BMap.Point(118.3088230000, 32.3002390000), 18);
map.enableScrollWheelZoom(true);
var navigationControl = new BMap.NavigationControl({
// 靠左上角位置
anchor: BMAP_ANCHOR_TOP_LEFT,
// LARGE类型
type: BMAP_NAVIGATION_CONTROL_LARGE,
// 启用显示定位
enableGeolocation: true
});
map.addControl(navigationControl);
function get_location() {
var address_latitude ={{ address_latitude |safe}};
var address_longitude ={{ address_longitude|safe }};
var address_data ={{ address_data |safe}};
var point = []; //存放标注点经纬信息的数组
var marker = []; //存放标注点对象的数组
for (var i = 0; i < address_longitude.length; i++) {
point[i] = new BMap.Point(address_longitude[i], address_latitude[i]); //循环生成新的地图点
marker[i] = new BMap.Marker(point[i]); //按照地图点坐标生成标记
map.addOverlay(marker[i]);
}
for (i = 0; i < marker.length; i++) {
(function () {
var index = i;
marker[index].addEventListener('click', function () {
this.openInfoWindow(new BMap.InfoWindow(address_data[index]));
});
})();
}
}
</script>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no"/>
<style type="text/css">
body, html {
width: 100%;
height: 100%;
margin: 0;
font-family: "微软雅黑";
}
#allmap {
height: 700px;
width: 100%;
}
#r-result {
width: 100%;
font-size: 14px;
}
</style>
<script type="text/javascript"
src="http://api.map.baidu.com/api?v=2.0&ak=你的秘钥"></script>
<title>经纬度定位</title>
</head>
<body>
<div id="allmap"></div>
<div id="r-result">
<input type="button" value="获取我的位置" onclick="get_location()"/>
</div>
</body>
</html>
<script type="text/javascript">
// 百度地图API功能
var map = new BMap.Map("allmap");
map.centerAndZoom(new BMap.Point(118.3088230000, 32.3002390000), 18);
map.enableScrollWheelZoom(true);
var navigationControl = new BMap.NavigationControl({
// 靠左上角位置
anchor: BMAP_ANCHOR_TOP_LEFT,
// LARGE类型
type: BMAP_NAVIGATION_CONTROL_LARGE,
// 启用显示定位
enableGeolocation: true
});
map.addControl(navigationControl);
function get_location() {
var address_latitude ={{ address_latitude |safe}};
var address_longitude ={{ address_longitude|safe }};
var address_data ={{ address_data |safe}};
var point = []; //存放标注点经纬信息的数组
var marker = []; //存放标注点对象的数组
for (var i = 0; i < address_longitude.length; i++) {
point[i] = new BMap.Point(address_longitude[i], address_latitude[i]); //循环生成新的地图点
marker[i] = new BMap.Marker(point[i]); //按照地图点坐标生成标记
map.addOverlay(marker[i]);
}
for (i = 0; i < marker.length; i++) {
(function () {
var index = i;
marker[index].addEventListener('click', function () {
this.openInfoWindow(new BMap.InfoWindow(address_data[index]));
});
})();
}
}
</script>
python manage.py runserver
注: