0% found this document useful (0 votes)
71 views

Android - Lecture 17 - LBS With Google API

1) This document discusses how to add location-based services to an Android application using the Google Maps API. 2) It provides instructions for getting an API key for Google Maps, setting up the manifest file, creating a MapActivity, and displaying the user's current location on a map. 3) It also explains how to add overlays to the map to mark points of interest, such as the user's location using a pin icon.

Uploaded by

Joe Jacob
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
71 views

Android - Lecture 17 - LBS With Google API

1) This document discusses how to add location-based services to an Android application using the Google Maps API. 2) It provides instructions for getting an API key for Google Maps, setting up the manifest file, creating a MapActivity, and displaying the user's current location on a map. 3) It also explains how to add overlays to the map to mark points of interest, such as the user's location using a pin icon.

Uploaded by

Joe Jacob
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 49

ANDROID

Location Based Services with Google Map API

Creating your Android Project


When Creating a Project with Google Maps you need to pick a Target that includes Google APIs

Editing your Manifest

Specify that you will use the Google Maps Library:

Then add the permissions for Internet, coarse, and fine location:

Google Maps API Key


Well need an API to utilize Google Maps and have our Map show in our Application

Google Maps API key

In order to show our google map we need to include an api key First you need to location your debug.keystore in your directory structure (Likely here):
C:\Users\portia\.android

Replace with your own name

Record the file path (We will use it shortly)

Google Maps API Key

Run the Command Prompt


Start

-> Run -> cmd

Google Maps API Key

Now we will utlize the keytool to get your MD5 Fingerprint keytool.exe is likely located at:
C:\Program

Files\Java\jre6\bin

Well first change directory on the command prompt to the location of keytools
execute

the line on the command prompt cd C:\Program Files\Java\jre6\bin

Google Maps API Key

Now you will execute a line similar to the following but with your directory path (FYI: this is all one line): keytool -list -alias androiddebugkey -storepass android keypass android keystore C:\Users\portia\.android\debug.keystore

Google Maps API Key

On your command prompt you should now see:

MD5 Certificate

Google Maps API Key

Now go to the following site:


http://code.google.com/android/maps-api-signup.html

Accept the Terms and Conditions and enter your MD5 fingerprint in the text box. Click Generate API Key

Google Maps API Key

Now you have your API key KEEP IT

Your Key is good for all apps on your computer as long as you keep normal settings, but once you transfer to another computer youll have to change the key to use your maps. However, itll work fine on the device.

Showing your Map


Now that we have an API Key we can generate our Map

Creating Map in Layout File

Well use the com.google.android.maps.MapView tag to generate the map Make sure to include an id for your map and the api key Note we still need to add some Java code for the map to show!

Our MapActivity

Now rather than simply extending Activity in your class you now need to extend MapActivity
Your map should now appear

Customizing your Map

We will now add to instance attributes for the class:

And create a method for initializing the map and call it from onCreate()

MapView and MapController


Setup your MapView and MapController Now you can specify Street or Satellite view, Add your Zoom Controls, and Specify how much you zoom by

Looking good

Zoom controls should now appear when you click the bottom of the screen

Your Location
Lets now make the map Appear at your location

Location Listener

We need to now listen for location changes so that we can find out where we currently are To do so our class now needs to implement LocationListener

Location Manager & Service Provider

Add the following two instance variables

Now initialize them in your onCreate() method

The service provider (gps, network) will be the best connection we can get

Requesting Location Updates

We now need to listen for location updates from the location manager:

Current Location

Now well use the location manager to get your last known location. This may not be where you currently are though:

Then call a function to show the specified location

We will make this function

Show Location

Create your showLocation method


if

the location is not empty then get its latitude and longitude

Show Location

Then you will create a point with the given latitude and longitude And pass it to the mapController so it animates to that place

Testing Current Location

To test this on the emulator you need to send the phone a location using the DDMS perspective then reload the app:
Windows -> Open Perspective -> Other -> DDMS

Testing Current Location

Now under the Location Controls section put in a Longitude and Latitude and Send it to your app

Now youll see your Location

You will have to re-open your app when you send in a new location to test it as we are only showing data onCreate()

Updating Location on Change


We now want the map to change locations when your location changes

onLocationChanged

We now will change the location every time the onLocationChanged method is called rather than having an explicit showLocation method. Copy the code from showLocation to onLocationChanged Delete showLocation method call onLocationChanged(location) rather than showLocation from onCreate()

Location Should Change!

Now every time you send a new longitude / latitude pair through the DDMS your locations should update on the emulator

Adding A Pin
We now want to add a pin to illustrate your location on the map

Adding a Pin

The Pins on maps used to highlight certain locations are called Overlays We will create an Overlay to pin your location

CurrentLocationOverlay

Create a new class called CurrentLocationOverlay and have it extend ItimizedOverlay<OverlayItem>


Create an OveralyItem instance variable of the class used to hold the current location Overlay

CurrentLocationOverlay Constructor

Modify the constructor as such to get the drawable graphic in a state ready to be a pin

CreateItem

This method will return the current Overlay

and size() will return 1 because we only have 1 Overlay

addItem

We will use this method to create our Overlay, given a point, what we want the title of the point to be, and the service provider

We then call populate to do all the processing of our Overlay

Adding Instance Variables

We will now add the following instance variables to


reference

our CurrentLocationOverlay, and to hold the list of all Overlays on the Map

Adding a Marker

Before we initialize our overlays we need to add a pin icon to our drawable directory to hold the image that will be drawn on our screen for the overlays Call it map_pin_current

Initializing the Overlays

Call the initCurrentLocationMarkerOverlays() from the onCreate() method after setting up the map
This

method captures the mapoverlays Gets the image for the pin and creates the itimizedOverlay to hold our overlay in the future

When to reset the Pin?

We will want to reset the pin every time the lcoation is changed Hence in our onLocationChanged method after getting our point well
update

the itimizedOverlay reset the maps overlays

Adding More Overlays


Now lets add another set of Overlays to keep track of special places

Holding the Overlays

Now we need to keep track of a set of Overlays rather than just one of the current place. To do this we will create another ItimizedOverlay that contains an ArrayList to hold all the OverlayItems

Start from CurrentLocationOverlay

Make a copy of CurrentLocationOverlay and call it SpecialLocationOverlay. Rename the class accordingly Replace the instance variable with an ArrayList
becomes

Change methods for ArrayList

Adding your Overlays

In your Main Activity after initializing your CurrentLocationOverlays call a method addSpecialLocationOverlays() which we will make

addSpecialLocationOverlays()

Here well get our pin from drawable (Make sure to add a new icon to the drawable directory) Initialize the SpecialLocationOverlay Add location Show them in the Map

addSpecialLocationOverlays()

Well make this method

addSpecialOverlay()

This method makes a point from the specified longitude / latitude and adds it to the specialoverlays

Special Points Appear Now

You might also like