Baidu Maps JavaScript API to obtain the user's current latitude and longitude location details and get the current user

Preface:

  The front end of the time just use Baidu maps js api get the user current latitude and longitude positioning and get the current position of the features in detail, in order to facilitate easy to find them in some of their next record here what I hope to be able to help needy children's shoes!

solution:

First, the introduction of JavaScript API v2.0 SDK

<script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=您的密钥"></script>

Second, in order to define a DIV tag ID of allmap of the page:

<div id="allmap"></div>

Avoid page appears:

Uncaught TypeError: Cannot read property 'gc' of undefined

Third, access to the SDK assisted positioning coordinates, then get the current user Address

<Script of the type = " text / JavaScript " >
     var the Map = new new BMap.Map ( " allmap " ); // Create a Map instance, attention must have a page id of the div allmp 
    var Point = new new BMap.Point ( 116.331398 , 39.897445 ); // create the given coordinates 
    map.centerAndZoom (Point, 12 ); /// / initialize the map, set the level of the center coordinates and maps 

    var Geolocation = new new BMap.Geolocation ();
     var gc = new new BMap.Geocoder ( ); // create a geocoder
     // open SDK assisted positioning
    geolocation.enableSDKLocation (); 
    geolocation.getCurrentPosition (function (R & lt) { 
        IF ( the this .getStatus () == BMAP_STATUS_SUCCESS) {
             var MK = new new BMap.Marker (r.point); 
            the Map.addOverlay (MK); 
            map.panTo (r.point); 
            Alert ( ' location: ' + r.point.lng + ' , ' + r.point.lat); 

            var Pt = r.point;    
            map.panTo (Pt); // move the map center point
             // Alert (r.point.lng); // X-axis
             //alert(r.point.lat);//Y轴 
 
            gc.getLocation(pt, function(rs){    
                var addComp = rs.addressComponents;
                //alert(addComp.city);
               alert(addComp.province + addComp.city + addComp.district + addComp.street + addComp.streetNumber);    
            });  

        }
        else {
            alert('failed'+this.getStatus());
        }        
    });
</script>

Fourth, the current latitude and longitude acquired by the browser:

<script type="text/javascript">
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);
    }
    else {
        alert('failed'+this.getStatus());
    }        
});
</script>

Five, ip localization obtain current city

<script type="text/javasript">
var map = new BMap.Map("allmap");
var point = new BMap.Point(116.331398,39.897445);
map.centerAndZoom(point,12);

function myFun(result){
    var cityName = result.name;
    map.setCenter(cityName);
    alert("当前定位城市:"+cityName);
}
var myCity = new BMap.LocalCity();
myCity.get(myFun); 
</script>

 

Guess you like

Origin www.cnblogs.com/Can-daydayup/p/10941470.html