您的位置:首页 > 其它

百度地图----浏览器定位获得详细地址

2017-10-23 17:13 232 查看

浏览器定位获得详细地址

// 百度地图API功能
var point = null;
var map = new BMap.Map("l-map");
map.centerAndZoom(new BMap.Point(116.395645,39.929986), 7);
map.enableScrollWheelZoom(true);
//获取浏览器定位
var geolocation = new BMap.Geolocation();
geolocation.getCurrentPosition(function(res){
if(res){
alert(JSON.stringify(res.point));
point = res.point;
analysis(res.point);
// map.centerAndZoom(new BMap.Point(res.point.lng,res.point.lat),12);
}
});
//解析经纬度
function analysis(pt){
var geoc = new BMap.Geocoder();
geoc.getLocation(new BMap.Point(pt.lng,pt.lat),function(res){
alert(res.addressComponents.city.substring(0,2));
map.centerAndZoom(res.address,12);
geoc.getPoint(res.address, function(pt){
pt && addMarker(new BMap.Point(pt.lng, pt.lat),new BMap.Label( res.address , {offset:new BMap.Size(20,-10)}));
}, "中国");
// map.addOverlay(new BMap.Marker(pt.lng,pt.lat),new BMap.Label(res.address, {offset:new BMap.Size(20,-10)}));
});
}
function addMarker(point,label){// 编写自定义函数,创建标注
var marker = new BMap.Marker(point);
map.addOverlay(marker);
marker.setLabel(label);
// marker.setAnimation(BMAP_ANIMATION_BOUNCE);//图标动画
}


demo效果

demo下载
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