Android studio Get Google Maps street name

Reading Time: 1 minute

This article is continuation of previous first Google maps activity. In there, we were put pointer on the map in the position where given city is located. In here, we will obtain user current position, and in Android studio get Google maps street name. We can add one button and then write following code that will obtain street name of your current position.

Geocoder geocoder = new Geocoder(this, Locale.getDefault());

In order to find position, we need to use visibleRegion of VisibleRegion class.

VisibleRegion visibleRegion = mMap.getProjection().getVisibleRegion();
List<Address> addressList = null;

Then we can use four variables to obtain longitude and latitude of visible region.

double left = visibleRegion.latLngBounds.southwest.longitude;
double right = visibleRegion.latLngBounds.northeast.longitude;
double top = visibleRegion.latLngBounds.northeast.latitude;
double bottom = visibleRegion.latLngBounds.southwest.latitude;

We obtain longitude and latitude as averaged value of left and right for longitude and top and bottom value for latitude respectively.

double longitude, latitude;

longitude = (left+right)/2.0;
latitude = (top+bottom)/2.0;

Let’s convert this numerical values to sting in order to supply gocoder object with user current location.

String str_longitude, str_latitude;

str_longitude = String.valueOf(longitude);
str_latitude = String.valueOf(latitude);

String str_address = “”;
try
{

addressList = geocoder.getFromLocation(latitude ,longitude, 1);

}
catch (IOException e)
{

e.printStackTrace();

}

Then, we use one object of Address class.

Address address = addressList.get(0);

Now, just read street name with .getAddressLine(0);

String str_street_name = address.getAddressLine(0);

We can display street name.

txtStreetName.setText(str_street_name);

Tagged with: ,

Leave a Reply

Your email address will not be published. Required fields are marked *

*