2. SESSION CONTENT
• Downloading Google Play Services
• Google Maps API key
• Placing a Marker
• Changing Marker Color
• Custom Marker Icon
• Moving camera to location with animation
• Changing Map Type
• Current Location
• Zooming Buttons and Functionality
• Compass Functionality
• My Location Button
• Map Rotate Gesture
facebook.com/ahmed.e.hassan
2
3. INTRODUCTION
facebook.com/ahmed.e.hassan
3
Google Maps v1 not working anymore, welcome V2.
Before starting a new project, we need to go through some pre required steps.
• Downloading Google Play Services
• Getting the Google Maps API key
5. DOWNLOADING GOOGLE
PLAY SERVICES
facebook.com/ahmed.e.hassan
5
Add google maps dependencies to your project:
Open your Project using ‘Android Studio’ -> Gradle Scripts -> build.gradle
(Module:app): and Add the Following:
http://gradleplease.appspot.com/
To Know the latest from Google Play Services:
6. facebook.com/ahmed.e.hassan
6
GOOGLE MAPS API KEY
we need to generate SHA-1 fingerprint using java keytool. Open your
terminal and execute the following command to generate SHA-1 fingerprint.
On Windows
keytool -list -v -keystore "%USERPROFILE%.androiddebug.keystore" -alias
androiddebugkey -storepass android -keypass android
On Linux or Mac OS
keytool -list -v -keystore ~/.android/debug.keystore -alias androiddebugkey -storepass
android -keypass android
11. facebook.com/ahmed.e.hassan
11
PLACING MARKER
You can place a marker on the map by using following code.
// latitude and longitude
double latitude = ;
double longitude = ;
// create marker
MarkerOptions marker = new MarkerOptions().position(new LatLng(latitude, longitude)).title("Hello Maps
");
// adding marker
googleMap.addMarker(marker);
12. facebook.com/ahmed.e.hassan
12
CHANGING MARKER
COLOR
By default map marker color will be RED. Google maps provides some set of
predefined colored icons for the marker.
// ROSE color icon
marker.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ROSE));
// GREEN color icon
marker.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN))
;
14. facebook.com/ahmed.e.hassan
14
CUSTOM MARKER ICON
Apart from maps native marker icons, you can use own image to show as a marker.
You can load the icon from any kind of supported sources.
fromAsset(String assetName) – Loading from assets folder
fromBitmap (Bitmap image) – Loading bitmap image
fromFile (String path) – Loading from file
fromResource (int resourceId) – Loading from drawable resource
// Changing marker icon
marker.icon(BitmapDescriptorFactory.fromResource(R.drawable.my_marker_icon)));
15. facebook.com/ahmed.e.hassan
15
MOVING CAMERA TO LOCATION
WITH ANIMATION
You may want to move camera to a particular position. Google maps provides
set of functions to achieve this.
CameraPosition cameraPosition = new CameraPosition.Builder().target(
new LatLng(17.385044, 78.486671)).zoom(12).build();
googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
18. facebook.com/ahmed.e.hassan
18
SHOWING CURRENT LOCATION
You can show user’s current location on the map by calling
setMyLocationEnabled(). Pass true / false to enable or disable this feature
googleMap.setMyLocationEnabled(true); // false to disable
21. facebook.com/ahmed.e.hassan
21
MY LOCATION BUTTON
My location button will be used to move map to your current location. This button
can be shown / hidden by calling setMyLocationButtonEnabled() function
googleMap.getUiSettings().setMyLocationButtonEnabled(true);