Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

You Are Here: Using GPS and Google Maps in Android

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.


advertisement
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:



<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="net.learn2develop.GPS"> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".GPS" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>

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.

 


Visit the Android Dev Center

 

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 Listing 1). 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 (Listing 2).

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 Listing 3.

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.



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap