Presentation is loading. Please wait.

Presentation is loading. Please wait.

Location-Based Services: Part 2 (Google Maps)

Similar presentations


Presentation on theme: "Location-Based Services: Part 2 (Google Maps)"— Presentation transcript:

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

2 Accessing Google Maps Google maps can be accessed in two ways
Through a browser or a WebView Through the Google Maps Android API (a.k.a., Google Maps Android API v2) Google Maps Android API 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 MapFragment and SupportMapFragment essentially replace the MapActivity class used in an earlier version of Google Maps API. ©SoftMoore Consulting

3 Google Maps Android API
Using Google Maps Android API, 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. ©SoftMoore Consulting

4 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 method GooglePlayServicesUtil.getOpenSourceSoftwareLicenseInfo() ©SoftMoore Consulting

5 Download Google Repository (Android SDK Manager)
©SoftMoore Consulting

6 Getting Started Make sure that Google Repository is installed, as shown in the previous slide. 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 Android Studio greatly simplifies the middle three steps. ©SoftMoore Consulting

7 Creating a New Application with a Google Maps Activity
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. ©SoftMoore Consulting

8 Example: google_maps_api.xml (in src/debug/res/values)
<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: You can also add your credentials to an existing key, using these values: ... SHA-1 certificate fingerprint: C9:95:96:58:E5:7C:58:32:FC:B8:0F:2D:9F:CD:5C:CF:94:BC:54:E4 (continued on next slide) ©SoftMoore Consulting

9 Example: google_maps_api.xml (in src/debug/res/values)
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> </resources> A sample key looks like “AIzaSyCNs7mQEGMgQG…” (39 characters) ©SoftMoore Consulting

10 Restrict the Key’s Use to Android Apps
To improve your application’s security, you should restrict the key’s usage to “Android apps” in the API Console. A link to the API Console is provided when you obtain a key. To restrict the keys usage you will need to provide the package name and the SHA-1 signing certificate fingerprint from your keystore. The SHA-1 certificate fingerprint is displayed in the file google_maps_api.xml. Alternatively, the SHA-1 certificate fingerprint can be obtained by running the following command on the keystore: keytool -list -v -keystore debug.keystore debug.keystore is the name of the default keystore, usually in directory C:\Users\username\.android (password is android) ©SoftMoore Consulting

11 Test the Application Note: By default the Google Maps
Activity does not contain an app bar. ©SoftMoore Consulting

12 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. ©SoftMoore Consulting

13 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. (This is done for you if you create a Google maps activity in Android Studio.) ©SoftMoore Consulting

14 Selected XML Attributes for a MapFragment
mapType values include none, normal, hybrid, satellite, and terrain uiZoomControls, uiCompass specify whether you want the zoom controls and compass to appear on the map uiZoomGestures, uiScrollGestures, etc. specify which gestures are enabled/disabled liteMode map that supports a subset of the map functionality Add these attributes using the following namespace declaration: xmlns:map=" ©SoftMoore Consulting

15 SupportMapFragment Example
<fragment xmlns:android=" xmlns:map=" xmlns:tools=" android:name="com.google.android.gms.maps.SupportMapFragment" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="edu.citadel.android.googlemaps.MapsActivity" map:uiCompass="true" map:uiZoomControls="true"/> Note: This map fragment includes zoom controls and a compass. ©SoftMoore Consulting

16 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. ©SoftMoore Consulting

17 The OnMapReadyCallback Interface
The OnMapReadyCallBack interface contains a single abstract method. public void onMapReady(GoogleMap googleMap) Implement the OnMapReadyCallback interface to get access to a GoogleMap object. Use the GoogleMap object to set map properties. ©SoftMoore Consulting

18 Implementing the OnMapReadyCallback 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 AppCompatActivity implements OnMapReadyCallback { // set to true if permission is granted private boolean isAccessLocationGranted = false; private GoogleMap mMap; ©SoftMoore Consulting

19 Implementing the OnMapReadyCallback Interface (continued)
Use getMapAsync() to set the callback on the map fragment. @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); MapFragment mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map); mapFragment.getMapAsync(this); checkAccessLocationPermission(); } Or use class SupportMapFragment and method getSupportFragmentManager() ©SoftMoore Consulting

20 Implementing the OnMapReadyCallback Interface (continued)
Implement the onMapReady() method. @Override public void onMapReady(GoogleMap googleMap) { mMap = googleMap; // Add a marker in Charleston and move the camera LatLng charleston = new LatLng(32.781, ); mMap.moveCamera(CameraUpdateFactory.newLatLngZoom (charleston, 14)); if (isAccessLocationGranted) try mMap.setMyLocationEnabled(true); } (continued on next slide) ©SoftMoore Consulting

21 Implementing the OnMapReadyCallback Interface (continued)
catch (SecurityException ex) { String errorMsg = getString(R.string.no_permission); Log.e(LOG_TAG, errorMsg, ex); } mMap.addMarker(new MarkerOptions() .title("Charleston, S.C.") .snippet("The most beautiful city in North America.") .position(charleston)); ©SoftMoore Consulting

22 Requesting User Permissions
In order to call map.setMyLocationEnabled(true), user permission to access the user’s location must be declared in the Android manifest file and requested at runtime. Example <manifest ... >   <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>     ... </manifest> ©SoftMoore Consulting

23 Google Map Example (continued)
Moves map to current location. Using two fingers to rotate the map will enable the compass. ©SoftMoore Consulting

24 Creating a Virtual Device for Google Maps
Make sure that Google Play Services and the Google Repository are installed as described earlier Make sure that the virtual device target supports Google APIs; i.e., the phrase “Google APIs” should appear somewhere in the target name. ©SoftMoore Consulting

25 Creating a Virtual Device for Google Maps (continued)
©SoftMoore Consulting

26 Relevant Links Getting Started Google Maps Android API
Google Maps Android API Set Up Google Play Services Map Objects Sign Your App Google Maps Sample Project <android-sdk>/extras/google/google_play_services/samples/maps ©SoftMoore Consulting


Download ppt "Location-Based Services: Part 2 (Google Maps)"

Similar presentations


Ads by Google