在Google地图反向地理编码中获取明确的城市名称 [英] Get definite City name in Google maps reverse geocoding

查看:155
本文介绍了在Google地图反向地理编码中获取明确的城市名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Google地图地理编码API,我可以获取特定坐标的格式化地址。为了得到确切的城市名称,我做了以下工作:

  $。ajax({
url:' http://maps.googleapis.com/maps/api/geocode/json?latlng='+lat+','+long+'&sensor=false',
success:function(data){
var formatted = data.results;
var address_array = formatted [6] .formatted_address.split(',');
var city = address_array [0];
}
});

其中 lat long 是使用浏览器坐标派生的。我的问题如下:



坐标 19.2100 72.1800 ,我得到的城市是孟买,但是距离大约3公里的一个类似的坐标系,我得到的城市为孟买郊区。如何在不改变我的代码的成功功能的情况下获得 Mumbai >?在我看来,结果数组并不总是遵循相同的格式,这在我显示城市名称时会产生问题。 解决方案

您需要查看结果的类型,而不是结果数组中的绝对索引。遍历结果数组,查找具有相应类型的条目。看起来像这样:


  • locality表示一个合并的城市或城镇政治实体



但数据可能因地区而异。

相关问题:从谷歌地理编码jquery抓取国家



看到这个结果,看起来像你想要的条目'b地方'和'政治'类型:

  {
long_name:Mumbai,
short_name:孟买,
types:[locality,political]
}


Using the Google Maps Geocoding API, i'm able to get the formatted address for a particular coordinate. To get the exact city name, I'm doing the following:

$.ajax({
    url: 'http://maps.googleapis.com/maps/api/geocode/json?latlng='+lat+','+long+'&sensor=false',
    success: function(data){
        var formatted = data.results;
        var address_array = formatted[6].formatted_address.split(',');
        var city = address_array[0];
   }
});

where lat and long are derived using the browser coordinates. My problem is the following:

From coordinates 19.2100 and 72.1800, I get the city as Mumbai, but from a similar set of coordinates about 3Km away, I get city as Mumbai Suburban. How can I get Mumbai without changing the success function of my code? It seems to me that the result array doesn't always stick to the same format which creates problems in my displaying of the city name.

解决方案

You need to look at the type of the result, not the absolute index in the array of results. Iterate through the results array looking for the entry which has the appropriate type. Looks like that would be:

  • locality indicates an incorporated city or town political entity

But data may vary with region.

related question: Grabbing country from google geocode jquery

See this result, looks like you want the entry with both the 'locality' and the 'political' types:

{
   "long_name" : "Mumbai",
   "short_name" : "Mumbai",
   "types" : [ "locality", "political" ]
}

这篇关于在Google地图反向地理编码中获取明确的城市名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