Android - Lecture 17 - LBS With Google API
Transcript of Android - Lecture 17 - LBS With Google API
-
8/2/2019 Android - Lecture 17 - LBS With Google API
1/49
ANDROID
Location Based Services with Google Map API
-
8/2/2019 Android - Lecture 17 - LBS With Google API
2/49
Creating your Android Project
When Creating a Project
with Google Maps you need
to pick a Target that
includes Google APIs
-
8/2/2019 Android - Lecture 17 - LBS With Google API
3/49
Editing your Manifest
Specify that you will use the Google Maps Library:
Then add the permissions for Internet, coarse, and
fine location:
-
8/2/2019 Android - Lecture 17 - LBS With Google API
4/49
Well need an API to utilize Google Maps and
have our Map show in our Application
Google Maps API Key
-
8/2/2019 Android - Lecture 17 - LBS With Google API
5/49
Google Maps API key
In order to show our google map we need toinclude an api key
First you need to location your debug.keystore in
your directory structure (Likely here): C:\Users\portia\.android
Record the file path (We will use it shortly)
Replace with yourown name
-
8/2/2019 Android - Lecture 17 - LBS With Google API
6/49
Google Maps API Key
Run the Command Prompt
Start -> Run -> cmd
-
8/2/2019 Android - Lecture 17 - LBS With Google API
7/49
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
-
8/2/2019 Android - Lecture 17 - LBS With Google API
8/49
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 androidkeystore
C:\Users\portia\.android\debug.keystore
-
8/2/2019 Android - Lecture 17 - LBS With Google API
9/49
Google Maps API Key
On your command prompt you should now see:
MD5 Certificate
-
8/2/2019 Android - Lecture 17 - LBS With Google API
10/49
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
http://code.google.com/android/maps-api-signup.htmlhttp://code.google.com/android/maps-api-signup.htmlhttp://code.google.com/android/maps-api-signup.htmlhttp://code.google.com/android/maps-api-signup.htmlhttp://code.google.com/android/maps-api-signup.htmlhttp://code.google.com/android/maps-api-signup.html -
8/2/2019 Android - Lecture 17 - LBS With Google API
11/49
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 changethe key to use your maps. However, itll work fine
on the device.
-
8/2/2019 Android - Lecture 17 - LBS With Google API
12/49
Now that we have an API Key we can generate
our Map
Showing your Map
-
8/2/2019 Android - Lecture 17 - LBS With Google API
13/49
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!
-
8/2/2019 Android - Lecture 17 - LBS With Google API
14/49
Our MapActivity
Now rather than simply extending Activity in your
class you now need to extend MapActivity
Your map should now
appear
-
8/2/2019 Android - Lecture 17 - LBS With Google API
15/49
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()
-
8/2/2019 Android - Lecture 17 - LBS With Google API
16/49
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
-
8/2/2019 Android - Lecture 17 - LBS With Google API
17/49
Looking good
Zoom controls should now appear when you click the
bottom of the screen
-
8/2/2019 Android - Lecture 17 - LBS With Google API
18/49
Lets now make the map Appear at your location
Your Location
-
8/2/2019 Android - Lecture 17 - LBS With Google API
19/49
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
-
8/2/2019 Android - Lecture 17 - LBS With Google API
20/49
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
-
8/2/2019 Android - Lecture 17 - LBS With Google API
21/49
Requesting Location Updates
We now need to listen for location updates from the
location manager:
-
8/2/2019 Android - Lecture 17 - LBS With Google API
22/49
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
-
8/2/2019 Android - Lecture 17 - LBS With Google API
23/49
Show Location
Create your showLocation method
if the location is not empty then get its latitude and
longitude
-
8/2/2019 Android - Lecture 17 - LBS With Google API
24/49
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
-
8/2/2019 Android - Lecture 17 - LBS With Google API
25/49
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
-
8/2/2019 Android - Lecture 17 - LBS With Google API
26/49
Testing Current Location
Now under the Location Controls section put in a
Longitude and Latitude and Send it to your app
-
8/2/2019 Android - Lecture 17 - LBS With Google API
27/49
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()
-
8/2/2019 Android - Lecture 17 - LBS With Google API
28/49
We now want the map to change locations when
your location changes
Updating Location on Change
-
8/2/2019 Android - Lecture 17 - LBS With Google API
29/49
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 toonLocationChanged
Delete showLocation method
call onLocationChanged(location) rather thanshowLocation from onCreate()
-
8/2/2019 Android - Lecture 17 - LBS With Google API
30/49
Location Should Change!
Now every time you send a new longitude /
latitude pair through the DDMS your locations
should update on the emulator
-
8/2/2019 Android - Lecture 17 - LBS With Google API
31/49
We now want to add a pin to illustrate your
location on the map
Adding A Pin
-
8/2/2019 Android - Lecture 17 - LBS With Google API
32/49
Adding a Pin
The Pins on maps used to highlight certain locations
are called Overlays
We will create an Overlay to pin your location
-
8/2/2019 Android - Lecture 17 - LBS With Google API
33/49
CurrentLocationOverlay
Create a new class called CurrentLocationOverlay
and have it extend ItimizedOverlay
Create an OveralyItem instance variable of theclass used to hold the current location Overlay
-
8/2/2019 Android - Lecture 17 - LBS With Google API
34/49
CurrentLocationOverlay Constructor
Modify the constructor as such to get the drawable
graphic in a state ready to be a pin
-
8/2/2019 Android - Lecture 17 - LBS With Google API
35/49
CreateItem
This method will return the current Overlay
and size() will return 1 because we only have 1 Overlay
-
8/2/2019 Android - Lecture 17 - LBS With Google API
36/49
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
-
8/2/2019 Android - Lecture 17 - LBS With Google API
37/49
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
-
8/2/2019 Android - Lecture 17 - LBS With Google API
38/49
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
-
8/2/2019 Android - Lecture 17 - LBS With Google API
39/49
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
-
8/2/2019 Android - Lecture 17 - LBS With Google API
40/49
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
-
8/2/2019 Android - Lecture 17 - LBS With Google API
41/49
Now lets add another set of Overlays to keep
track of special places
Adding More Overlays
-
8/2/2019 Android - Lecture 17 - LBS With Google API
42/49
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 theOverlayItems
-
8/2/2019 Android - Lecture 17 - LBS With Google API
43/49
Start from CurrentLocationOverlay
Make a copy of CurrentLocationOverlay and call it
SpecialLocationOverlay.
Rename the class accordingly
Replace the instance variable with an ArrayList
becomes
-
8/2/2019 Android - Lecture 17 - LBS With Google API
44/49
Change methods for ArrayList
-
8/2/2019 Android - Lecture 17 - LBS With Google API
45/49
Adding your Overlays
In your Main Activity after initializing your
CurrentLocationOverlays call a method
addSpecialLocationOverlays() which we will make
-
8/2/2019 Android - Lecture 17 - LBS With Google API
46/49
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
-
8/2/2019 Android - Lecture 17 - LBS With Google API
47/49
addSpecialLocationOverlays()
Well make this
method
-
8/2/2019 Android - Lecture 17 - LBS With Google API
48/49
addSpecialOverlay()
This method makes a point from the specified
longitude / latitude and adds it to the
specialoverlays
-
8/2/2019 Android - Lecture 17 - LBS With Google API
49/49
Special Points Appear Now