从地址获取 Android 中的纬度和经度

15
我想从地址中获取地点的纬度和经度,不想使用GPS或网络获取。
我希望用户能够在 TextView 中输入他想要的地方的地址(例如他的办公室),在文本框下方显示一些建议,就像在 Google Maps 应用中输入地址时一样。然后我想检索给定地址的坐标。
如果无法实现这一点,是否有一种方法可以通过 Google Maps 应用本身获取地址。也许我可以从我的应用程序调用 Gmaps,用户将键入地址,而 Gmaps 将返回坐标。
我该如何做?
4个回答

16

Android框架中提供的地理编码器API

我之前使用过Android API中存在的地理编码API,但它并不适用于所有设备。实际上,根据我的和其他人的经验,许多设备在使用地理编码API时会返回null。

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

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

为了避免这个问题,只需简单地使用反向地理编码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对象以找到所需的数据即可。

之前的一篇文章描述了如何从URL中解析JSON对象

希望这可以帮助您。


我花了2个小时尝试使用Geocoding API让它工作,但没有成功。Geocoder.getFromLocationName(address, x)总是返回null。这种方法是最好的方法。感谢您的回答! - Christos Baziotis
我在多个设备上遇到了完全相同的问题;它在某些设备上可以工作,但远非全部。这种方法几乎可以保证有效,但与地理编码器相比需要额外的开销。但是,我个人更喜欢可靠性而不是性能 :) 很高兴它有所帮助。 - Demitrian

9
你可以使用以下方法:
        Geocoder coder = new Geocoder(this);
    try {
        ArrayList<Address> adresses = (ArrayList<Address>) coder.getFromLocationName("Your Address", 50);
        for(Address add : adresses){
            if (statement) {//Controls to ensure it is right address such as country etc.
                double longitude = add.getLongitude();
                double latitude = add.getLatitude();
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

2
/**
 * @method getLocationFromAddress
 * @param strAddress Address/Location String
 * @desc Get searched location points from address and plot/update on map.
 */
public void getLocationFromAddress(String strAddress)
{
    //Create coder with Activity context - this
    Geocoder coder = new Geocoder(this);
    List<Address> address;

    try {
        //Get latLng from String
        address = coder.getFromLocationName(strAddress,5);

        //check for null
        if (address == null) {
            return;
        }

        //Lets take first possibility from the all possibilities.
        Address location=address.get(0);
        LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());

        //Put marker on map on that LatLng
        Marker srchMarker = mMap.addMarker(new MarkerOptions().position(latLng).title("Destination").icon(BitmapDescriptorFactory.fromResource(R.drawable.bb)));

        //Animate and Zoon on that map location
        mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
        mMap.animateCamera(CameraUpdateFactory.zoomTo(15));

    } catch (IOException e)
    {
        e.printStackTrace();
    }
}

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接