Lecture 3
Android Location and Maps
Location Services and Maps
• Android provides location framework that
your app can use to determine
– Device’s location
– Listening for updates
• Google maps external library available for
displaying and managing maps
Location Services
• By using LocationManager you are able to
– Query for the list of LocationProviders for
the last known location
– Register for updates of the user’s current location
– Register for given Intent to be fired if the
device comes within given proximity of given
lat/long
Obtaining User Location
• Location can be determined via GPS and/or
cell tower + Wi-‐Fi signals
• GPS is accurate, but needs outdoors, fix is
slower and it uses more battery.
• To request location, you use
LocationManager
‐ -class
Requesting Location Updates
Can be also
GPS_PROVIDER
C
o
n
t
r
o
l
the
frequency
which
listener
receives
updates;
min time
and min
distance
Requesting User Permissions
• You must add permissions in order to get user
location
• ACCESS_COARSE_LOCATION
– If you use NETWORK_PROVIDER
• ACCESS_FINE_LOCATION
– If you use GPS_PROVIDER or NETWORK_PROVIDER
Getting Last Known Location
• First location can take time. Use cached
location!
– Location lastKnownLocation =
locationManager.getLastKnownLocation(LocationManager
.NETWORK_PROVIDER);
Mock Location Data
• If you don’t have Android device, you can use
emulator for location services by giving mock
data
• Mock data works only with GPS_PROVIDER
• Use
– Emulator Control View from Eclipse
– DDMS (Dalvik Debug Monitor Server)
– Geo command from console
Emulator Control View
Geo
• Connect to emulator from console
– telnet localhost
<console-port>
• Send the location data
– geo fix -121 46
GOOGLE MAPS EXTERNAL DIRECTORY
Google Maps
• External API Add-O
‐ n to Android SDK
• Install Google APIs Add-‐On from Android SDK
and AVD Manager (Google APIs by Google)
• When developing, set Google API Add-‐On as
target
Google APIs as Target
Overview
1. Add uses-library
‐ and internet permission
to manifest file
2. Use the Maps API
3. Get Maps API key and sign your app
1. Add uses-library
‐ element to Manifest file
• Because we're using the Google Maps library,
which is not a part of the standard Android
library, we need to declare it in the Android
Manifest
– <uses-library
android:name="com.google.android.m
aps"
/>
• Internet permissions (downloadable maps)
– <uses-permission
android:name="android.permission.I
NTERNET " />
2. Use the Maps API: MapView
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android
="http://schemas.android.com/apk/res/android"
android:id="@+id/mainlayout"
android:orientation="vertical"
android:layout_width="fill_par
ent"
android:layout_height="fill_pa
rent" >
<com.google.android.maps.MapVie
w android:id="@+id/mapview"
android:layout_width="fill_pa
rent"
android:layout_height="fill_p
arent"
android:clickable="true"
android:apiKey="Your Maps API
Key"
/>
</RelativeLayout>
2. Use the Maps API: Class
3. Get Maps API key and Sign Your App
• Create MD5 certificate fingerprint either in
release or in debug
• Release
– $ keytool -list -alias
alias_name -keystore my-release-
key.keystore
• Debug
– $ keytool -list -alias
androiddebugkey - keystore
<path_to_debug_keystore>.keystore
- storepass android -keypass
android
• Path to debug keystore in Windows Vista
– C:\Users\<user>\.android\
debug.keystore
3. Sign with the Service
• http://code.google.com/android/maps-‐a‐pi-
signup.html
Showing Latitude and Longitude on Map
mapView = (MapView) findViewById(R.id.mapview);
mapController = mapView.getController();
int lat = (int) (location.getLatitude() * 1E6);
int lng = (int) (location.getLongitude() * 1E6);
GeoPoint point = new GeoPoint(lat, lng);
mapController.animateTo(point);
DISPLAYING GRAPHICS ON MAPS
Custom Markers
• Overlay – Individual item on map
• Create custom class that inherites
ItemizedOverlay class
• ItemizedOverlay class is a base class
for an Overlay which consists of a list of
OverlayItems.
ItemizedOverlay
MyOverlayItems
ArrayList<OverlayItem> items
void
addOverLay(OverlayItem i)
OverlayItem
createItem(int i) int
size()
…
From Activity
Thank you