34
Location-Based Services: Part 2 (Google Maps)

Location-Based Services: Part 2 (Google Maps). Accessing Google Maps Google maps can be accessed in two ways Through a browser or a WebView Through the

Embed Size (px)

Citation preview

Page 1: Location-Based Services: Part 2 (Google Maps). Accessing Google Maps Google maps can be accessed in two ways Through a browser or a WebView Through the

Location-Based Services: Part 2(Google Maps)

Page 2: Location-Based Services: Part 2 (Google Maps). Accessing Google Maps Google maps can be accessed in two ways Through a browser or a WebView Through the

©SoftMoore Consulting

Accessing Google Maps

Google maps can be accessed in two ways

• Through a browser or a WebView

• Through the Google Maps Android API v2

• Google Maps Android API v2– allows you to incorporate Google Maps into applications– is distributed as part of the Google Play Services SDK– encapsulates maps in a MapFragment or a

SupportMapFragment

Slide 2

MapFragment and SupportMapFragment essentiallyreplace the MapActivity class used in version 1.

Page 3: Location-Based Services: Part 2 (Google Maps). Accessing Google Maps Google maps can be accessed in two ways Through a browser or a WebView Through the

©SoftMoore Consulting

Google Maps Android API v2

Using Google Maps Android API v2, you can

• Add maps to your app– 3D maps − terrain maps– satellite maps − etc.

• Customize the map– markers − image overlays– polylines/polygons − etc.

• Control the user’s view– zoom − pan– rotate − etc.

Slide 3

Page 4: Location-Based Services: Part 2 (Google Maps). Accessing Google Maps Google maps can be accessed in two ways Through a browser or a WebView Through the

©SoftMoore Consulting

Attribution Requirements

• If you use the Google Maps Android API in your application, you must include the Google Play Services attribution text as part of a “Legal Notices” section in your application. Including legal notices as an independent menu item, or as part of an “About” menu item, is recommended.

• The attribution text is available by making a call to methodGooglePlayServicesUtil.getOpenSourceSoftwareLicenseInfo()

Slide 4

Page 5: Location-Based Services: Part 2 (Google Maps). Accessing Google Maps Google maps can be accessed in two ways Through a browser or a WebView Through the

©SoftMoore Consulting

Getting Started

• Download and configure the Google Play services SDK, which includes the Google Maps Android API.

• Obtain a Google Maps API key.– register a project in the Google APIs Console– get a Google Maps key associated with the signing certificate

and package for your app

• Add the required settings in your application’s manifest.

• Add a map to your application.

• Test your application

Slide 5

Android Studio helps simplify the middle three steps.

Page 6: Location-Based Services: Part 2 (Google Maps). Accessing Google Maps Google maps can be accessed in two ways Through a browser or a WebView Through the

©SoftMoore Consulting

Download Google Play Services(Android SDK Manager)

Slide 6

Page 7: Location-Based Services: Part 2 (Google Maps). Accessing Google Maps Google maps can be accessed in two ways Through a browser or a WebView Through the

©SoftMoore Consulting

Creating a New Applicationwith a Google Maps Activity

• Make sure that the Google Play services SDK is installed, as shown in previous slide.

• Using Android Studio, start with a Google Maps Activity when creating a new project or add a Google Maps Activity to an existing project.

• The resource google_maps_api.xml contains the information and steps needed to obtain a Google Maps API key.

• The slides at the end of this section provide the details needed to add Google Maps to an application if you do not create a Google Maps Activity in Android Studio.

Slide 7

Page 8: Location-Based Services: Part 2 (Google Maps). Accessing Google Maps Google maps can be accessed in two ways Through a browser or a WebView Through the

©SoftMoore Consulting

Test the Application

Slide 8

Note: By defaultthe Google MapsActivity does notcontain an actionbar.

Page 9: Location-Based Services: Part 2 (Google Maps). Accessing Google Maps Google maps can be accessed in two ways Through a browser or a WebView Through the

©SoftMoore Consulting

Map UI Properties

• Similar to other Android UI properties, most map UI properties can be set using one of two approaches– declarative (in an XML file)– procedural approach (in the Java code).

• As usual, the declarative approach is generally preferred.

Slide 9

Page 10: Location-Based Services: Part 2 (Google Maps). Accessing Google Maps Google maps can be accessed in two ways Through a browser or a WebView Through the

