In the first part of this tutorial series, we’ll show how to integrate Google maps in your Android location. Then in the next part, we’ll show how to automate the process of obtaining the current location of user’s device.

Let’s Get Started

After creating a new project, create Key by going to Credential-> Create credentials-> APIKEY.

Now you can add restrictions if you needed.

Note: If you want to make it project independent, then no need to add package name and SHA1 key.

Now enable Google Map Android API Service (If not enabled)

Go to Dashboard -> Enable API -> Google Map Android API.

Once you’ve done all the settings in the developer console, open your Android Studio to develop the demo.

Firstly, create a new project.

Now to automatically add the map in the new activity, right click on the package -> New -> Google map activity.

Next, Setup your key through main -> res -> googlemapapi.xml

After setting up the above key, go to Android manifest file and add the shown metadata if not added.

Also, don’t forget to give the necessary permissions.

Next, open Gradle and check whether the following dependency is added or not in build.gradle of project’s main module.

Start Code Integration

MapsActivity.java

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
/**
* Manipulates the map once available.
* This callback is triggered when the map is ready to be used.
* This is where we can add markers or lines, add listeners or move the camera. In this case,
* we just add a marker near Sydney, Australia.
* If Google Play services is not installed on the device, the user will be prompted to install
* it inside the SupportMapFragment. This method will only be triggered once the user has
* installed Google Play services and returned to the app.
*/
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Add a marker in Sydney and move the camera
LatLng sydney = new LatLng(-34, 151);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}
}

However, if you face any problem implementing, contact our developers for help.

While adding location-based features in your Android apps, do remember that users might have location turned off. This means you have to plan for when any user has location turned off. Still, if you need any expert to help you with building your Android app, you can hire Android app developer from us.