18 Maps
18 Maps
Based Services
Contents
● Add Google Map to Android application
● Google Map methods
● Access phone’s position
Add Google Map to Android application
● Add Google Play Services to project
● Add MapFragment
● Get an API key
● Add permission to AndroidManifest.xml
● Wait for map to be ready
Step 1: Add Google Play Services to project
● add Google Play to project in app's build.gradle file
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar’])
implementation 'com.google.android.gms:play-
services:18.0.0’
}
2. Add MapFragment
Step 2: Add MapFragment
● Google Maps API provides a fragment class named MapFragment for
displaying a map within an activity.
Link: https://code.google.com/apis/console/
○ Enable API
○ choose Android Key
○ paste in the SHA-1 key you got from the previous slide
Step 3: Get an API key
● After create project
And enable API
Step 3: Get an API key
● Cancel billing and select
“Credentails” from menu
Step 3: Get an API key
● Create API key
Step 3: Get an API key
● Key generated
<manifest ...>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission
android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission
android:name="android.permission.ACCESS_FINE_LOCATION" />
<application ...>
...
</application>
</manifest>
Step 4. AndroidManifest.xml add permissions
● ACCESS_COARSE_LOCATION Permission:
Purpose: allows an app to access approximate location based on network sources
such as cell towers and Wi-Fi. The location obtained is less precise than using GPS
but consumes less power.
Typical Usage: Apps that require location services but do not need pinpoint
accuracy may request this permission. Examples include weather only need to know
which city or area you are in rather than your exact position.
● ACCESS_FINE_LOCATION Permission:
Purpose: allows an app to access precise location data, typically from GPS, but it
can also include data from Wi-Fi and mobile networks to provide a highly accurate
location.
Typical Usage: Apps that require precise location tracking, such as navigation apps
(e.g., Google Maps) or apps with geotagging features.
Step 5: Wait for map to be ready
GoogleMap methods
● placing items on the map:
○ addCircle, addGroundOverlay, addMarker, addPolygon, addPolyline,
addTileOverlay
○ clear - Removes all markers, polylines/polygons, overlays
● manipulating the camera:
○ getCameraPosition, moveCamera, animateCamera, stopAnimation
● map settings and appearance:
○ setBuildingsEnabled, setIndoorEnabled, setMapType, setPadding,
setTrafficEnabled
GoogleMap methods
● snapshot - take a screen shot of the map as a bitmap
● event listeners:
○ setOnCameraChangeListener, setOnMapClickListener,
setOnMapLoadedCallback, setOnMapLongClickListener,
setOnMarkerClickListener, setOnMarkerDragListener,
setOnMyLocationChangeListener
The map's camera
● The current viewing window of a map's camera is defined by:
○ target location (latitude/longitude), zoom (2.0 - 21.0),
○ bearing (orientation/rotation), and tilt (degrees)
Set camera in XML
● Set initial map settings and camera position in the layout XML:
<fragment ...
android:name="com.google.android.gms.maps.MapFragment"
android:id="@+id/ID"
map:cameraBearing="112.5"
map:cameraTargetLat="-33.796923"
map:cameraTargetLng="150.922433"
map:cameraTilt="30"
map:cameraZoom="13"
map:mapType="normal"
map:uiCompass="false"
map:uiRotateGestures="true"
map:uiScrollGestures="false"
map:uiTiltGestures="true"
map:uiZoomControls="false"
map:uiZoomGestures="true" />
Set camera in Java code
● CameraUpdateFactory methods:
○ newLatLng(new LatLng(lat, lng))
○ newLatLngBounds(new LatLngBounds(SW, NE), padding)
○ newLatLngZoom(new LatLng(lat, lng), zoom)
○ newCameraPosition(CameraPosition)
○ others:
LatLngBounds bounds = new LatLngBounds(
new LatLng(20, -130.0), // SW
new LatLng(55, -70.0)); // NE
map.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds, 50));
Placing markers
● A GoogleMap object has an addMarker method that
can let you put "push pin" markers at locations on
the map.
○ The marker's methods return the marker, so you can chain
them.
○ options: alpha, draggable, icon, position, rotation, title,
visible, ...
map.addMarker(new MarkerOptions()
.position(new LatLng(40.801, -96.691))
.title("Lincoln, NE") );
Marker mark = map.addMarker(new MarkerOptions() ...);
mark.remove();
Lines and paths
● A GoogleMap object has an addPolyline method that can let you
put lines between locations on the map.
○ options: color, visible, width, zIndex, ...
map.addPolyline(new PolylineOptions()
.add(new LatLng(40.801, -96.691)) // Lincoln, NE
.add(new LatLng(34.020, -118.412)) // Los Angeles, CA
.add(new LatLng(40.703, -73.980)) // New York, NY
);
Polyline polly = map.addPolyline(...); polly.remove();
Accessing phone's location
● Android LocationManager gives you the phone's position:
○ GPS provider provides highest accuracy
○ Network provider is a fallback in case GPS is disabled / not present
Location update events
● Track user's movement by listening for location update events
Classwork