Reputation: 337
I follow this question (How can I find the latitude and longitude from address?) and try to get the latitude and longitude of an address. I call the method like this "GeoPoint point=getLocationFromAddress("colombo,sri lanka");" . but give null value. how to call method correctly??
public GeoPoint getLocationFromAddress(String strAddress){
Geocoder coder = new Geocoder(this);
List<Address> address;
GeoPoint p1 = null;
try {
address = coder.getFromLocationName(strAddress,5);
if (address==null) {
return null;
}
Address location=address.get(0);
location.getLatitude();
location.getLongitude();
p1 = new GeoPoint((int) (location.getLatitude() * 1E6),
(int) (location.getLongitude() * 1E6));
return p1;
}
}
Upvotes: 2
Views: 264
Reputation: 31
You can use the following method that take a Location object that contain latitud and longitud coordinates and Log the location to the LogCat console:
public void setLocation(Location loc) {
//Get Human readable address from latitud and longitud coordinates
if (loc.getLatitude() != 0.0 && loc.getLongitude() != 0.0) {
try {
Geocoder geocoder = new Geocoder(this, Locale.getDefault());
List<Address> list = geocoder.getFromLocation(loc.getLatitude(), loc.getLongitude(), 1);
if (!list.isEmpty()) {
Address address = list.get(0);
Log.d(CLASS_TAG, "Mi direcci—n es: \n" + address.getAddressLine(0));
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
Upvotes: 1
Reputation: 158
In my opinion you should use google place autocomplete/place picker api's. Select the place and thereafter you can get Latitude and Longitude. Check this out: https://developers.google.com/places/android-api/autocomplete
Upvotes: 1
Reputation: 1477
try below code
Geocoder gcd = new Geocoder(ReserveTimer.this, Locale.getDefault());
List<Address> addresses = gcd.getFromLocation(Double.parseDouble(lati), Double.parseDouble(longi), 1);
if (addresses.size() > 0) {
System.out.println(addresses.get(0).getLocality());
System.out.println(addresses.get(0).getCountryName());
System.out.println(addresses.get(0).getAddressLine(0));
System.out.println(addresses.get(0).getAdminArea());
System.out.println(addresses.get(0).getCountryName());
System.out.println(addresses.get(0).getPostalCode());
}
Upvotes: 1