Android Google Maps Current Location, Night Mode Features With Examples

In this tutorial, we’ll play around with the Android Google Maps API. Showing the user the current location, lat/lng bounds, start navigation, enabling night mode etc.

Android Google Maps Current Location

Before we start implementing some cool android google maps features in our application, add the Google Maps v2 API key value in the meta-data tag in the AndroidManifest.xml file as mentioned in this tutorial.

Create a new project in Android Studio and select the template as Google Maps Activity.

Note: Google Play Services dependency will be added by default for this template.

Implement Google Play Location Services in your MapsActivity.java class as shown below.

In the above code mMap.setMyLocationEnabled(true); is used to show the user’s current location.

The below image is the output of the application when the above code is run.

android-google-map-advanced-features-output-253x450

The blue dot is our current location. We need to focus the camera on the current location in the map to prevent zooming and scrolling manually.

Change the onConnected() method as;

In the above code 12 is the zoom level set. We can set the minimum and maximum zoom level using mMap.setMinZoomPreference(float v); and mMap.setMaxZoomPreference(float v);.

Android Google Maps Night Mode Project Structure

android-google-maps-advanced-features

To enable night mode in the apps. We need to set the map style in the onMapReady method as;

mMap.setMapStyle(MapStyleOptions.loadRawResourceStyle(this, R.raw.mapstyle_night));

The mapstyle_night.json code is shown below.

Enable traffics in the map by the following code:

mMap.setTrafficEnabled(true);

android-google-maps-night-mode-output

mMap.setLatLngBoundsForCameraTarget(); is used to constrain the lat/lng center bounds of the focal point of the map (the camera target) so that users can only scroll and pan within these bounds.

To implement the above. Let’s take LatLngBounds for a part of city Adelaide for example.
Following is a snippet that’s put inside onMapReady method

Following is the output of the application.

android google maps snap camera target

Android Google Maps – Start Google Navigation

To start navigation app we need to pass the latitude and longitude of the destination in the following way:

The above code can be invoked from an onClickListener linked to a Button/any other widget.

This brings an end to android google maps tutorial. You can download the Android GoogleMapsFeatures Project from the below link.

By admin

Leave a Reply

%d bloggers like this: