How to show your current location in a map
In Chapter: Location and Map Applications
Author: Enrique Diaz ('nRike')
You want to see where you are in a map using the built-in GPS in Android devices
We can take advantage of the Google APIs and Location-Based Services to allow people to find their friends, find relevant places and many more. In this snippet of code, we can read the GPS location and see where we are located in a map called from the internet.
+Warning+ The Google Maps V1 API is deprecated; new code should use the V2 API discussed in Recipes 4285 and 4297.
This example of code shows how to create a MapActivity to show where are we located. Also, we're going to add some permissions to allow our project read the latitude and longitude based in the Google Maps API.
Create a new project called MyCurrentLocation with an Activity called MyCurrentLocation. Because we need to obtain the Latitude and Longitude from the GPS, we need to add the following permissions in the Android Manifest:
<?xml version="1.0" encoding="utf-8"?>
<application android:icon="@drawable/icon" android:label="@string/app_name">
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<uses-sdk android:minSdkVersion="4" />
If you found this recipe useful, why not
buy the book
and have the whole collection always at hand?"