Published using Google Docs
107-Android Studio 1.5 Google Maps Activity Template
Updated automatically every 5 minutes

107-Android Studio 1.5 Google Maps Activity Template

1) Create New Project

1.1) Configure New Project

1.2) Select Target Device

1.3) Add Activity

1.4) Customize The Activity

2) Observe the project codes

2.1) MainActivity.java

2.2) res/layout/activity_maps.xml

2.3) res/values/google_maps_api.xml

DOWNLOAD

Continue from Tutorial 106

1) Create New Project

1.1) Configure New Project

Application Name: MyMap1

Company Domain: notarazi.com

Package Name: com.notarazi.mymap1 (auto-generated)

Project Location: C:\Project1\MyMap1\ (auto-generated)

1.2) Select Target Device

1.3) Add Activity

Select Google Maps Activity.

1.4) Customize The Activity

Accept defaults.

2) Observe the project codes

2.1) MainActivity.java

package com.notarazi.mymap1;

import android.support.v4.app.FragmentActivity;

import android.os.Bundle;

import com.google.android.gms.maps.CameraUpdateFactory;

import com.google.android.gms.maps.GoogleMap;

import com.google.android.gms.maps.OnMapReadyCallback;

import com.google.android.gms.maps.SupportMapFragment;

import com.google.android.gms.maps.model.LatLng;

import com.google.android.gms.maps.model.MarkerOptions;

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));

   }

}

2.2) res/layout/activity_maps.xml

<fragment xmlns:android="http://schemas.android.com/apk/res/android"

   xmlns:map="http://schemas.android.com/apk/res-auto"

   xmlns:tools="http://schemas.android.com/tools"

   android:id="@+id/map"

   android:name="com.google.android.gms.maps.SupportMapFragment"

   android:layout_width="match_parent"

   android:layout_height="match_parent"

   tools:context="com.notarazi.mymap1.MapsActivity" />

2.3) res/values/google_maps_api.xml

<resources>

   <!--

   TODO: Before you run your application, you need a Google Maps API key.

   To get one, follow this link, follow the directions and press "Create" at the end:

   https://console.developers.google.com/flows/enableapi?apiid=maps_android_backend&keyType=CLIENT_SIDE_ANDROID&r=9A:6B:FF:29:0A:A4:9C:15:46:B8:F3:04:0E:4A:D7:1F:9F:DA:F4:24%3Bcom.notarazi.mymap1

   You can also add your credentials to an existing key, using this line:

   9A:6B:FF:29:0A:A4:9C:15:46:B8:F3:04:0E:4A:D7:1F:9F:DA:F4:24;com.notarazi.mymap1

   Alternatively, follow the directions here:

   https://developers.google.com/maps/documentation/android/start#get-key

   Once you have your key (it starts with "AIza"), replace the "google_maps_key"

   string in this file.

   -->

   <string name="google_maps_key" templateMergeStrategy="preserve" translatable="false">YOUR_KEY_HERE</string>

</resources>

OUTCOME.

Runtime Error Message:

01-31 16:53:12.837 4964-5360/com.notarazi.mymap1 E/b: Authentication failed on the server.

01-31 16:53:12.837 4964-5360/com.notarazi.mymap1 E/Google Maps Android API: Authorization failure.  Please see https://developers.google.com/maps/documentation/android/start for how to correctly set up the map.

01-31 16:53:12.840 4964-5360/com.notarazi.mymap1 E/Google Maps Android API: In the Google Developer Console (https://console.developers.google.com)

                                                                            Ensure that the "Google Maps Android API v2" is enabled.

                                                                            Ensure that the following Android Key exists:

                                                                                    API Key: YOUR_KEY_HERE

                                                                                    Android Application (<cert_fingerprint>;<package_name>): 9A:6B:FF:29:0A:A4:9C:15:46:B8:F3:04:0E:4A:D7:1F:9F:DA:F4:24;com.notarazi.mymap1

DOWNLOAD

106-MyMap1.zip