©SoftMoore Consulting

Classes MapFragment and SupportMapFragment

• Classes MapFragment and SupportMapFragment (in package com.google.android.gms.maps) are the simplest ways to place a map in an application.

• Use SupportMapFragment if you need to support Android devices running API 10 and lower.

• These classes wrap a view of a map that automatically handles the necessary life cycle needs.

• Add a MapFragment or a SupportMapFragment to an activity’s layout file.

Slide 10

Page 11: Location-Based Services: Part 2 (Google Maps). Accessing Google Maps Google maps can be accessed in two ways Through a browser or a WebView Through the

©SoftMoore Consulting

Selected XML Attributes for a MapFragment

• mapTypevalues include none, normal, hybrid, satellite, and terrain

• uiZoomControls, uiCompassspecify whether you want the zoom controls and compass to

appear on the map

• uiZoomGestures, uiScrollGestures, etc.specify which gestures are enabled/disabled

• liteModemap that supports a subset of the map functionality

Slide 11

Add these attributes using the following namespace declaration:xmlns:map="http://schemas.android.com/apk/res-auto"

Page 12: Location-Based Services: Part 2 (Google Maps). Accessing Google Maps Google maps can be accessed in two ways Through a browser or a WebView Through the

©SoftMoore Consulting

SupportMapFragment Example

<fragment xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:map="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/map" tools:context=".MapsActivity" android:name="com.google.android.gms.maps.SupportMapFragment" map:uiZoomControls="true" />

Slide 12

Note: This map fragment includes zoom controls.

Page 13: Location-Based Services: Part 2 (Google Maps). Accessing Google Maps Google maps can be accessed in two ways Through a browser or a WebView Through the

©SoftMoore Consulting

Class GoogleMap

• GoogleMap models the map object within an application.

• GoogleMap automatically handles– Connecting to the Google Maps service.– Downloading map tiles.– Displaying tiles on the device screen.– Displaying various controls such as pan and zoom.– Responding to pan and zoom gestures by moving the map and

zooming in or out.

Slide 13

Page 14: Location-Based Services: Part 2 (Google Maps). Accessing Google Maps Google maps can be accessed in two ways Through a browser or a WebView Through the

©SoftMoore Consulting

The OnMapReadyCallback Interface

• The OnMapReadyCallBack interface contains a single abstract method.public void onMapReady(GoogleMap map)

• Implement the OnMapReadyCallback interface to get access to a GoogleMap object.

• Use the GoogleMap object to set map properties.

Slide 14

Page 15: Location-Based Services: Part 2 (Google Maps). Accessing Google Maps Google maps can be accessed in two ways Through a browser or a WebView Through the

©SoftMoore Consulting

Implementing theOnMapReadyCallback Interface

• Import the necessary map classes and interfaces.import com.google.android.gms.maps.*;import com.google.android.gms.maps.model.*;

• Declare that the activity implement the interface.public class MainActivity extends ActionBarActivity implements OnMapReadyCallback

Slide 15

Page 16: Location-Based Services: Part 2 (Google Maps). Accessing Google Maps Google maps can be accessed in two ways Through a browser or a WebView Through the

©SoftMoore Consulting

Implementing theOnMapReadyCallback Interface (continued)

• Use getMapAsync() to set the callback on the map fragment.@Overrideprotected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);

MapFragment mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map); mapFragment.getMapAsync(this); }

Slide 16

Or use class SupportMapFragment andmethod getSupportFragmentManager()

Page 17: Location-Based Services: Part 2 (Google Maps). Accessing Google Maps Google maps can be accessed in two ways Through a browser or a WebView Through the

©SoftMoore Consulting

Implementing theOnMapReadyCallback Interface (continued)

• Implement the onMapReady() method.@Overridepublic void onMapReady(GoogleMap map) { LatLng charleston = new LatLng(32.781, -79.935);

map.setMyLocationEnabled(true); map.moveCamera(CameraUpdateFactory .newLatLngZoom(charleston, 14));

map.addMarker(new MarkerOptions() .title("Charleston, S.C.") .snippet("The most beautiful city in North America.") .position(charleston)); }

Slide 17

Page 18: Location-Based Services: Part 2 (Google Maps). Accessing Google Maps Google maps can be accessed in two ways Through a browser or a WebView Through the

©SoftMoore Consulting

