4. Download Google Play
Services
Google Maps Android API V2 is part of Google’s Play
services SDK, which you must download and
configure to work with your existing Android SDK
installation in Eclipse to use the mapping functions.
In Eclipse, choose Window > Android SDK Manager.
In the list of packages that appears scroll down to
theExtras folder and expand it.
Select the Google Play services checkbox and install
the package.
5. Import Google Play Services
into your workspace
Once Eclipse downloads and installs the Google Play
services package, you can import it into your workspace.
Select File > Import > Android > Existing Android Code
into Workspace then browse to the location of the
downloaded Google Play services package on your
computer.
It should be inside your downloaded Android SDK directory,
at the following
location:extras/google/google_play_services/libproject/google
-play-services_lib.
6. Generate API key
To access the Google Maps tools, you need an API
key, which you can obtain through a Google account.
The key is based on your Android app debug or
release certificate.
If you have released apps before, you might have
used the keytool resource to sign them.
In that case, you can use the keystore you generated
at that time to get your API key.
9. Turn on Google Maps
Android API v2
Select Services from
the list on the left of the
APIs console.
You should see a list of
Google services, scroll
down to Google Maps
Android API V2 and
click to turn it on for
your account.
10. Create Key
Now we can get a key for the
app.
Select API Access on the
left-hand-side of the API
console.
You may already see a key
for browser apps, but we
need one specifically for
Android apps.
Near the bottom of the page,
select Create new Android
key.
12. Include Google Play Services
lib into your project
Although we added the Google Play services package to the
Eclipse workspace, we still need to setup this particular app to
use it.
Select your new project in the Eclipse Package Explorer and
configure its Properties(right-click > Properties or Window >
Properties with the project selected).
Select the Android tab and scroll to the Library section, then
choose Add.
Select the Google Play Services library to add it to your
project.
18. Location Manager
What is a Location Manager?
This class provides access to the system
location services.
These services allow applications to obtain
periodic updates of the device's geographical
location, or to fire an application-specified
Intent when the device enters the proximity of a
given geographical location.
19. Location Listener
What is a Location Listener?
Used for receiving notifications from the
LocationManager when the location has changed.
These methods are called if the LocationListener
has been registered with the location manager
service using the requestLocationUpdates(String,
long, float, LocationListener) method.
20. Location Strategies
Knowing where the user is allows your application to be smarter and
deliver better information to the user.
When developing a location-aware application for Android, you can utilize
GPS and Android's Network Location Provider to acquire the user location.
Although GPS is most accurate, it only works outdoors, it quickly consumes
battery power, and doesn't return the location as quickly as users want.
Android's Network Location Provider determines user location using cell
tower and Wi-Fi signals, providing location information in a way that works
indoors and outdoors, responds faster, and uses less battery power.
To obtain the user location in your application, you can use both GPS and
the Network Location Provider, or just one.
21. Example of Obtaining User
Location
// Acquire a reference to the system Location Manager
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
// Define a listener that responds to location updates
LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
// Called when a new location is found by the network location provider.
makeUseOfNewLocation(location);
}
public void onStatusChanged(String provider, int status, Bundle extras) {}
public void onProviderEnabled(String provider) {}
public void onProviderDisabled(String provider) {}
};
// Register the listener with the Location Manager to receive location updates
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
23. Getting a fast fix with the
last known location
String locationProvider = LocationManager.NETWORK_PROVIDER;
// Or use LocationManager.GPS_PROVIDER
Location lastKnownLocation = locationManager.getLastKnownLocation(locationProvider);
24. Stop listening for updates
// Remove the listener you previously added
locationManager.removeUpdates(locationListener);