google地址解析是根據地名得到對應的經緯度,反解析就是根據經緯度得到和經緯度相匹配的地名,可能有多個。google地址反解析服務的輸出格式支持xml、csv、json。
請求及響應如下:
xml請求:
http://ditu.google.cn/maps/geo?output=xml&key=abcdef&q=39.924000,116.310100,其中key的值隨便給就行了甚至不給也沒問題,q=39.924000,116.310100爲要反解析的緯度和經度。
xml響應:
<?xml version="1.0" encoding="UTF-8" ?>
<kml xmlns="http://earth.google.com/kml/2.0"><Response>
<name>39.924000,116.310100</name>
<Status>
<code>200</code>
<request>geocode</request>
</Status>
<Placemark id="p1">
<address>中國北京市海淀區西三環中路3號</address>
<AddressDetails Accuracy="8" xmlns="urn:oasis:names:tc:ciq:xsdschema:xAL:2.0"><Country><CountryNameCode>CN</CountryNameCode><CountryName>中國</CountryName><AdministrativeArea><AdministrativeAreaName>北京市</AdministrativeAreaName><DependentLocality><DependentLocalityName>海淀區</DependentLocalityName><Thoroughfare><ThoroughfareName>西三環中路3號</ThoroughfareName></Thoroughfare></DependentLocality></AdministrativeArea></Country></AddressDetails>
<ExtendedData>
<LatLonBox north="39.9271476" south="39.9208524" east="116.3132953" west="116.3070001" />
</ExtendedData>
<Point><coordinates>116.3101242,39.9240000,0</coordinates></Point>
</Placemark>
<Placemark id="p2">
<address>中國北京市海淀區航天橋站
</address>
<AddressDetails Accuracy="9"
xmlns="urn:oasis:names:tc:ciq:xsdschema:xAL:2.0"><AddressLine>航天橋站</AddressLine></AddressDetails>
<ExtendedData>
<LatLonBox north="39.9271796" south="39.9208844" east="116.3135696" west="116.3072744" />
</ExtendedData>
<Point><coordinates>116.3104220,39.9240320,0</coordinates></Point>
</Placemark>
<Placemark id="p3">
<address>中國北京市海淀區航天橋(109國道)</address>
<AddressDetails Accuracy="4" xmlns="urn:oasis:names:tc:ciq:xsdschema:xAL:2.0"><Country><CountryNameCode>CN</CountryNameCode><CountryName>中國</CountryName><AdministrativeArea><AdministrativeAreaName>北京市</AdministrativeAreaName><DependentLocality><DependentLocalityName>海淀區</DependentLocalityName><AddressLine>航天橋(109國道)</AddressLine></DependentLocality></AdministrativeArea></Country></AddressDetails>
<ExtendedData>
<LatLonBox north="39.9270946" south="39.9207994" east="116.3139006" west="116.3076054" />
</ExtendedData>
<Point><coordinates>116.3107530,39.9239470,0</coordinates></Point>
</Placemark>
<Placemark id="p4">
<address>中國北京市海淀區</address>
<AddressDetails Accuracy="4" xmlns="urn:oasis:names:tc:ciq:xsdschema:xAL:2.0"><Country><CountryNameCode>CN</CountryNameCode><CountryName>中國</CountryName><AdministrativeArea><AdministrativeAreaName>北京市</AdministrativeAreaName><DependentLocality><DependentLocalityName>海淀區</DependentLocalityName></DependentLocality></AdministrativeArea></Country></AddressDetails>
<ExtendedData>
<LatLonBox north="40.1567397" south="39.8876256" east="116.3919132" west="116.0470104" />
</ExtendedData>
<Point><coordinates>116.2984190,39.9594050,0</coordinates></Point>
</Placemark>
<Placemark id="p5">
<address>中國北京市北京市市轄區</address>
<AddressDetails Accuracy="4" xmlns="urn:oasis:names:tc:ciq:xsdschema:xAL:2.0"><Country><CountryNameCode>CN</CountryNameCode><CountryName>中國</CountryName><Locality><LocalityName>北京市</LocalityName><AddressLine>北京市市轄區</AddressLine></Locality></Country></AddressDetails>
<ExtendedData>
<LatLonBox north="40.1785550" south="39.5078540" east="116.6333300" west="115.4138100" />
</ExtendedData>
<Point><coordinates>115.9467997,39.9229023,0</coordinates></Point>
</Placemark>
<Placemark id="p6">
<address>中國北京市北京城區</address>
<AddressDetails Accuracy="3" xmlns="urn:oasis:names:tc:ciq:xsdschema:xAL:2.0"><Country><CountryNameCode>CN</CountryNameCode><CountryName>中國</CountryName><AdministrativeArea><AdministrativeAreaName>北京市</AdministrativeAreaName><AddressLine>北京城區</AddressLine></AdministrativeArea></Country></AddressDetails>
<ExtendedData>
<LatLonBox north="41.0608685" south="39.4432527" east="117.3895952" west="115.4203742" />
</ExtendedData>
<Point><coordinates>116.4081980,39.9046670,0</coordinates></Point>
</Placemark>
</Response></kml>
其中,每個Placemark爲一個解析出的地址點,其描述爲address元素內容,匹配程度爲屬性Accuracy的值說明,值越高說明匹配程度越高,這個地址點真正對應的經緯度爲coordinates元素內容說明。這種輸出格式有點煩雜,甚至有點錯誤,但不影響結果,下述。
csv請求:
http://ditu.google.cn/maps/geo?output=csv&key=abcdef&q=39.924000,116.310100
csv響應:
200,8,"中國北京市海淀區西三環中路3號"
其中,200爲響應碼,620表明超出請求數量限制;8應該爲匹配程度,後面的爲地址描述。這種形式比較簡潔,但是不一定是最臨近請求點的匹配結果。從xml的響應中可以看到還有爲9的響應匹配(中國北京市海淀區航天橋站 )。
xml格式的響應最爲全面,可以根據一定的規則挑選適合的Placemark,並可以利用實際經緯度和請求經緯度的差做進一步說明,比如距離、方位。
google地址反解析xml服務可能推出時間不長,或者使用的較少,或者是其他原因。xml返回中說明是UTF-8編碼,<?xml version="1.0" encoding="UTF-8" ?>,但是實際上是GBK的編碼。說明和實際不一致,導致xml解析時出錯,這可能算是google的一個疏忽了。不過可以強制指定要解析內容的編碼,而不是根據文檔的自身說明就可以了。
另外,從測試結果看,能達到匹配值4以上的時候比較少,也就時說偏差比較大,這些服務還不夠實用。