How to get latitude and longitude from an address on Android?

Possible duplicate:
How to find latitude and longitude from an address?

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

+4
source share
3 answers
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) { } } } 
+4
source

This will give you (skip) all address bar matches. If you just need the first match, make sure the address .size () is different than 0, then take address.get(0);

In my use, I get all matches and show them as parameters. Then the user clicks one and selects the 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 } } 
+1
source

You can get the code below,

 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(); } } 
+1
source

All Articles