Mobile Programming Google Maps API Google Maps API
Mobile Programming Google Maps API
Google Maps API lets you include Google maps in your app 2
Customize maps in your app: Controls Styles Markers and more 3
Customization examples Change the color of the sea Put Android on your map Draw shapes on your map 4
ADDING MAP TO YOUR APP
Adding a map to your app Obtain API keys to use Google Map Add Permissions Include a Google Map in your app Change the look and feel of the map Change map behavior 6
API Setup
Register app and get API key To use the Maps SDK for Android, you must get an API key which you can then add to your mobile app, website, or web server. The API key is used to track API requests associated with your project for usage and billing. You can use the Google Cloud Platform Console to manage projects, get API keys, and add API key restrictions. All you have to do is : Register your app in Google Cloud Platform Console Get debug certificate and release certificate and request API key
Steps to get API Debug Key 1. Go to the Google Cloud Platform Console. 2. From the Project drop-down menu, create a new project. 3. From the Navigation menu, select APIs & Services > Credentials. 4. On the Credentials page, click Create credentials > API key. 5. The API key created dialog displays your newly created API key. 6. On the dialog, click Restrict Key. 7. On the API key page, under Key restrictions, set the Application restrictions. 8. Select Android apps and follow the instructions. 9. Click + Add package name and fingerprint. 10. Enter your package name and SHA-1 certificate fingerprint. For example:
Create and Get SHA-1 Fingerprint The Android API key restriction is based on a short form of your app's digital certificate, known as its SHA-1 fingerprint. The debug key for signing your application can be found in the userhome/. android/debug. keystore file. To create the SHA-1 for your debug keystore go to cmd and use the keytool command from your JDK installation. keytool -list -v -keystore %USERPROFILE%/. android/debug. keystore -alias androiddebugkey storepass android -keypass android
Add API Key in your Project Copy your API Key Go to the Manifest. xml <? xml version="1. 0" encoding="utf-8"? > <manifest xmlns: android="http: //schemas. android. com/apk/res/android" package="com. pdoffice. labs. maps"> <uses-permission android: name="android. permission. ACCESS_FINE_LOCATION" /> <application …. > <!-The API key for Google Maps-based APIs is defined as a string resource. --> <meta-data android: name="com. google. android. geo. API_KEY" android: value="@string/google_maps_key" /> <activity …. </activity> </application> </manifest>
ADD PERMISSIONS
Add Permissions The ACCESS_COARSE/FINE_LOCATION permissions are not required to use Google Maps Android API, but you must specify either coarse or fine location permissions for the 'My. Location' functionality.
Include Google Map in your App
Display map in layout To add map to layout, use either: Map. View Map. Fragment With Map. View, you must forward all lifecycle methods, so it's easier to use Map. Fragment 15
Map. Fragment places map in fragment container Use Support. Map. Fragment for backward compatibility Use <fragment> tag in layout android: name= "com. google. android. gms. maps. Support. Map. Fragment" 16
In your Layout File <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. Support. Map. Fragment" android: layout_width="match_parent" android: layout_height="match_parent" tools: context="com. vogella. android. maps. Maps. Activity" />
Map. Fragment attributes You can specify additional map attributes map: camera. Target. Lat="-33. 8" map: camera. Target. Lng="151" map: camera. Tilt="30" map: camera. Zoom="13" map: map. Type="normal" and more! 18
on. Map. Ready. Callback Activity that shows maps must implement On. Map. Ready. Callback interface When map is ready, map is passed to on. Map. Ready() get. Map. Async() specifies where to call on. Map. Ready() callback Google. Map represents the map 19
Map. Activity Example public class Maps. Activity extends Fragment. Activity implements On. Map. Ready. Callback { private Google. Map m. Map; public void on. Create(Bundle saved. Instance. State) { super. on. Create(saved. Instance. State); set. Content. View(R. layout. activity_maps); Support. Map. Fragment map. Fragment = (Support. Map. Fragment) get. Support. Fragment. Manager(). find. Fragment. By. Id(R. id. map); map. Fragment. get. Map. Async( this); } @Override public void on. Map. Ready(Google. Map google. Map) { m. Map = google. Map; Toast. make. Text(Maps. Activity. this, "Map Ready", Toast. LENGTH_LONG). show(); m. Map. set. My. Location. Enabled(true); m. Map. get. Ui. Settings(). set. My. Location. Button. Enabled( true); } } 20
on. Map. Ready() callback When map is ready, on. Map. Ready() is called. Initialize the Google. Map in on. Map. Ready() Examples: initialize center of map, add markers, set zoom levels for initial map display 21
on. Map. Ready() - Another Example public void on. Map. Ready(Google. Map google. Map) { // Add a marker in Sydney and move the camera Lat. Lng sydney = new Lat. Lng(-34, 151); google. Map. add. Marker(new Marker. Options(). position(sydney). title("Marker in Sydney")); google. Map. move. Camera( Camera. Update. Factory. new. Lat. Lng(sydney)); } 22
What you can configure Camera position, including location, zoom, bearing, and tilt Map type Whether zoom buttons appear and/or compass appears Gestures to manipulate map Configuration can be done in both code and in XML file
Map types Normal Hybrid Normal: Road map Satellite: Satellite photo Hybrid: Satellite and roads Terrain: Topographic data 24 Terrain
Set map type Call set. Map. Type() with Google. Map type constant Example of setting to "hybrid" type: Google. Map map; // Sets the map type to "hybrid" m. Map. set. Map. Type(Google. Map. MAP_TYPE_HYBRID); 25
Google. Map events Google. Map object has many listeners On. Map. Click. Listener On. Map. Long. Click. Listener On. Marker. Click. Listener On. My. Location. Change. Listener… // and more 26
Lab Task Use Google Maps, add marker with a icon of your choice other than the default one on the current location of the user, the marker should have a title that says “My Current Location” and a subtitle showing the current latitude and longitude coordinates.
Thank You!
- Slides: 28