百度地图API定位+显示位置

1. 先在需要嵌入地图的页面引入map.js

<script src="http://api.map.baidu.com/api?v=2.0&ak=你的秘钥"></script>

2. 地图定位并显示位置信息

 // 百度地图API功能
var map = new BMap.Map("allmap");
var point = new BMap.Point(116.331398,39.897445);
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);
        map.addOverlay(mk);
        map.panTo(r.point);
        // alert('您的位置:'+r.point.lng+','+r.point.lat);
        var point = new BMap.Point(r.point.lng,r.point.lat);//用所定位的经纬度查找所在地省市街道等信息
        var gc = new BMap.Geocoder();
        gc.getLocation(point, function(rs){
           var addComp = rs.addressComponents; console.log(rs.address);//地址信息
           $('#place').append(rs.address)
           //alert(rs.address);//弹出所在地址

        });
    }
    else {
        alert('failed'+this.getStatus());
    }
},{enableHighAccuracy: true})

猜你喜欢

转载自www.cnblogs.com/wyp-AY/p/9361663.html