Reputation: 21
Following is my code..
fetchlastlocation() is a method which is called when gps icon(imageView) is clicked. This code works but I want to get/fetch the street address and attach it to marker title. What should I do to get this result?
private void fetchlastlocation() {
if(ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(this,new String[]
{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_CODE);
return;
}
Task<Location> task=fusedLocationProviderClient.getLastLocation();
task.addOnSuccessListener(new OnSuccessListener<Location>() {
@Override
public void onSuccess(Location location1) {
if(location1!=null){
currentlocation=location1;
LatLng latLng=new LatLng(currentlocation.getLatitude(),currentlocation.getLongitude());
MarkerOptions markerOptions=new MarkerOptions().position(latLng);
nMap.addMarker(new MarkerOptions().position(latLng).draggable(true));
nMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng,50));
}
}
});
}
Upvotes: 0
Views: 57
Reputation: 1195
Use Geocoder to get the address from latlong
public static String getCountryName(Context context, double latitude, double longitude) {
Geocoder geocoder = new Geocoder(context, Locale.getDefault());
List<Address> addresses = null;
try {
addresses = geocoder.getFromLocation(latitude, longitude, 1);
Address result;
if (addresses != null && !addresses.isEmpty()) {
return addresses.get(0).getAddressLine(0);
// String city = addresses[0].locality;
// String state = addresses[0].adminArea;
// String country = addresses[0].countryName;
// String postalCode = addresses[0].postalCode;
// String knownName = addresses[0].featureName;
}
return null;
} catch (IOException ignored) {
//do something
}
}
Upvotes: 1
Reputation: 812
What You want to do is called reverse Geocoding. check docs
Since you already have your geographic coordinates (lattitude and longitude) use them with the Geocoder class to lookup a human readable address that you want
Upvotes: 0