谷歌地图:如何获得国家、州/省/地区、城市的纬度/经度值? [英] Google Maps: how to get country, state/province/region, city given a lat/long value?

查看:42
本文介绍了谷歌地图:如何获得国家、州/省/地区、城市的纬度/经度值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一份国家、州和地区的清单基于我拥有的一系列经纬度值的城市.我需要以保留层次结构且没有重复的方式存储此信息(例如美国"和美国"和美利坚合众国"是同一个国家;我只希望我的数据库中有这个国家的一个实例).

I need a list of countries, states & cities based on a collection of lat/long values I have. I need to store this information in a manner that hierarchy is preserve and without duplicates (e.g. "USA" and "United States" and "United States of America" are the same country; I only want one instance of this country in my database).

这可能与 Google Map API 相关吗?

Is this possible to do with Google Map API?

推荐答案

您要找的东西叫做 reverse地理编码.Google 通过 Google Geocoding API 提供服务器端反向地理编码服务,您应该可以将其用于您的项目.

What you are looking for is called reverse geocoding. Google provides a server-side reverse geocoding service through the Google Geocoding API, which you should be able to use for your project.

这是对以下请求的响应的样子:

This is how a response to the following request would look like:

http://maps.googleapis.com/maps/api/geocode/json?latlng=40.714224,-73.961452&sensor=false

回复:

{
  "status": "OK",
  "results": [ {
    "types": [ "street_address" ],
    "formatted_address": "275-291 Bedford Ave, Brooklyn, NY 11211, USA",
    "address_components": [ {
      "long_name": "275-291",
      "short_name": "275-291",
      "types": [ "street_number" ]
    }, {
      "long_name": "Bedford Ave",
      "short_name": "Bedford Ave",
      "types": [ "route" ]
    }, {
      "long_name": "New York",
      "short_name": "New York",
      "types": [ "locality", "political" ]
    }, {
      "long_name": "Brooklyn",
      "short_name": "Brooklyn",
      "types": [ "administrative_area_level_3", "political" ]
    }, {
      "long_name": "Kings",
      "short_name": "Kings",
      "types": [ "administrative_area_level_2", "political" ]
    }, {
      "long_name": "New York",
      "short_name": "NY",
      "types": [ "administrative_area_level_1", "political" ]
    }, {
      "long_name": "United States",
      "short_name": "US",
      "types": [ "country", "political" ]
    }, {
      "long_name": "11211",
      "short_name": "11211",
      "types": [ "postal_code" ]
    } ],
    "geometry": {
      "location": {
        "lat": 40.7142298,
        "lng": -73.9614669
      },
      "location_type": "RANGE_INTERPOLATED",
      "viewport": {
        "southwest": {
          "lat": 40.7110822,
          "lng": -73.9646145
        },
        "northeast": {
          "lat": 40.7173774,
          "lng": -73.9583193
        }
      }
    }
  },

  ... Additional results[] ...

您也可以选择以 xml 而不是 json 格式接收响应,只需将请求 URI 中的 xml 替换为 json:

You can also opt to receive the response in xml instead of json, by simply substituting json for xml in the request URI:

http://maps.googleapis.com/maps/api/geocode/xml?latlng=40.714224,-73.961452&sensor=false

据我所知,Google 也会为地址组件返回相同的名称,尤其是对于国家名称和城市名称等高级名称.不过,请记住,虽然结果对于大多数应用程序来说非常准确,但您仍然会偶尔发现拼写错误或含糊不清的结果.

As far as I know, Google will also return the same name for address components, especially for high-level names like country names and city names. Nevertheless, keep in mind that while the results are very accurate for most applications, you could still find the occasional spelling mistake or ambiguous result.

这篇关于谷歌地图:如何获得国家、州/省/地区、城市的纬度/经度值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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