Muhammad Hashim Shafiq
Muhammad Hashim Shafiq

Reputation: 653

How to get Location coordinates from a String Address in Android?

I have an activity with an EditText to enter the address. When the user enters the address and presses the save button I want to get Location coordinates from the entered address. Is this possible by using Google Maps or Geocoding API's? I have checked and not successful.

Any help will be appreciated.

Upvotes: 1

Views: 1128

Answers (1)

MurugananthamS
MurugananthamS

Reputation: 2405

Try this:

  <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>

  <uses-permission android:name="android.permission.INTERNET"/>


 public LatLng getLocationFromAddress(Context context,String strAddress) {

Geocoder coder = new Geocoder(context);
List<Address> address;
LatLng LatLan= null;

try {
    // May throw an IOException
    address = coder.getFromLocationName(strAddress, 5);
    if (address == null) {
        return null;
    }

    Address location = address.get(0);
    LatLan= new LatLng(location.getLatitude(), location.getLongitude() );

} catch (IOException ex) {

    ex.printStackTrace();
}

return LatLan;
}

Upvotes: 2

Related Questions