分类: LINUX
2008-11-27 11:38:06
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 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 ). This method takes in four parameters:
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 . 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.
|
|
|||
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 ).
In main.xml, replace the
Finally, modify the GPS.java file to incorporate Google Maps (see ).
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 ).
Summary
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.
|