Google Maps activity in Android Apps

This tutorial consists of;

  • Step 1: In Android Studio ( am using 2.2 ), create a new project with Google Maps activity in it.
  • Step 2: The Java code to add.
  • Step 3: Handling the MULTIDEX issue.

Incase you’d like to develop an Android apps with the Google Maps activity inside, first you need to acquire the Google Maps API key from the Google API developer console.

Step 1: In Android Studio ( am using 2.2 ), create a new project with Google Maps activity in it.

new projectnew-phone-tablet

Choose the Google Maps Activity

new-maps-activity

You will be displayed with a link in the google_maps_api.xml file. Copy (Ctrl+C) the link till the end of line

machine-sha-1-to-google-api-console

Paste (Ctrl+V) on a browser and hit Enter. You will be asked to login to your Google account.

copy-sha1-to-browser

Hit the button Create API key.

Maps-API-enabled-and-generate-API-key

Copy the key generated by the Google API developer console. The code starts with Alza…

copy-maps-API-key

And back to the Android Studio, paste the API key in the location specified (still in the google_maps_api.xml file).

paste-Maps-API-key-in-Ansroid-Studio-project (1)

 

We’ll discuss later what codes to add to complete the project.

Step 2: The Java code to add.

Open the Java file which has the Maps Activitity, and add the following codes, copy the code after the image.

code-to-display-maps-lat-long

public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;

// Add a marker in KUIS, Bandar Seri Putra and move the camera
// KUIS 2.8905° N, 101.7885° E
LatLng bsp = new LatLng(2.8905, 101.7885);
mMap.addMarker(new MarkerOptions().position(bsp).title(location));

CameraPosition cameraPosition = new CameraPosition.Builder()
.target(bsp)
.zoom(20)
.bearing(70)
.tilt(25)
.build();
mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));

}//end onMapReady

Step 3: Handling the MULTIDEX issue.

multidex-error-message

In AndroidManifest.xml

android:name="android.support.multidex.MultiDexApplication"
android-manifest-enable-multidex

In build.gradle add these two lines;

multiDexEnabled true //// ADD Enabling multidex support

compile 'com.android.support:multidex:1.0.0' // ADD Enabling multidex supportbuild.gradle-sync-now
And hit Sync Now

After finishing the Gradle sync, run and test in a real device. Emulator will not display the maps with the default setting.

Need face-to-face lesson?, we also provide Android Studio training, 0129034614
http://bit.ly/androidjsk
android-kokuis-banner2

This tutorial is developing, bookmark or like below to follow…
SHARE to your friends...

Popular Posts

WorldConferences.net