Chinaunix首页 | 论坛 | 博客
  • 博客访问: 822099
  • 博文数量: 210
  • 博客积分: 10002
  • 博客等级: 上将
  • 技术积分: 1840
  • 用 户 组: 普通用户
  • 注册时间: 2008-11-18 09:56
文章分类

全部博文(210)

文章存档

2011年(1)

2010年(6)

2009年(65)

2008年(138)

我的朋友

分类: 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. 
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:



package="net.learn2develop.GPS">

android:name="android.permission.ACCESS_FINE_LOCATION" />

android:label="@string/app_name">








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:

  • 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 ().

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.




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 ).


The Location Controls Section: This section allows you to test GPS functionality on the Android emulator.
 
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 ).

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 . Once a .GPX file is downloaded, click the Load GPX… button to load the .GPX file (see ).

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 . Like the .GPX file, you can also send a series of coordinates to the Android emulator by clicking on the Play button (see ).


Loading a .GPX File: Click the Load GPX button to load the .GPX file.
 
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 ).

In main.xml, replace the element with the element:


You Are Here: Displaying the current location using Google Maps.



android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>

android:id="@+id/mapview1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:enabled="true"
android:clickable="true"
android:apiKey="apisamples" />


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
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.
阅读(1706) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~