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 Listing 4).
In main.xml, replace the <TextView> element with the <MapView> element:
 | |
| Figure 5. You Are Here: Displaying the current location using Google Maps. |
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<com.google.android.maps.MapView
android:id="@+id/mapview1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:enabled="true"
android:clickable="true"
android:apiKey="apisamples" />
</LinearLayout>
Finally, modify the
GPS.java file to incorporate Google Maps (see
Listing 5).
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 Figure 5).
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.