谷歌地圖地理解析和反解析geocode.geocoder詳解


地址解析就是將地址(如:貴州省貴陽市)轉換為地理坐標(如經度:106.71,緯度:26.57)的過程。

地理反解析和上面的過程相反是將地理坐標(如緯度:26.57,經度:106.71)轉換為地址(中國貴州省貴陽市南明區翠微巷7號 郵政編碼: 550002)的過程。
受當地法律限制及各方面原因,國內很多地圖並不包含地理解析和反解析功能(地理解析和反解析功能功能不夠強悍),Google永遠是最棒的。廢話不多說
要使用到Google map 地理解析和反解析功能,我們需要了解google.maps.Geocoder類,谷歌地圖給我們提供了強大的api,下面我們來實現
1.初始化地圖(最基本的,不解釋)

 
//初始化地圖
var map = new google.maps.Map(document.getElementById("map_canvas"),{
    center : new google.maps.LatLng(26.57, 106.72),
    zoom : 8,
    mapTypeId : google.maps.MapTypeId.ROADMAP
});

2.實例化谷歌Geocoder服務

 
//實例化Geocoder服務
var geocoder = new google.maps.Geocoder();

這樣我們就可以進行地理解析和反解析了,使用代碼:

 geocoder.geocode(request:GeocoderRequest, callback:function(Array.<GeocoderResult>, GeocoderStatus))
 

i.數據請求:

其中需要進行請求的數據GeocoderRequest可為4種屬性:

屬性 類型 描述
de style="margin: 0px; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; " >addressde> de style="margin: 0px; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; " >stringde> 需要解析的地名. 可選.
de style="margin: 0px; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; " >boundsde> de style="margin: 0px; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; " >LatLngBoundsde> 經緯度搜索范圍. 可選.(我沒有具體試用過)
de style="margin: 0px; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; " >locationde> de style="margin: 0px; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; " >LatLng(注意類型)de> 需要解析的經緯度. 可選.
de style="margin: 0px; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; " >regionde> de style="margin: 0px; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; " >stringde> 國家代碼. 可選.(我沒有具體試用過)

對於解析我們使用address,反解析使用location(注意傳入的類型),請求的話,至少選擇一種。

ii:結果處理:

而對於回掉函數(即解析后返回的處理函數)包含兩個內容,GeocoderResult(解析結果,數組類型)和GeocoderStatus(解析狀態)
1.解析狀態是使用Geocoder()進行解析后返回的狀態,包含5種:

ERROR(谷歌地圖服務可能出錯)
INVALID_REQUEST(GeocoderRequest無效,即輸入的請求是錯誤的,可能是沒有選擇,或者屬性寫錯)
OK(解析完成,並有相應數據)
OVER_QUERY_LIMIT(響應超時)
REQUEST_DENIED(網頁被禁止geocoder解析)
UNKNOWN_ERROR(未知錯誤)
ZERO_RESULTS(零結果)

我們能用的就是狀態為OK的情況
2.解析結果

屬性 類型 描述
de style="margin: 0px; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; " >address_componentsde> de style="margin: 0px; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; " >Array.<GeocoderAddressComponent>de> de style="margin: 0px; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; " >GeocoderAddressComponentde>s數組
de style="margin: 0px; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; " >formatted_addressde> de style="margin: 0px; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; " >stringde> 格式化后的最佳匹配地址(地名可小到街道)
de style="margin: 0px; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; " >geometryde> de style="margin: 0px; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; " >GeocoderGeometryde> de style="margin: 0px; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; " >GeocoderGeometryde> 對象
de style="margin: 0px; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; " >typesde> de style="margin: 0px; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; " >Array.<string>de> 一個表示返回的地理編碼元素的類型的字符串數組

其中每一次解析成功后都會有上面的信息,我們最需要的就兩樣formatted_address和geometry。而address_components是一個地名數組,包含long_name(比如只返回省市名稱),short_name和types,可以自己去試一下。

a. 格式化后的地名formatted_address,只需直接調用即可

b.geometry返回一個de style="margin: 0px; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; " >GeocoderGeometryde> 對象,其中又包含有4個屬性

Properties Type Description
de style="margin: 0px; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; " >boundsde> de style="margin: 0px; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; " >LatLngBoundsde> 解析出來的精確的界限
de style="margin: 0px; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; " >locationde> de style="margin: 0px; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; " >LatLngde> 緯度/經度坐標
de style="margin: 0px; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; " >location_typede> de style="margin: 0px; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; " >GeocoderLocationTypede> 返回的de style="margin: 0px; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; " >location類型de>
de style="margin: 0px; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; " >viewportde> de style="margin: 0px; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; " >LatLngBoundsde> 解析結果的視圖范圍

至此,所有關於地理解析和反解析就差不多說明完了,具體api參見https://developers.google.com/maps/documentation/javascript/reference#Geocoder

感覺說的好暈!基於此我做了一個關於批量解析和反解析的應用,詳見:http://map.yanue.net/geo.htmlhttp://map.yanue.net/toLatLng .使用詳解:http://www.yanue.net/archives/207.html ,

下面我們來實例一下

實例代碼:(你有必要復制到本地試一下)

 
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<script src="http://maps.google.com/maps/api/js?sensor=false&libraries=places" type="text/javascript"></script>
<title>谷歌地圖地理解析和反解析geocode.geocoder詳解</title>
<meta name="author" content="yanue" />
<meta name="copyright" content="powered by yanue" />
<link rel="site" href="http://map.yanue.net/" />
<script type="text/javascript">
window.onload = function() {
//初始化地圖
var map = new google.maps.Map(document.getElementById("map_canvas"),{
    center : new google.maps.LatLng(26.57, 106.72),
    zoom : 8,
    mapTypeId : google.maps.MapTypeId.ROADMAP
});
//實例化Geocoder服務
var geocoder = new google.maps.Geocoder();
 
//1.地理解析過程
//請求數據GeocoderRequest為address,值為'貴陽'
geocoder.geocode({address:'貴陽'},function geoResults(results, status){
  //這里是回掉函數(即結果處理函數)
  //狀態為Ok說明有結果
  if (status == google.maps.GeocoderStatus.OK) {
      //一般情況下會有多個結果
      //第一個結果為最佳匹配的結果(匹配地名最全的結果),這里只去第一個,其他的可以根據需要自己循環出來
      //格式化過后的地址
      alert('地理解析結果:'+results[0].formatted_address);
      //geometry是一個包含bounds(界限),location(緯度/經度坐標),location_type和viewport(視圖范圍)
      //獲取解析后的經緯度      
        alert('地理解析結果:'+results[0].geometry.location);
  }else{
    alert(":error " + status);
  }
});
 
//2.地理反解析過程
//請求數據GeocoderRequest為location,值類型為LatLng因此我們要實例化經緯度
geocoder.geocode({location:new google.maps.LatLng(26.57, 106.72)},function geoResults(results, status){
  //這里處理結果和上面一模一樣
  if (status == google.maps.GeocoderStatus.OK) {
      alert('地理反解析結果:'+results[0].formatted_address);
        alert('地理反解析結果:'+results[0].geometry.location);
  }else{
    alert(":error " + status);
  }
});
}
</script>
</head>
<body>
    <div id="map_canvas" style='width: 300px; height: 200px;'></div>
</body>
</html>

 

至此,文章完

 

http://www.yanue.net/archives/217.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM