- 论坛徽章:
- 0
|
In
the next installment of this Android series, you'll learn how to
incorporate GPS and Google Maps into your Android application, so your
users can see where they are, all the time.
by
Wei-Meng Lee
![]()
n
the previous article in the Android series, you learned how to
integrate the Google Maps into your Android application. One of the
really neat ways you can use Google Maps is to send GPS data directly
into it so that you can view your current location real-time. This
article will show you how to programmatically access the data returned
by your built-in GPS receiver and then send the data to Google Maps.
Creating the Project
Using Eclipse, create a new Android project and name it GPS.java.
To use GPS functionality in your Android application, you'll need to add the ACCESS_FINE_LOCATION permission to the AndroidManifest.xml file:
In Android, location-based services are provided by the LocationManager class located in the android.location
package. Using the LocationManager class, your application can obtain
periodic updates of the device's geographical locations as well as fire
an intent when it enters the proximity of a certain location.
In the GPS.java file, first obtain a reference to the LocationManager class using the getSystemService()
method. To be notified whenever there is a change in location, you need
to register for a request for changes in locations so that your program
can be notified periodically. This is done via the requestLocationUpdates() method (see
[url=javascript:showSupportItem('listing1')]Listing 1[/url]
). This method takes in four parameters:
- provider: The name of the provider with which you register
- minTime: The minimum time interval for notifications, in milliseconds.
- minDistance: The minimum distance interval for notifications, in meters.
- listener: An object whose onLocationChanged() method will be called for each location update.
The MyLocationListener class (shown shortly) implements the
LocationListener abstract class. There are four methods that you need
to override in this implementation:
- onLocationChanged(Location location): This method is called when the location has changed.
- onProviderDisabled(String provider): This method is called when the provider is disabled by the user.
- onProviderEnabled(String provider): This method is called when the provider is enabled by the user.
- onStatusChanged(String provider, int status, Bundle extras): This method is called when the provider status changes.
In this example, you're more interested in what happens when a location changes, so you'll write some code in the onLocationChanged() method (
[url=javascript:showSupportItem('listing2')]Listing 2[/url]
).
Specifically, when a location changes you will display a small
dialog on the screen showing the new location information: latitude and
longitude. You show this dialog using the Toast class. The complete GPS.java now looks like
[url=javascript:showSupportItem('listing3')]Listing 3[/url]
.
To test the application, press F11 in Eclipse to debug the application
on the Android emulator. While at the time of writing this article, you
may not have a real Android device to test, there are a number of ways
to test GPS functionality on your Android application.
Testing GPS Functionalities
The DDMS tool in the Android plug-in for Eclipse allows you to test GPS
functionality very easily. In Eclipse, switch to the DDMS view and
locate the Location Controls section in the Emulator Control tab (see
[url=javascript:showSupportItem('figure1')]Figure 1[/url]
).
[url=javascript:showSupportItem('figure1')] [/url]
[url=javascript:showSupportItem('figure1')]Figure 1.[/url]
The Location Controls Section: This section allows you to test GPS functionality on the Android emulator.
[url=javascript:showSupportItem('figure2')] [/url]
[url=javascript:showSupportItem('figure2')]Figure 2.[/url]
The New Location Information: When the GPS data is received, the application displays the latitude and longitude obtained.
There are three separate tabs in the Location Controls section. First,
you can manually send in the coordinates by specifying the latitude and
longitude. When the GPS data is received on the Android emulator, the
application will display the latitude and longitude obtained (see
[url=javascript:showSupportItem('figure2')]Figure 2[/url]
).
Another way to send in geographical locations is to use a .GPX file. GPX (GPS Exchange Format) is a light-weight XML data format for interchange of GPS data. You can download GPS samples
here
. Once a .GPX file is downloaded, click the Load GPX… button to load the .GPX file (see
[url=javascript:showSupportItem('figure3')]Figure 3[/url]
).
You can click the Play button to send a series of coordinates to the
Android emulator at regular time intervals. The Android Eclipse plug-in
also supports KML (Keyhole Markup Language) files. You can download a
sample .KML file
here
. Like the .GPX file, you can also send a series of coordinates to the Android emulator by clicking on the Play button (see
[url=javascript:showSupportItem('figure4')]Figure 4[/url]
).
[url=javascript:showSupportItem('figure3')] [/url]
[url=javascript:showSupportItem('figure3')]Figure 3.[/url]
Loading a .GPX File: Click the Load GPX button to load the .GPX file.
[url=javascript:showSupportItem('figure4')] [/url]
[url=javascript:showSupportItem('figure4')]Figure 4.[/url]
Loading a .KML File: Sending coordinates to the .KML file.
If you don't use Eclipse, you can also telnet to the Android emulator and use the geo command to send GPS coordinates to the emulator:
C:\>telnet localhost 5554
Android Console: type 'help' for a list of commands
OK
geo fix -82.411629 28.054553
OK
The geo command also allows you to send NMEA data sentences, like this:
geo nmea $GPGGA,001431.092,0118.2653,N,10351.1359,E,0,00,,-19.6,M,4.1,M,,0000*5B
Using GPS with Google Maps
Simply displaying the latitude and longitude when a location has
changed is not very interesting. A much more interesting thing to do
would be to couple the data together with the Google Maps application.
As you learnt in the previous article, for Google Maps to work, you need to add the ACCESS_FINE_LOCATION permission and then use the Google Maps library (see
[url=javascript:showSupportItem('listing4')]Listing 4[/url]
).
In main.xml, replace the element with the element:
[url=javascript:showSupportItem('figure5')] [/url]
[url=javascript:showSupportItem('figure5')]Figure 5.[/url]
You Are Here: Displaying the current location using Google Maps.
Finally, modify the GPS.java file to incorporate Google Maps (see
[url=javascript:showSupportItem('listing5')]Listing 5[/url]
).
In the above, when a location changes, the latitude and longitude is
sent to the Google Maps application, which then displays the map of the
current location (see
[url=javascript:showSupportItem('figure5')]Figure 5[/url]
).
Summary
You've seen how to make use of the built-in GPS receivers in Android
devices to do some very interesting things. Besides displaying maps,
you can also log your positions by saving the GPS coordinates into a
text file. This will enable you to retrieve them at a later date.
Wei-Meng Lee is a Microsoft MVP and founder of
Developer Learning Solutions
,
a technology company specializing in hands-on training on the latest
Microsoft technologies. He is an established developer and trainer
specializing in .NET and wireless technologies. Wei-Meng speaks
regularly at international conferences and has authored and coauthored
numerous books on .NET, XML, and wireless technologies. He writes
extensively on topics ranging from .NET to Mac OS X. He is also the
author of the .NET Compact Framework Pocket Guide, ASP.NET 2.0: A
Developer's Notebook (both from O'Reilly Media, Inc.), and Programming
Sudoku (Apress). Here is Wei-Meng's
blog
.
本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u2/85805/showart_1668835.html |
|