获取纬度,经度从Android的地址 [英] Get latitude,longitude from address in Android

查看:149
本文介绍了获取纬度,经度从Android的地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获得一个位置的纬度和经度的地址。 我不希望得到从GPS纬度和经度,也不是在网络

I want to get the latitude and longitude of a place from an address. I don't want to get the latitude and longitude from the GPS nor the the network.

我希望用户能够在一个TextView输入他想要的地方的地址(例如:他的办公室)和文本框的一些建议,将图所示,只是当你键入谷歌地图应用程序的地址一样。然后我想获取给定地址的坐标。

I want the user to be able to type in a TextView the address of the place he wants (ex. his office) and below the textfield some suggestions will be shown, just like when you type the address in the google maps app. Then i want to retrieve the coordinates of the given address.

如果不能这样做是有办法获得通过谷歌地图应用程序本身的地址。也许我可以从我的应用程序调用的GMaps实现,用户将输入地址和GMaps实现将返回的坐标。

If this can't be done is there a way to get the address via the google maps app itself. Perhaps i could call the gmaps from my app and the user will type the address and the gmaps would return the coordinates.

我怎样才能做到这一点?

How can i do it?

推荐答案

在Android框架提供的地理codeR API

我有previously与地理编码的API,它存在于Android的API中工作但它在所有设备无法正常工作。其实,按我和其他人的经验,很多设备都将使用地理编码API时,返回null。

I have previously worked with the Geocoding API which exists in the Android API but it does not work on all devices. In fact, as per my and others experiences, a lot of devices will return null when using the Geocoding API.

出于这个原因,我选择了使用反向地理codeR它完美的作品在所有设备上,但需要额外的开销,由于HTTP请求。​​

For that reason, I have chosen to use the Reverse Geocoder which works perfectly on all devices, but requires an additional overhead due to the HTTP request.

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

要避免这个问题,这是使用反向地理编码API 它返回一个JSON的一个简单的事情对象。

To avoid this issue, it is a simple matter of using the Reverse Geocoding API which returns a JSON Object.

您可以使用API​​通过经纬度来查找地址,或者发现纬度和经度从地址:

You can either use the API to find an address through latitude and longitude, or find latitude and longitude from an address:

<一个href="http://maps.googleapis.com/maps/api/geo$c$c/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=true_or_false">http://maps.googleapis.com/maps/api/geo$c$c/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"
}

这是现发送一个HTTP请求反向地理编码API与用户输入一个地址,然后解析JSON对象找到你需要的数据的一个简单的事情。

It is hereby a simple matter of sending a HTTP request to the Reverse Geocoding API with an address entered by the user, and thereafter parsing the JSON Object to find the data you need.

一个previous职位描述 JSON对象可以从网址解析。

A previous post describes how a JSON object can be parsed from an URL.

希望这有助于。

这篇关于获取纬度,经度从Android的地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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