1. 效果图
2.引入百度地图以及创建div地图容器的盒子
2. 创建地图实例,获取当前位置信息
var map = new BMap.Map("l-map"); var geolocation = new BMap.Geolocation(); geolocation.getCurrentPosition(function(r) { if (this.getStatus() == BMAP_STATUS_SUCCESS) { //位置经纬度 nowPointLng = r.point.lng; nowPointLat = r.point.lat; //var point = new BMap.Point(r.point.lng, r.point.lat); //point = new BMap.Point(112.947624, 28.20751); point = new BMap.Point(r.point.lng,r.point.lat); getYanbi(r.point.lng, r.point.lat); gc = new BMap.Geocoder(); gc.getLocation(point, function(rs) { var addComp = rs.addressComponents; infoWindow = new BMap.InfoWindow("<p style='font-size:14px;'>" + "我的位置:" + rs.address + "</p>"); $("#adress").text(rs.address); }); var myIcon = new BMap.Icon("../../static/adress.png", new BMap.Size(40, 45)); var mk = new BMap.Marker(point, { icon: myIcon }); mk.addEventListener("click", function() { this.openInfoWindow(infoWindow); }); map.addOverlay(mk); //标出所在地 map.panTo(r.point); //地图中心移动 //用所定位的经纬度查找所在地省市街道等信息 map.centerAndZoom(point, 14); var ResultArray = []; var local = new BMap.LocalSearch(map, { renderOptions: { map: map, // panel: "r-result", selectFirstResult: false, //是否选择第一个检索结果,检索完成后将默认选中第一个marker autoViewport: false //检索结束后是否自动调整地图视野。 }, onMarkersSet: function(pois) {}, onInfoHtmlSet: function(LocalResultPoi) {}, onSearchComplete: function(results) {}, }); // local.setPageCapacity(5); //设置每页容量,默认情况下,检索只会返回10家你检索的服务 // let newAd = ad.split(","); local.searchNearby("", point, 2000); } else { alert('failed' + this.getStatus()); } }, { enableHighAccuracy: true })3. 坐标图片用自定义
var myIcon = new BMap.Icon("../../static/adress.png", new BMap.Size(40, 45)); var mk = new BMap.Marker(point, { icon: myIcon }); mk.addEventListener("click", function() { this.openInfoWindow(infoWindow); }); map.addOverlay(mk); //标出所在地 map.panTo(point); //地图中心移动4. 步行规划
// 步行规划let walk = new BMap.WalkingRoute(map, {renderOptions: { map: map,autoViewport: true,panel: "results", }, }); // 当前位置let end = point1;let start = new BMap.Point(Lng, Lat);walk.search(start, end);