修正完整以下代码实现调用百度地图API进行精准定位并在弹窗中显示门牌地址div id=initMap style=display none;div script type=textjavascript src=apimapbaiducomapiv=30&ak=gHILVzmuMIlwy2Z5lE33dedbeI87UIZKscript script var zd = getLocation;
修正后的代码如下:
<div id="initMap" style="display: none;"></div>
<script type="text/javascript" src="//api.map.baidu.com/api?v=3.0&ak=gHILVzmuMIlwy2Z5lE33dedbeI87UIZK"></script>
<script>
var zd = getLocation();
function getLocation() {
// 百度地图API功能
var map = new BMapGL.Map("initMap");
var point = new BMapGL.Point(116.331398,39.897445);
map.centerAndZoom(point, 12);
var geolocation = new BMapGL.Geolocation();
geolocation.getCurrentPosition(function(r){
console.log(r)
if(this.getStatus() == BMAP_STATUS_SUCCESS){
console.log('您的位置:' + r.point.lng + ',' + r.point.lat);
var x_pi = 3.14159265358979324 * 3000.0 / 180.0;
var x = r.point.lng;
var y = r.point.lat;
var z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * x_pi);
var theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * x_pi);
var lngs = z * Math.cos(theta) + 0.0065;
var lats = z * Math.sin(theta) + 0.006;
var point = new BMapGL.Point(lngs, lats);
var gc = new BMapGL.Geocoder();
gc.getLocation(point, function(rs){
var addComp = rs.addressComponents;
console.log(rs.address);//地址信息
alert(rs.address);
});
}
});
}
</script>
这个修正的代码会在页面加载时调用百度地图API获取当前位置信息,并在弹窗中显示门牌地址
原文地址: http://www.cveoy.top/t/topic/iU9g 著作权归作者所有。请勿转载和采集!