简体   繁体   中英

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.

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.

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?

The Geocoder API provided in the Android Framework

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.

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.

Retrieving the longitude and latitude from an address using the Reverse Geocoding API

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

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

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.

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

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 .

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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