$("document").ready(function(){ getMap(); }); function getMap(){ // 百度地图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); var lng = r.point.lng; var lat = r.point.lat; getLocation( lng, lat ) } else { alert('failed'+this.getStatus()); } },{enableHighAccuracy: true}) } function getLocation( lng, lat ){ var url = "http://api.map.baidu.com/geocoder/v2/?ak=XXXXXXXXXXXXXXXXXXX&callback=renderReverse&location="+lat+","+lng+"&output=json&pois=1"; $.ajax({ url: url, type: 'GET', dataType: 'JSONP',//here success: function (result) { if( result.status == 0 ){ $("#location").html(result.result.formatted_address); nearbyStore( lng, lat ); }else{ if( confirm("获取位置失败,是否重新获取?") ){ getMap(); }else{ return false; } } } }); }