百度API_獲取當前詳細地址

頁面統一調用:javascript

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

百度密鑰:Xbq3g4meudxD5Q0MB9osTLpghtml

JS調用:java

方法一:json

var geolocation = new BMap.Geolocation();
		geolocation.getCurrentPosition(function(r){
			if(this.getStatus() == BMAP_STATUS_SUCCESS){
				mylng = r.point.lng;		//經度
				mylat = r.point.lat;		//緯度
				var url="http://api.map.baidu.com/geocoder/v2/?ak=Xbq3g4meudxD5Q0MB9osTLpg&callback=renderReverse&location="+mylat+","+mylng+"&output=json&pois=0";
				var script=document.createElement('script');
				script.type='text/javascript';
				script.src=url;
				document.body.appendChild(script);			
			}else{
				alert('failed'+this.getStatus());
			}        
		},{enableHighAccuracy: true});
		
		renderReverse=function(response){
			var addr=response.result.formatted_address;
			alert('您如今的位置:'+addr);
		}

 

方法二:api

//獲取當前地址
		var geolocation = new BMap.Geolocation();
		var geoc = new BMap.Geocoder();
		geolocation.getCurrentPosition(function(r){
			if(this.getStatus() == BMAP_STATUS_SUCCESS){
				
				mylng = r.point.lng;		//經度
				mylat = r.point.lat;		//緯度
				var point = new BMap.Point(mylng,mylat);
				
				geoc.getLocation(point, function(rs){
					var addComp = rs.addressComponents;
					alert(addComp.province + ", " + addComp.city + ", " + addComp.district + ", " + addComp.street + ", " + addComp.streetNumber);
				}); 
			}else{
				alert('failed'+this.getStatus());
			}        
		},{enableHighAccuracy: true});
相關文章
相關標籤/搜索