繁体   English   中英

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

[英]Get latitude,longitude from address in Android

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

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

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

我该怎么做?

Android框架中提供的Geocoder API

我之前使用的是Geocoding API,它存在于Android API中,但它并不适用于所有设备。 事实上,根据我和其他人的经验,许多设备在使用Geocoding API时将返回null。

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

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

为了避免这个问题,使用返回JSON对象的反向地理编码API很简单。

您可以使用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对象

希望这可以帮助。

您可以使用以下方法:

        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();
    }
/**
 * @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();
    }
}

你需要的是: http//developer.android.com/reference/android/location/Geocoder.html

你可以像这样使用它:

List<Address> result = new Geocoder(context).getFromLocationName(locationName, maxResults);

您将获得一个Address列表,然后您可以在这些地址上调用getLatitudegetLongitude

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM