1. 创建地图引用
//创建地图引用 function createMap() { var script = document.createElement("script"); script.src = "https://api.map.baidu.com/api?v=2.0&ak=你的密匙&s=1&callback=storeAddLocation";//此为v2.0版本的引用方式 document.body.appendChild(script); pages.storeAdd.init = 1; }
2.函数
function storeAddLocation() { var defaultmap = new BMap.Map("allmap"); var point = new BMap.Point(pages.storeAdd.point.longitude, pages.storeAdd.point.latitude); defaultmap.centerAndZoom(point, 19); var marker = new BMap.Marker(point); defaultmap.addOverlay(marker); // map.centerAndZoom(point, 12); var geolocation = new BMap.Geolocation(); geolocation.getCurrentPosition(function (r) { if (this.getStatus() == BMAP_STATUS_SUCCESS) { var mk = new BMap.Marker(r.point); defaultmap.addOverlay(mk); defaultmap.panTo(r.point);
//经纬度 // alert('您的位置:' + r.point.lng + ',' + r.point.lat); var geoc = new BMap.Geocoder(); var pt = r.point; geoc.getLocation(pt, function (rs) { var addComp = rs.addressComponents;
//地址名称 //alert(addComp.province + ", " + addComp.city + ", " + addComp.district + ", " + addComp.street + ", " + addComp.streetNumber); pages.storeAdd.defaultAddress = addComp.province + ", " + addComp.city + ", " + addComp.district + ", " + addComp.street + ", " + addComp.streetNumber; //pages.storeLocation.addressInfo = defaultAddress; // $('.shopAdress').val(defaultAddress); // alert(pages.storeAdd.defaultAddress) $('.shopAdress').val(pages.storeAdd.defaultAddress); }); } else { alert('failed' + this.getStatus()); } }, { enableHighAccuracy: true }) }
3. 结束