5

Possible Duplicate:
How can I find the latitude and longitude from address?

I want to get latitude and longitude of particular address . How can i do this ?

Community
  • 1
  • 1
diordna
  • 149
  • 3
  • 10

3 Answers3

4
private void getFromLocation(String address)
    {
          double latitude= 0.0, longtitude= 0.0;

        Geocoder geoCoder = new Geocoder(this, Locale.getDefault());    
        try 
        {
            List<Address> addresses = geoCoder.getFromLocationName(address , 1);
            if (addresses.size() > 0) 
            {            
                GeoPoint p = new GeoPoint(
                        (int) (addresses.get(0).getLatitude() * 1E6), 
                        (int) (addresses.get(0).getLongitude() * 1E6));

                latitude=p.getLatitudeE6()/1E6;
                longtitude=p.getLongitudeE6()/1E6;


                }
        }
        catch(Exception ee)
        {

        }
    }
}
Amol Sawant
  • 13,842
  • 2
  • 20
  • 27
Kamal
  • 1,415
  • 13
  • 24
2

This will give you (loop through) all the matches of the address string. If you just desire the first match, make sure address.size() is great than 0, then take address.get(0);

In my usage, I get all matches, and show them as options. The user then clicks one, and it selects that GPS location.

Geocoder geocoder = new Geocoder(getBaseContext());  
List<Address> addresses;
try {
   addresses = geocoder.getFromLocationName("Example StreeT, UK, DNFE", 20);

   for(int i = 0; i < addresses.size(); i++) { // MULTIPLE MATCHES

     Address addr = addresses.get(i);

     double latitude = addr.getLatitude();
     double longitude = addr.getLongitude(); // DO SOMETHING WITH VALUES

   }

}
IAmGroot
  • 13,760
  • 18
  • 84
  • 154
2

You can get from the below code,

private void GetLatitudeAndLongitude() {
    geocoder = new Geocoder(mContext, Locale.getDefault());
    try {
        List<Address> addresses = geocoder.getFromLocationName(txtLocation.getText().toString().trim().toLowerCase(), 1);
        if (addresses.size() > 0) {
            homeInfoModel.setLalitude(String.valueOf(addresses.get(0).getLatitude()));
            homeInfoModel.setLongitude(String.valueOf(addresses.get(0).getLongitude()));
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}
user
  • 323
  • 6
  • 11