Search code examples
javaandroidgoogle-mapsgeocodinggoogle-geocoder

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.


Solution

  • 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;
    }