百度地图API精准定位并显示门牌地址 - HTML代码示例
<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);
x_pi = 3.14159265358979324 * 3000.0 / 180.0;
x = r.point.lng;
y = r.point.lat;
z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * x_pi);
theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * x_pi);
lngs = z * Math.cos(theta) + 0.0065;
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>
<p>这段代码实现了调用百度地图API进行精准定位,并在弹窗中显示门牌地址。</p>

原文地址: http://www.cveoy.top/t/topic/qA77 著作权归作者所有。请勿转载和采集!