This document provides instructions for accessing GPS information on an Android phone and displaying GPS location data on a Google Maps view. It describes adding the ACCESS_FINE_LOCATION permission, obtaining location updates from the LocationManager class, and registering a LocationListener. It then explains how to generate an API key for Google Maps and display the user's coordinates on a map when the location changes.
This document provides instructions for accessing GPS information on an Android phone and displaying GPS location data on a Google Maps view. It describes adding the ACCESS_FINE_LOCATION permission, obtaining location updates from the LocationManager class, and registering a LocationListener. It then explains how to generate an API key for Google Maps and display the user's coordinates on a map when the location changes.
( Modified from Original Source at http://www.devx.com/wireless/Article/3939 !
"sing #clipse$ create a new Android pro%ect and name it &'S.%ava. (o use &'S functionalit) in )our Android application$ )ou*ll need to add the A++#SS,-./#,0O+A(.O/ permission to theAndroidManifest.xml file: <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="android.gps" android:versionCode="1" android:versionName="1.0"> <uses-permission android:name="android.permission.ACCESS_F!E_"#CA$#!" /> <application android:icon="%dra&a'(e/icon" android:label="%string/app_name"> <activity android:name=".androidgps" android:label="%string/app_name"> <intent-filter> <action android:name="android.intent.action.)A!" /> <category android:name="android.intent.categor*."A+!C,E-" /> </intent-filter> </activity> </application> </manifest> .n Android$ location12ased services are provided 2) the 0ocationManager class located in the android.location pac3age. "sing the 0ocationManager class$ )our application can o2tain periodic updates of the device*s geographical locations as well as fire an intent when it enters the proximit) of a certain location. .n the &'S.%ava file$ first o2tain a reference to the 0ocationManager class using the getS)stemService(! method. (o 2e notified whenever there is a change in location$ )ou need to register for a re4uest for changes in locations so that )our program can 2e notified periodicall). (his is done via the re4uest0ocation"pdates(!. (his method ta3es in four parameters: provider: (he name of the provider with which )ou register min(ime: (he minimum time interval for notifications$ in milliseconds. min5istance: (he minimum distance interval for notifications$ in meters. listener: An o2%ect whose on0ocation+hanged(! method will 2e called for each location update. .n this example$ )ou*re more interested in what happens when a location changes$ so )ou*ll write some codein the on0ocation+hanged(!method. Specificall)$ when a location changes )ou will displa) a small dialog on the screen showing the new location information: latitude and longitude. 6ou show this dialog using the (oast class. (he complete &'S.%ava now loo3s li3e: package android.gps; import android.app.Activity; import android.os.Bundle; import android.content.Context; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; import android.widget.Toast; public class androidgps extends Activity { !! Called w"en t"e activity is #irst created. ! private LocationManager lm; private LocationListener locationListener; !! Called w"en t"e activity is #irst created. ! $%verride public void onCreate&Bundle saved'nstance(tate) { super.onCreate&saved'nstance(tate); setContent*iew&+.layout.main);
,,,use t"e LocationManager class to obtain -.( locations,,, lm / &LocationManager) get(ystem(ervice&Context.L%CAT'%01(2+*'C2);
Using GPS with Google Maps (Modified from original source at: http://www.devx.com/wireless/Article/39239/0/page/3 http://www.remwedevelopment.com/dev/a3!/Android"#ow"$o"%et"&p"an"A'(")e*"for"+oogle"Maps.html , %impl* displa*ing the latitude and longitude when a location has changed is not ver* interesting. A much more interesting thing to do would e to couple the data together with the +oogle Maps application. -hen *ou create *our application choose the +oogle A'( (as the target device,. (f not listed. *ou need to update /clipse using the avd update manager (under windows menu item in eclipse,. As *ou learnt in the previous %ection. for +oogle Maps to wor0. *ou need to add the CC!""#$%N!#&'C(%'N permission into the Androidmanifest.xml file and then use the +oogle Maps lirar*.
</&inear&ayout> (he google maps api uses an api3e) (7api3e)8 value in main.xml file needs to 2e replaced with this!. (his is li3e a code signing 3e) specific to the computer )ou are developing on. (he steps to generate the api3e) are: 1 0ocate the de2ug.3e)store file on )our computer. On windows 9' this is located in: +::5ocuments and Settings:()our account name!:.android 1 +op) the de2ug.3e)store file to the android tools folder in the location for )our S5; on the machine. 1 -rom the tools folder run the command : keytool ,list ,alias androiddebugkey ,storepass android ,keypass android ,keystore debug.keystore 1 Once )ou hit the enter 3e)$ )ou*ll see something li3e this (the actual M5< that we*re interested in is the last line!: androiddebugkey6 Mar >76 ?77@6 .rivateAey2ntry6 Certi#icate #ingerprint &M5B); 5>;>C;DA;B5;EF;EF;AD;BC;@5;C5;@A;DD;A?;CC;>>;AC 1 /ow open )our 2rowser and go to the following url... http://code.google.com/intl/%a/android/maps1api1signup.html 1 Accept the agreement and paste the md< that the 3e) tool returned to )ou$ in m) case it was something li3e... 5=:=>:?A:@5:A3:A3:A?:<>:95:+5:9A:??:A:>+:==:A+ (hen press B&enerate A'. ;e)B and )ou*ll 2e redirected to a page that has )our api 3e). /ow that )ou have the api 3e)$ )ou can use it in the main.xml la)out file of )our Map(est application. Cere*s what mine loo3s li3e: <?xml version="1.0" encoding="utf-8"?> <&inear&ayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="0ertica(" android:layout#+idt-="fi((_parent" android:layout#-eig-t="fi((_parent" >
map=ie& B @)ap=ie&A find=ie&7*d@-.id.map0ie&1A< mc B map=ie&.getContro((er@A< F %#0erride protected boolean is-outeDisp(a*ed@A > // TODO Auto-generated method stu' return false< F
private class )*"ocation"istener implements "ocation"istener > %#0erride public void on"ocationChanged@"ocation (ocA > if @(oc GB nullA > $oast.make$e3t@get7aseConte3t@AE ""ocation changed : "at: " 1 (oc.get"atitude@A 1 " "ng: " 1 (oc.get"ongitude@AE $oast."E!.$,_S,#-$A.sho&@A<
.eo/oint p B new .eo/oint@ @intA @(oc.get"atitude@A ? 1EHAE @intA @(oc.get"ongitude@A ? 1EHAA< mc.animate$o@pA< mc.set5oom@1HA< map=ie&.in0a(idate@A< F F %#0erride public void on/ro0iderDisa'(ed@String pro0iderA > // TODO Auto-generated method stu' F %#0erride public void on/ro0iderEna'(ed@String pro0iderA > // TODO Auto-generated method stu' F %#0erride public void onStatusChanged@String pro0iderE int statusE 7und(e e3trasA > // TODO Auto-generated method stu' F F F (n the aove. when a location changes. the latitude and longitude is sent to the +oogle Maps application. which then displa*s the map of the current location Accessing Accelerometer information on your Android Phone (Modified from original source at: http://2log.androgames.net/D</android1accelerometer1tutorial/ http://mo2ilestrategist.2logspot.com/E=E/E=/android1accelerometer1and1orientation.html ! An instance of the SensorManager is re4uired in order to retrieve informations a2out the supported sensors. /o permission is re4uired to access the sensor service. .t is then possi2le to retrieve the list of availa2le sensors of a certain t)pe. -or an accelerometer sensor$ the t)pe to use is given 2) the Sensor.(6'#,A++#0#FOM#(#F constant. .f at least one Sensor exists$ it is possi2le to register a Sensor#vent0istener for a Sensor of the list. .t is possi2le to specif) the delivering rate for sensor events. Specified rate must 2e one of : SensorManager.S#/SOF,5#0A6,-AS(#S( : as fast as possi2le SensorManager.S#/SOF,5#0A6,&AM# : rate suita2le for game SensorManager.S#/SOF,5#0A6,/OFMA0 : normal rate SensorManager.S#/SOF,5#0A6,". : rate suita2le for ". (hread 6ou need to first specif) all the varia2les in the main.xml file which form the &". of )our accelerometer application: <?xml version="1.0" encoding="utf-8"?> <(able&ayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout#+idt-="fi((_parent" android:layout#-eig-t="fi((_parent" android:stretc-Columns="1"> <(able/o+> <(ext*ie+ android:id="%1id/acce(erometer_(a'e(" android:layout#column="1" android:text="Acce(erometer" android:text"i0e="9pt" android:padding="Idip" /> </(able/o+>
<(able/o+> <(ext*ie+ android:id="%1id/orient_3_(a'e(" android:layout#column="1" android:text="8:" android:text"i0e="8pt" android:padding="Idip" /> <(ext*ie+ android:id="%1id/orient_3_0a(ue" android:gravity="right" android:text"i0e="8pt" android:padding="Idip" /> </(able/o+> <(able/o+> <(ext*ie+ android:id="%1id/orient_*_(a'e(" android:layout#column="1" android:text="J:" android:text"i0e="8pt" android:padding="Idip" /> <(ext*ie+ android:id="%1id/orient_*_0a(ue" android:gravity="right" android:text"i0e="8pt" android:padding="Idip" /> </(able/o+> <(able/o+> <(ext*ie+ android:id="%1id/orient_:_(a'e(" android:layout#column="1" android:text="5:" android:text"i0e="8pt" android:padding="Idip" /> <(ext*ie+ android:id="%1id/orient_:_0a(ue" android:gravity="right" android:text"i0e="8pt" android:padding="Idip" /> </(able/o+> </(able&ayout> .n the case of a Sensor#vent triggered 2) a Sensor of t)pe Sensor. (6'#,A++#0#FOM#(#F$ the eventGs values represents the acceleration of the phone given 2) a vector in a cartesian coordinate s)stem. 0anding on a ta2le$ the values returned 2) the Sensor#vent for the phone should 2e : =. E m/s along x axis . E m/s along ) axis 3. 9$DE>>< m/s along H axis -rom an event to another$ the coordinates of the acceleration vector are stored to detect sudden acceleration changes and to trigger a sha3e event when the threshold is reached. Others events could 2e implemented such as the detection of up and down gestures$ circular gestures and lot more. As usual$ it is prefera2le to register listeners in the onFesume(! method of the Activit) and removed in the on-inish(! method of the Activit). (hen add the following code to )our activit) class 2efore compiling on )our android device (note that accelerometer code will onl) wor3 for a device and not the emulator!. package android.accelerometer; import android.app.Activity; import android."ardware.(ensor; import android."ardware.(ensor2vent; import android."ardware.(ensor2ventListener; import android."ardware.(ensorManager; import android.os.Bundle; import android.widget.Text*iew; public class accelerometer extends Activity implements (ensor2ventListener {
Accelerometer G6 H6 and I values private Text*iew accelG*alue; private Text*iew accelH*alue; private Text*iew accelI*alue;
%rientation G6 H6 and I values private Text*iew orientG*alue; private Text*iew orientH*alue; private Text*iew orientI*alue;
private (ensorManager sensorManager / null;
!! Called w"en t"e activity is #irst created. ! $%verride public void onCreate&Bundle saved'nstance(tate) { super.onCreate&saved'nstance(tate); -et a re#erence to a (ensorManager sensorManager / &(ensorManager) get(ystem(ervice&(20(%+1(2+*'C2); setContent*iew&+.layout.main);
Capture accelerometer related view elements accelG*alue / &Text*iew) #ind*iewBy'd&+.id.accel1x1value); accelH*alue / &Text*iew) #ind*iewBy'd&+.id.accel1y1value); accelI*alue / &Text*iew) #ind*iewBy'd&+.id.accel1J1value);
Capture orientation related view elements orientG*alue / &Text*iew) #ind*iewBy'd&+.id.orient1x1value); orientH*alue / &Text*iew) #ind*iewBy'd&+.id.orient1y1value); orientI*alue / &Text*iew) #ind*iewBy'd&+.id.orient1J1value);
'nitialiJe accelerometer related view elements accelG*alue.setText&:7.77:); accelH*alue.setText&:7.77:); accelI*alue.setText&:7.77:);
'nitialiJe orientation related view elements orientG*alue.setText&:7.77:); orientH*alue.setText&:7.77:); orientI*alue.setText&:7.77:); 8
T"is met"od will update t"e 4' on new sensor events public void on(ensorC"anged&(ensor2vent sensor2vent) { synchronized &this) { if &sensor2vent.sensor.getType&) // (ensor.TH.21ACC2L2+%M2T2+) { accelG*alue.setText&Kloat.to(tring&sensor2vent.valuesL7M)); accelH*alue.setText&Kloat.to(tring&sensor2vent.valuesL>M)); accelI*alue.setText&Kloat.to(tring&sensor2vent.valuesL?M)); 8