33

How do i get gps co-ordinates of the location/address entered by user in android ?

Rahul Baradia
  • 11,802
  • 17
  • 73
  • 121

5 Answers5

80
Geocoder geocoder = new Geocoder(<your context>);  
List<Address> addresses;
addresses = geocoder.getFromLocationName(<String address>, 1);
if(addresses.size() > 0) {
    double latitude= addresses.get(0).getLatitude();
    double longitude= addresses.get(0).getLongitude();
}
Chau
  • 5,540
  • 9
  • 65
  • 95
Natali
  • 2,934
  • 4
  • 39
  • 53
  • 2
    @natali I want to say one thing ...hmmmmm your profile image is very very very.... cute :).. – Satyam Jul 10 '12 at 07:48
11

You can use Android's Geocoder to do reverse geocoding:

Geocoder geocoder = new Geocoder(this, Locale.getDefault());
List<Address> addresses = geocoder.getFromLocationName(myLocation, 1);
Address address = addresses.get(0);
double longitude = address.getLongitude();
double latitude = address.getLatitude();

Also include the following in AndroidManifest.xml:

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

Also note that you need to be using an API which includes a Geocoder implementation. APIs which include this are the Android Google APIs for example. You can use Geocoder.isPresent() to check if an implementation exists for your targeted API.

Check out the Geocoder documentation for more information.

Tyler Treat
  • 14,640
  • 15
  • 80
  • 115
3
List<Address> addresses;
addresses = geocoder.getFromLocationName(<String address>, 1);
if(addresses.size() > 0){
double latitude= addresses.get(0).getLatitude();
double longitude= addresses.get(0).getLongitude();
}

manifest permissions:-

android.permission.INTERNET
android.permission.ACCESS_COARSE_LOCATION
android.permission.ACCESS_FINE_LOCATION
android.permission.ACCESS_MOCK_LOCATION
IAmGroot
  • 13,760
  • 18
  • 84
  • 154
Prachi
  • 2,559
  • 4
  • 25
  • 37
2

use manifest permissions like

android.permission.INTERNET 
android.permission.ACCESS_COARSE_LOCATION     
android.permission.ACCESS_FINE_LOCATION 
android.permission.ACCESS_MOCK_LOCATION

and go with this

Geocoder geocoder = new Geocoder(this, Locale.getDefault());
List<Address> addresses = geocoder.getFromLocationName(myLocation, 1);
Address address = addresses.get(0);
if(addresses.size() > 0) {
    double latitude = addresses.get(0).getLatitude();
    double longitude = addresses.get(0).getLongitude();
}
Chau
  • 5,540
  • 9
  • 65
  • 95
Satyam
  • 1,672
  • 3
  • 20
  • 34
0

Here is a complete sample

First define a zoom

    final CameraUpdate zoom = CameraUpdateFactory.zoomTo(5);

Clear any map markers

    mMap.clear();
    //Declare a new marker
    final MarkerOptions mp = new MarkerOptions();
    //declare a EditText to get the user informed address
    EditText etEndereco = findViewById(R.id.map_prof_etEnd);

    Geocoder geocoder = new Geocoder(map_prof.this);
    List<Address> addresses = null;
    try {
        addresses = geocoder.getFromLocationName(etEndereco.getText().toString(), 1);
    } catch (IOException e) {
        e.printStackTrace();
    }
    if (addresses.size() > 0) {
        double latitude = addresses.get(0).getLatitude();
        double longitude = addresses.get(0).getLongitude();

        mp.position(new LatLng(latitude, longitude));

        Log.e("teste map2", "inserted latitude " + Latitude + ", inserted Longitude " + Longitude);

        mp.title("Selected location");
        CameraUpdate center = CameraUpdateFactory.newLatLng(new LatLng(latitude, longitude));
        mMap.clear();
        mMap.addMarker(mp);
        mMap.moveCamera(center);
        mMap.animateCamera(zoom);
    }
Thiago Silva
  • 670
  • 6
  • 18