Android - Lecture 17 - LBS With Google API
Android - Lecture 17 - LBS With Google API
Then add the permissions for Internet, coarse, and fine location:
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
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
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
MD5 Certificate
Accept the Terms and Conditions and enter your MD5 fingerprint in the text box. Click Generate API Key
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.
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
And create a method for initializing the map and call it from onCreate()
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
The service provider (gps, network) will be the best connection we can get
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:
Show Location
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
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
Now under the Location Controls section put in a Longitude and Latitude and Send it to your app
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()
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()
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
CurrentLocationOverlay Constructor
Modify the constructor as such to get the drawable graphic in a state ready to be a pin
CreateItem
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
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
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
We will want to reset the pin every time the lcoation is changed Hence in our onLocationChanged method after getting our point well
update
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
Make a copy of CurrentLocationOverlay and call it SpecialLocationOverlay. Rename the class accordingly Replace the instance variable with an ArrayList
becomes
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()
addSpecialOverlay()
This method makes a point from the specified longitude / latitude and adds it to the specialoverlays