如何从谷歌地图地理编码返回地址类型?
我目前正在使用谷歌地图 API 对地图上的位置进行地理编码并返回街道的地址。
我目前使用以下代码返回地址:
function codeLatLng(markerPos) {
geocoder.geocode({'latLng': markerPos}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (results[1]) {
//Set markerAddress variable
var markerAddress = results[0].formatted_address;
alert(markerAddress);
...
但是,如果我不想返回格式化地址,而是使用地址组件类型返回更详细的版本,如何返回某些地址值,例如: http://code.google.com/apis/maps/documentation/geocoding/#Types
感谢帮助。
I am currently using the google maps api to geocode locations on the map and return the address of the street.
I currently return the address with the following code:
function codeLatLng(markerPos) {
geocoder.geocode({'latLng': markerPos}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (results[1]) {
//Set markerAddress variable
var markerAddress = results[0].formatted_address;
alert(markerAddress);
...
But what if I don't want to return the formatted address but a more detailed version using Address Component Types, how can I return certain address values like: http://code.google.com/apis/maps/documentation/geocoding/#Types
Help appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请问,为什么要检查
results[1]
然后使用results[0]
(或者这只是我应该忽略的拼写错误)?只要
status
为OK
,就会至少一个结果。否则,
status
将为ZERO_RESULTS
。无论如何,您可以使用这样的东西:
您可以使用整个
address_components
数组进行很多操作,玩得开心! :)为了获得更多乐趣,请查看 Google Maps API v3 Geocoder Tool(源代码),网址为 http://gmaps-samples-v3.googlecode.com/svn/trunk/geocoder/v3-geocoder-tool.html
May I ask, why are you checking for
results[1]
and then usingresults[0]
(or is it just a typo I should ignore)?As long as
status
isOK
, there will be at least one result.Otherwise,
status
would beZERO_RESULTS
.Anyway, you can use something like this:
You can play a lot with the whole
address_components
array, have fun! :)For even more fun, have a look at the (source code of the) Google Maps API v3 Geocoder Tool at http://gmaps-samples-v3.googlecode.com/svn/trunk/geocoder/v3-geocoder-tool.html
long_name
是地理编码器返回的地址组件的完整文本描述或名称。long_name
is the full text description or name of the address component as returned by the Geocoder.