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 : Page 3

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

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.



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.
Comment and Contribute

 

 

 

 

 


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

 

 

Sitemap