Google Map Example(continued)

Slide 18

Page 19: Location-Based Services: Part 2 (Google Maps). Accessing Google Maps Google maps can be accessed in two ways Through a browser or a WebView Through the

©SoftMoore Consulting

Creating a Virtual Device for Google Maps

• Make sure that you have installed (using the Android SDK manager) a Google APIs system image for the virtual device target.

• Make sure that the virtual device target supports Google APIs; i.e., the phrase “Google APIs” should appear somewhere in the target name.

Slide 19

Page 20: Location-Based Services: Part 2 (Google Maps). Accessing Google Maps Google maps can be accessed in two ways Through a browser or a WebView Through the

©SoftMoore Consulting

Creating a Virtual Device for Google Maps(continued)

Slide 20

Page 21: Location-Based Services: Part 2 (Google Maps). Accessing Google Maps Google maps can be accessed in two ways Through a browser or a WebView Through the

©SoftMoore Consulting

Creating a Virtual Device for Google Maps(continued)

Slide 21

Page 22: Location-Based Services: Part 2 (Google Maps). Accessing Google Maps Google maps can be accessed in two ways Through a browser or a WebView Through the

©SoftMoore Consulting

Adding Google Maps to an Application

Slide 22

The remaining slides in this section provide the detailsneeded to add Google Maps to an application if youdo not create a Google Maps Activity in Android Studio.

Page 23: Location-Based Services: Part 2 (Google Maps). Accessing Google Maps Google maps can be accessed in two ways Through a browser or a WebView Through the

©SoftMoore Consulting

