Tuesday, March 8, 2016

107-Android Studio 1.5 Google Maps Activity Template

107-Android Studio 1.5 Google Maps Activity Template

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;
   protected void onCreate(Bundle savedInstanceState) {
       // Obtain the SupportMapFragment and get notified when the map is ready to be used.
       SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
    * 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.
   public void onMapReady(GoogleMap googleMap) {
       mMap = googleMap;
       // Add a marker in Sydney and move the camera
       LatLng sydney = new LatLng(-34151);
       mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));

2.2) res/layout/activity_maps.xml

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
   tools:context="com.notarazi.mymap1.MapsActivity" />

2.3) res/values/google_maps_api.xml

   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:
   You can also add your credentials to an existing key, using this line:
   Alternatively, follow the directions here:
   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>
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



No comments:

Post a Comment