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以上的时候比较少,也就时说偏差比较大,这些服务还不够实用。