简体   繁体   English

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

[英]Get latitude,longitude from address in Android

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. 我不想从GPS或网络中获得经度和纬度。

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. 我希望用户能够在TextView中输入他想要的地址(例如他的办公室)和文本字段下方的一些建议,就像在Google地图应用中键入地址一样。 Then i want to retrieve the coordinates of the given address. 然后我想检索给定地址的坐标。

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. 也许我可以从我的应用程序调用gmaps,用户将键入地址,gmaps将返回坐标。

How can i do it? 我该怎么做?

The Geocoder API provided in the Android Framework Android框架中提供的Geocoder API

I have previously worked with the Geocoding API which exists in the Android API but it does not work on all devices. 我之前使用的是Geocoding API,它存在于Android API中,但它并不适用于所有设备。 In fact, as per my and others experiences, a lot of devices will return null when using the Geocoding API. 事实上,根据我和其他人的经验,许多设备在使用Geocoding API时将返回null。

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

Retrieving the longitude and latitude from an address using the Reverse Geocoding API 使用反向地理编码API从地址中检索经度和纬度

To avoid this issue, it is a simple matter of using the Reverse Geocoding API which returns a JSON Object. 为了避免这个问题,使用返回JSON对象的反向地理编码API很简单。

You can either use the API to find an address through latitude and longitude, or find latitude and longitude from an address: 您可以使用API​​通过纬度和经度查找地址,也可以从地址中查找纬度和经度:

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

Returns: 返回:

{
   "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"
}

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. 因此,向反向地理编码API发送HTTP请求的简单问题是用户输入的地址,然后解析JSON对象以找到所需的数据。

A previous post describes how a JSON object can be parsed from an URL . 上一篇文章描述了如何从URL解析JSON对象

Hope this helps. 希望这可以帮助。

You can use the method below: 您可以使用以下方法:

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

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

You can use it like that: 你可以像这样使用它:

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

You will get a list of Address and you can then call getLatitude and getLongitude on these addresses. 您将获得一个Address列表,然后您可以在这些地址上调用getLatitudegetLongitude

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

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