Professional Documents
Culture Documents
Tagele B.
1
Android: Localization basics
Geolocalization Identification of the real-world
geographic location of an end-user.
Geolocalization Identification of the
real-world geographic location of an the
end-user.
LOCALIZATION THROUGH
location-based GPS
services (LBS): enable
LOCALIZATION THROUGH WI-FI
you to find the
LOCALIZATION devices
THROUGH current
CELLULAR location.
NETWORK
4
Getting a Google Play API Key
Note:Existing keys from a Google Maps Android v1 application,
commonly known as MapView, will not work with the v2 API. The
Google Maps Android API uses a new system of managing keys.
Note:You need anAndroid API key, not a browser key. You can
use the same API key for your Google Maps Android API apps and
yourGoogle Places API for Androidapps.
Access your project's API keys and other credentials as
follows:
1. Go to the Google Developers
Console(https://console.developers.google.com/)
2. In the sidebar on the left, select Credentials.
3. If your project doesn't already have an Android API key,
create one now by selecting Add credentials > API key >
Android key.
4. In the resulting dialog, enter your app's SHA-1 fingerprint
and package name. For ample:
B:0D:AC:74:D3:21:E1:43:67:71:9B:62:91:AF:A1:66:6E:44:5D:75
com.example.android.mapexample 5
Add the API key to your application
Follow the steps below to include the API key in
your application's manifest, contained in the file
AndroidManifest.xml.
In AndroidManifest.xml, add the following element
as a child of the
9
SELECTING A LOCATION PROVIDER
Depending on the device, you can use several
technologies to determine the current location.
Each technology, available as a Location
Provider, offers different capabilities including
differences in power consumption, accuracy, and
the ability to determine altitude, speed, or heading
information.
Finding Location Providers
The LocationManager class includes static string
constants that return the provider name for three
Location Providers:
LocationManager.GPS_PROVIDER
LocationManager.NETWORK_PROVIDER
LocationManager.PASSIVE_PROVIDER
The GPS and passive provider requires fine 10
To get a list of the names of all the providers
available (based on hardware available on the
device,and the permissions granted the
application), call getProviders, using a Boolean
to indicate if you want all, or only the enabled,
providers to be returned:
13
getBestProvider to return the best Provider for
If more than one Location Provider matches your
Criteria,
The one with the greatest accuracy is returned.
If no Location Providers meet your
requirements, the Criteria are loosened, in
the following order, until a provide
Power use
Accuracy of returned location
Accuracy of bearing, speed, and altitude
Availability of bearing, speed, and
altitude r is found:
The criterion for allowing a device with monetary
cost is never implicitly relaxed. If no provider is
14
Determining Location Provider Capabilities
To get an instance of a specific provider, call
getProvider, passing in the name:
19
Override the onCreate method of the WhereAmI Activity to get a
reference to the Location Manager.
Define LocationManager and Provider Class
Call getLastKnownLocation to get the last known location, and
pass it in to an updateWithNewLocation method stub.
20
Complete the updateWithNewLocation method
to show the passed-in Location in the Text View by
extracting the latitude and longitude values.
21
Refreshing the Current Location
The requestLocationUpdates methods are used
to request regular updates of location changes
using a LocationListener.
Location Listeners also contain hooks for
changes in a providers status and availability.
The requestLocationUpdates method accepts
either a specific Location Provider name or a
set of Criteria to determine the provider to use.
To optimize efficiency and minimize cost and
power use, you can also specify the minimum
time and the minimum distance between
location change updates.
Example: shows the skeleton code for requesting
regular updates based on a minimum time and
distance using a Location Listener. 22
23
Tracking Your Location in Where Am I
In this example, the Where Am I project is
enhanced to update your current location by
listening for location changes.
Updates are restricted to one every 2 seconds,
and only when movement of more than 10
meters has been detected.
Rather than explicitly selecting a provider, you
update the application to use a set of Criteria and
let Android find the best provider available.
1. Open the WhereAmI Activity
Update the onCreate method to use Criteria to find a
Location Provider that features high accuracy and
draws as little power as possible.
24
Requesting a Single Location Update
25
Create a new LocationListener instance variable
that fires the existing updateWithNew Location
method whenever a location change is detected.
26
BEST PRACTICE FOR LOCATION UPDATES
30
Forward Geocoding
Forward geocoding (or just geocoding) determines
map coordinates for a given location.
To geocode an address, call
getFromLocationName on a Geocoder object.
Pass in a string that describes the address you
want the coordinates for, the maximum number of
results to return, and optionally provide a
geographic bounding box within which to restrict
your search results:
List<Address> result =
gc.getFromLocationName(streetAddress,
maxResults);
As with reverse geocoding, if no matches are
found, null is returned. The availability,accuracy,
and granularity of geocoding results depends 31
Geocoding Where Am I
In this example you extend the Where Am I project
to include and update the current street address
whenever the device moves.
32
33
34
CREATING MAP-BASED ACTIVITIES
One of the most intuitive ways to provide context
for a physical location or address is to use a map.
Using a MapView, you can create Activities that
include an interactive map.
Map Views support annotation using Overlays and
by pinning Views to geographical locations.
Map Views offer full programmatic control of the
map display, letting you control the zoom,
location, and display modes including the option
to display a satellite view.
In the following sections you see
how to use Overlays and the MapController to create
dynamic map-based Activities.
Unlike online mashups, your map Activities run
natively on the device, giving you a more 35
Introducing Map View and Map Activity
This section introduces several classes used to
support Android maps:
MapView: is the user interface element that
displays the map.
MapActivity: is the base class you extend to
create an Activity that can include a Map View. The
MapActivity class handles the application life cycle
and background service management required for
displaying maps. You can only use Map Views
within MapActivity-derived Activities.
Overlay: is the class used to annotate your maps.
Using Overlays, you can use a Canvas to draw
onto any number of layers displayed on top of a
Map View.
MapController: is used to control the map, 36
Getting Your Maps API Key
To use a Map View in your application, you must
first obtain an API key from the Android developer
website at http://code.google.com/android/maps-
api-signup.html.
Without an API key the Map View cannot download
the tiles used to display the map.
To obtain a key, you need to specify the MD5
fingerprint of the certificate used to sign your
application.
Generally, you sign your application using two
certificates: a default debug certificate and a
production certificate.
The following sections explain how to obtain the
MD5 fingerprint of any signing certificate used
with your application. 37
Creating a Map-Based Activity
To use maps in your applications, you need to
extend MapActivity.
The layout for the new class must then include a
MapView to display a Google Maps interface
element.
Override the onCreate method to lay out the
screen that includes a MapView, and
override isRouteDisplayed to return true if the
Activity will be displaying routing information (such
as traffic directions).
The Android maps library is not a standard Android
package; as an optional API, it must be explicitly
included in the application manifest before it can
be used. Add the library to your manifest
<uses-library 38
MyMapActivity
39
MapView.xml
42
Using the Map Controller
Use the Map Controller to pan and zoom a MapView. You can
get a reference to a MapViews controller using getController.
MapController mapController = mapView.getController();
Map locations in the Android mapping classes are
represented by GeoPoint objects, which contain a latitude
and longitude measured in microdegrees. To convert
degrees to microdegrees, multiply by 1E6 (1,000,000).
Before you can use the latitude and longitude values stored
in the Location objects returned by location-based services,
you need to convert them to microdegrees and store them
as GeoPoints.
Double lat = 37.422006*1E6;
Double lng = -122.084095*1E6;
GeoPoint point = new GeoPoint(lat.intValue(), lng.intValue());
Recenter and zoom the Map View using the setCenter and
setZoom methods available on the Map Views
MapController.
43
mapController.setCenter(point);
Mapping Where Am I
The following code example extends the Where Am I project
again. This time you add mapping functionality by
transforming it into a Map Activity. As the device location
changes, the map automatically re-centers on the new
position.
1. Start by checking your project properties to ensure your
project build target is a Google APIs target rather than an
Android Open Source Project target
2. Modify the application manifest t to add the maps library:
45
5. Running the application now should display the
original address text with a MapView beneath it, as
shown in Figure below.
46
6. Returning to the WhereAmI Activity, configure the Map View and
store a reference to its MapController as an instance variable. Set
up the Map View display options to show the satellite and zoom in
for a closer look.
47
7. The final step is to modify the updateWithNewLocation
method to re-center the map on the current location using
the Map Controller:
48
49
Reading Assignment
Creating and Using Overlays
Creating New Overlays
Drawing on the Overlay Canvas
Handling Map Tap Events
Adding and Removing Overlays
50