前言:百度地图是网页中使用地图的常用第三方工具,这里结合React项目中学到的应用场景总结一些使用要点。
一、在网页中嵌入百度地图
- 控制台:查看应用、创建应用(获得百度地图密钥)
-
开发文档:选择JavaScriptAPI
 Referer白名单:*? ?(例如*.tedu.cn? 域名只能有这个字段,才能使用这个密钥,避免因盗用密钥使用自己网站的服务)
-
引入一个特殊的script,关键放置密钥的地方:
<script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=你的密钥"></script>
-
核心代码:
var map = new BMap.Map("container"); //创建地图实例
var point = new BMap.Point(116.300829,39.915836); //创建点坐标(天安门坐标为例)
map.centerAndZoom(point,17); //以指定点为中心并缩放 (17表示层级)
-
启用滚轮缩放方法:map.enableScrollWheelZoom(true);
-
添加导航控件:map.addControl(new BMap.NacigationControl());
-
添加缩放控件:map.addControl(new BMap.ScalControl());
-
添加概览图控件:map.addControl(new BMap.OverviewMapControl());
-
添加地图类型控件:map.addControl(new BMap.MapTypeControl());
-
常用覆盖物:小标记/小说明/小页签
var mk = new BMap.Marker(point);
map.addOverlay(mk);
marker.setAnimation(BMap)ANIMATION_BOUNCE); //弹跳动画
二、绘制订单地图

-
Easy Mock订单详情数据接口:/order/detail
{
"code": 0,"msg": "","list": {
"status": 2,"order_sn": "T1803244422704080JGJI","bike_sn": ‘802410001‘,"mode|1-2": 1,"start_location": "北京市昌平区回龙观东大街","end_location": "北京市海淀区奥林匹克公园","city_id": 1,"mobile": "13597482075","user_name": "@cname","distance": 10000,"bike_gps": "116.398806,40.008637","start_time": 1546580667000,"end_time": 1546608995000,"total_time": 224,"position_list": [{
"lon": 116.361221,"lat": 40.043776
},{
"lon": 116.363736,"lat": 40.038086
},{
"lon": 116.364599,"lat": 40.036484
},{
"lon": 116.373438,"lat": 40.03538
},{
"lon": 116.377966,"lat": 40.036263
},{
"lon": 116.379762,"lat": 40.03654
},{
"lon": 116.38084,"lat": 40.033225
},"lat": 40.029413
},{
"lon": 116.381343,"lat": 40.021291
},{
"lon": 116.381846,"lat": 40.015821
},{
"lon": 116.382637,"lat": 40.008084
},{
"lon": 116.398806,"lat": 40.008637
}
],"area": [{
"lon": "116.274737","lat": "40.139759","ts": null
},{
"lon": "116.316562","lat": "40.144943",{
"lon": "116.351631","lat": "40.129498",{
"lon": "116.390582","lat": "40.082481",{
"lon": "116.38742","lat": "40.01065",{
"lon": "116.414297","lat": "40.01181",{
"lon": "116.696242","lat": "39.964035",{
"lon": "116.494498","lat": "39.851306",{
"lon": "116.238086","lat": "39.848647",{
"lon": "116.189454","lat": "39.999418",{
"lon": "116.244646","lat": "39.990574",{
"lon": "116.281441","lat": "40.008703",{
"lon": "116.271092","lat": "40.142201","ts": null
}
],"area_list": null,"npl_list": [{
"id": 8265,"name": "北辰世纪中心-a座","type": 3,"status": 0,"map_point": "116.39338796444|40.008120315215;116.39494038009002|40.008177258745;116.39496911688|40.006268094213;116.39512457763|40.004256795877;116.39360214742|40.004222412241;116.39357190147|40.005075745782;116.39351397873|40.005836165232;116.39338796444|40.008120315215","map_point_array": [
"116.39338796444|40.008120315215","116.396053|40.008273","116.396448|40.006338","116.396915|40.004266","116.39192|40.004072","116.391525|40.004984","116.391381|40.005924","116.391166|40.007913"
],"map_status": 1,"creator_name": "赵程程","create_time": 1507863539000
}]
}
}
-
创建ak,加载百度地图sdk:【百度地图开发平台】 
-
地图初始化:public->index.html中引入百度地图第三方JS库
<title>React App</title>
<!--第三方库 百度地图-->
<script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=vALBxRpSKdoPxLyDcuMQPWFL8avoRTP0"></script>
pages->order->detail.js中:调用new window.BMap.Map(‘id‘)初始化Map
//初始化地图
renderMap = (list) => {
this.map = new window.BMap.Map(‘orderDetailMap‘);
// this.map.centerAndZoom(‘北京‘,11)
//添加地图控件
this.addMapControl();
//调用路线图绘制方法
this.drawBikeRoute(list.position_list);
//调用服务区绘制方法
this.drawServiceArea(list.area);
}
注意:单页面应用必须调用window.BMap全局变量才能识别BMap,否则会报错no defined;多页面应用中直接使用new BMao.Map(‘id‘)即可
-
添加地图控件
//添加地图控件
addMapControl = () => {
let map = this.map;
map.addControl(new window.BMap.ScaleControl({ anchor: window.BMAP_ANCHOR_TOP_RIGHT }));
map.addControl(new window.BMap.NavigationControl({ anchor: window.BMAP_ANCHOR_TOP_RIGHT }));
}
-
绘制用户行驶路线
//绘制用户行驶路线图
drawBikeRoute = (positionList) => {
let map = this.map;
let startPoint = ‘‘;
let endPoint = ‘‘;
if(positionList.length > 0){
//起点
let first = positionList[0];
startPoint = new window.BMap.Point(first.lon,first.lat);
let startIcon = new window.BMap.Icon(‘/assets/start_point.png‘,new window.BMap.Size(36,42),{
imageSize: new window.BMap.Size(36,anchor: new window.BMap.Size(36,42)
})
let startMarker = new window.BMap.Marker(startPoint,{icon: startIcon});
this.map.addOverlay(startMarker);
//终点
let last = positionList[positionList.length-1];
endPoint = new window.BMap.Point(last.lon,last.lat);
let endIcon = new window.BMap.Icon(‘/assets/end_point.png‘,42)
})
let endMarker = new window.BMap.Marker(endPoint,{icon: endIcon});
this.map.addOverlay(endMarker);
//连接路线图
let trackPoint = [];
for(let i=0; i<positionList.length; i++){
let point = positionList[i]
trackPoint.push(new window.BMap.Point(point.lon,point.lat))
}
let polyline = new window.BMap.Polyline(trackPoint,{
strokeColor: ‘#1869AD‘,strokeWeight: 3,strokeOpacity: 1
})
this.map.addOverlay(polyline);
//设置地图中心点
this.map.centerAndZoom(endPoint,11)
}
}
//绘制服务区
drawServiceArea = (positionList) => {
let trackPoint = [];
for(let i=0; i<positionList.length; i++){
let point = positionList[i]
trackPoint.push(new window.BMap.Point(point.lon,point.lat))
}
let polygon = new window.BMap.Polygon(trackPoint,{
strokeColor: ‘#CE0000‘,strokeWeight: 4,strokeOpacity: 1,fillColor: ‘#ff8605‘,fillOpacity: 0.4
})
this.map.addOverlay(polygon);
}
?
三、绘制车辆地图
注:转载请注明出处
(编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|