android - 从Android中的地址获取经纬度

标签 android location coordinates

我想从一个地址获取一个地方的经纬度。 我不想从 GPS 或网络获取经纬度。

我希望用户能够在 TextView 中输入他想要去的地方(例如他的办公室)的地址,然后在文本字段下方显示一些建议,就像您在谷歌地图应用中输入地址一样.然后我想检索给定地址的坐标。

如果无法做到这一点,是否有办法通过谷歌地图应用程序本身获取地址。也许我可以从我的应用程序调用 gmaps,用户将键入地址,gmaps 将返回坐标。

我该怎么做?

最佳答案

Android Framework 中提供的 Geocoder API

我之前使用过 Geocoding API which exists in the Android API但它不适用于所有设备。事实上,根据我和其他人的经验,很多设备在使用 Geocoding API 时都会返回 null。

出于这个原因,我选择使用在所有设备上都能完美运行的反向地理编码器,但由于 HTTP 请求需要额外的开销。

使用反向地理编码 API 从地址检索经度和纬度

要避免这个问题,使用 Reverse Geocoding API 很简单。它返回一个 JSON 对象。

您可以使用 API 通过经纬度查找地址,也可以通过地址查找经纬度:

http://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=true_or_false

返回:

{
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "1600",
               "short_name" : "1600",
               "types" : [ "street_number" ]
            },
            {
               "long_name" : "Amphitheatre Pkwy",
               "short_name" : "Amphitheatre Pkwy",
               "types" : [ "route" ]
            },
            {
               "long_name" : "Mountain View",
               "short_name" : "Mountain View",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Santa Clara",
               "short_name" : "Santa Clara",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "California",
               "short_name" : "CA",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "United States",
               "short_name" : "US",
               "types" : [ "country", "political" ]
            },
            {
               "long_name" : "94043",
               "short_name" : "94043",
               "types" : [ "postal_code" ]
            }
         ],
         "formatted_address" : "1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA",
         "geometry" : {
            "location" : {
               "lat" : 37.42291810,
               "lng" : -122.08542120
            },
            "location_type" : "ROOFTOP",
            "viewport" : {
               "northeast" : {
                  "lat" : 37.42426708029149,
                  "lng" : -122.0840722197085
               },
               "southwest" : {
                  "lat" : 37.42156911970850,
                  "lng" : -122.0867701802915
               }
            }
         },
         "types" : [ "street_address" ]
      }
   ],
   "status" : "OK"
}

因此,使用用户输入的地址向反向地理编码 API 发送 HTTP 请求很简单,然后解析 JSON 对象以找到您需要的数据。

之前的一篇文章描述了如何 a JSON object can be parsed from an URL .

希望这对您有所帮助。

关于android - 从Android中的地址获取经纬度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17835426/

相关文章:

Android - 自定义小部件未更新

android - setLatitude setLongitude NullPointer 异常

sql-server-2008 - 使用纬度和经度返回SQL Server 2008中两个位置之间的距离

coordinates - 通过距离和方位从已知位置查找点坐标的地理算法

java - LinkedHashSet 不删除重复项

Android AdWhirl 和 AdSense 3.1

java - java中文本中的数字下标

java - 如何在 pojoclass 的帮助下将 JSon 数组转换为 Arraylist?

java - 定位服务还是定位线程?

swift - UIScreen.main.nativeBounds 与 x 和 y 坐标有何关系?