Setting Up Google Play Services(https://developer.android.com/google/play-services/setup.html)

• Make sure that the Google Play services SDK is installed, as shown in previous slide.

• In Android Studio under “Gradle Scripts”, edit the build.gradle file for “Module: app”(not the build.gradle file for the project)

Under dependencies (near the bottom), add the following line at the end:compile 'com.google.android.gms:play-services-maps:6.5.+'

Also add this line if you are combining maps with other location services such as location updates.compile 'com.google.android.gms:play-services-location:6.5.+'

Slide 23

Page 24: Location-Based Services: Part 2 (Google Maps). Accessing Google Maps Google maps can be accessed in two ways Through a browser or a WebView Through the

©SoftMoore Consulting

Setting Up Google Play Services(continued)

• Save the changes and click “Sync Project with Gradle Files” in the toolbar, or click on menu itemTools Android Sync Project with Gradle Files.

• Edit file AndroidManifest.xml and add the following tag as a child of the <application> element:<meta-data android:name="com.google.android.gms.version"       android:value="@integer/google_play_services_version"/>

Slide 24

Note: You can ignore instructions about creating a ProGuardexception if you are building in debug mode (i.e., not release mode).

Page 25: Location-Based Services: Part 2 (Google Maps). Accessing Google Maps Google maps can be accessed in two ways Through a browser or a WebView Through the

©SoftMoore Consulting

Obtaining a Maps API Key

• Before using the Google Maps service, you need to obtain a Google Maps API key.

• If you created a Google Maps Activity using Android Studio, then the resource google_maps_api.xml contains the information and steps needed to obtain a Google Maps API key.

• Otherwise you must– Use your certificate’s SHA1 fingerprint to register with the Maps

service to obtain a Maps API key– Include the Maps API key in MapView elements of the XML

layout files– Sign the application with the same certificate used to obtain the

Maps API key

Slide 25

Page 26: Location-Based Services: Part 2 (Google Maps). Accessing Google Maps Google maps can be accessed in two ways Through a browser or a WebView Through the

©SoftMoore Consulting

The SDK Debug Key

• Recall that during installation, the Android SDK tools create a debug keystore and key in <ANDROID_SDK_HOME>\.android with predetermined names/passwords:– Keystore name: “debug.keystore”– Keystore password: “android”– Key alias: “androiddebugkey”– Key password: “android”

• At each compilation, the SDK tools use this debug key to sign the .apk file.

Slide 26

Page 27: Location-Based Services: Part 2 (Google Maps). Accessing Google Maps Google maps can be accessed in two ways Through a browser or a WebView Through the

©SoftMoore Consulting

Obtaining the Debug Key’s SHA1 Fingerprint

• Use Java’s keytool “- list” option to obtain the certificate’s SHA1 fingerprint.

• ExampleC:\Java\Android\sdk\android-sdk\.android>keytool –list -keystore debug.keystoreEnter keystore password:

Keystore type: JKSKeystore provider: SUN

Your keystore contains 1 entry

androiddebugkey, Dec 14, 2014, PrivateKeyEntry,Certificate fingerprint (SHA1): 37:97:13:32:E7:57:08:56:9A:E7:51:CB:43:B3:...

Slide 27

response

all onone line

(enter “android”)

Page 28: Location-Based Services: Part 2 (Google Maps). Accessing Google Maps Google maps can be accessed in two ways Through a browser or a WebView Through the

©SoftMoore Consulting

Register with the Google MapsService to Obtain a Maps API Key

• To get a Google Maps API key, go tohttps://developers.google.com/maps/documentation/

android/start

• Scroll to “Create an API project in the Google APIs Console” and follow instructions to register your project for the Maps API.

• Then scroll to “Obtain a Google Maps API key” and follow the instructions to obtain an API key. You will need to enter the certificate’s fingerprint and one or more package names; e.g.,37:97:13:32:E7:57:08:56:9A:E7:...;edu.citadel.android.maps

Slide 28

Page 29: Location-Based Services: Part 2 (Google Maps). Accessing Google Maps Google maps can be accessed in two ways Through a browser or a WebView Through the

©SoftMoore Consulting

Register with the Google MapsService to Obtain a Maps API Key (continued)

• When submitted, you will get back a page with an API key that can be used for apps signed with your certificate: following information; e.g., something like AIzaSyBI6yPiqwvpUH8_X7w_HNE2yiHhpDuUD58

Slide 29

Page 30: Location-Based Services: Part 2 (Google Maps). Accessing Google Maps Google maps can be accessed in two ways Through a browser or a WebView Through the

©SoftMoore Consulting

Configure the Application’s Manifest

• Add the following element as a child of the <application> element:<meta-data android:name="com.google.android.maps.v2.API_KEY"

android:value="AIzaSyBI6yPiqwvpUH8_X7w_HNE2yiHhpDuUD58"/>

• Specify permissions; e.g.,android.permission.INTERNETandroid.permission.ACCESS_NETWORK_STATEandroid.permission.WRITE_EXTERNAL_STORAGEandroid.permission.ACCESS_FINE_LOCATION

Slide 30

Use your Maps API key here.

(or COARSE_LOCATION)

Page 31: Location-Based Services: Part 2 (Google Maps). Accessing Google Maps Google maps can be accessed in two ways Through a browser or a WebView Through the

©SoftMoore Consulting

Configure the Application’s Manifest(continued)

• Recommended: To prevent Google Play Store from displaying the app on devices that don't support OpenGL ES version 2, add the following element as a child of the <manifest> element:<uses-feature android:glEsVersion="0x00020000" android:required="true"/>

Slide 31

Page 32: Location-Based Services: Part 2 (Google Maps). Accessing Google Maps Google maps can be accessed in two ways Through a browser or a WebView Through the

©SoftMoore Consulting

Create Simple Test App

Create a basic new project in Android Studio, and replace the layout file (activity-main.xml) with the following:<?xml version="1.0" encoding="utf-8"?><fragment xmlns:android="http://schemas.android.com/apk/res/android" xmlns:map="http://schemas.android.com/apk/res-auto" android:id="@+id/map" android:layout_width="match_parent" android:layout_height="match_parent" android:name="com.google.android.gms.maps.MapFragment" />

Slide 32

Page 33: Location-Based Services: Part 2 (Google Maps). Accessing Google Maps Google maps can be accessed in two ways Through a browser or a WebView Through the

©SoftMoore Consulting

Test the Simple App

Slide 33

Page 34: Location-Based Services: Part 2 (Google Maps). Accessing Google Maps Google maps can be accessed in two ways Through a browser or a WebView Through the

©SoftMoore Consulting

Relevant Links

• Google Maps Android API v2https://developers.google.com/maps/documentation/android/

• Setting Up Google Play Serviceshttps://developer.android.com/google/play-services/setup.html

• Map Objectshttps://developers.google.com/maps/documentation/android/map

• Signing Your Applicationshttp://developer.android.com/tools/publishing/app-signing.html

• Google Maps Sample Project <android-sdk>/extras/google/google_play_services/samples/maps

Slide 34