index.jd revision f013e1afd1e68af5e3b868c26a653bbfb39538f8
1page.title=Location 2@jd:body 3 4<div id="qv-wrapper"> 5<div id="qv"> 6 7 <h2>In this document</h2> 8 <ol> 9 <li><a href="#location">android.location</a></li> 10 <li><a href="#maps">com.google.android.maps</a></li> 11 </ol> 12</div> 13</div> 14 15<p>The Android SDK includes two packages that provide Android's primary support 16for building location-based services: 17{@link android.location} and {@link-fixme com.google.android.maps}. 18Please read on below for a brief introduction to each package.</p> 19 20<h2 id="location">android.location</h2> 21 22<p>This package contains several classes related to 23location services in the Android platform. Most importantly, it introduces the 24{@link android.location.LocationManager} 25service, which provides an API to determine location and bearing if the 26underlying device (if it supports the service). The LocationManager 27should <strong>not</strong> be 28instantiated directly; rather, a handle to it should be retrieved via 29{@link android.content.Context#getSystemService(String) 30getSystemService(Context.LOCATION_SERVICE)}.</p> 31 32<p>Once your application has a handle to the LocationManager, your application 33will be able to do three things:</p> 34 35<ul> 36 <li>Query for the list of all LocationProviders known to the 37 LocationManager for its last known location.</li> 38 <li>Register/unregister for periodic updates of current location from a 39 LocationProvider (specified either by Criteria or name).</li> 40 <li>Register/unregister for a given Intent to be fired if the device comes 41 within a given proximity (specified by radius in meters) of a given 42 lat/long.</li> 43</ul> 44 45<p>However, during initial development, you may not have access to real 46data from a real location provider (Network or GPS). So it may be necessary to 47spoof some data for your application, with some mock location data.</p> 48 49<p class="note"><strong>Note:</strong> If you've used mock LocationProviders in 50previous versions of the SDK (m3/m5), you can no longer provide canned LocationProviders 51in the /system/etc/location directory. These directories will be wiped during boot-up. 52Please follow the new procedures below.</p> 53 54 55<h3>Providing Mock Location Data</h3> 56 57<p>When testing your application on the Android emulator, there are a couple different 58ways to send it some spoof location data: with the DDMS tool or the "geo" command.</p> 59 60<h4 id="ddms">Using DDMS</h4> 61<p>With the DDMS tool, you can simulate location data a few different ways:</p> 62<ul> 63 <li>Manually send individual longitude/latitude coordinates to the device.</li> 64 <li>Use a GPX file describing a route for playback to the device.</li> 65 <li>Use a KML file describing individual placemarks for sequenced playback to the device.</li> 66</ul> 67<p>For more information on using DDMS to spoof location data, see the 68<a href="{@docRoot}guide/developing/tools/ddms.html#emulator-control">Using DDMS guide</a>. 69 70<h4 id="geo">Using the "geo" command</h4> 71<p>Launch your application in the Android emulator and open a terminal/console in 72your SDK's <code>/tools</code> directory. Now you can use:</p> 73<ul><li><code>geo fix</code> to send a fixed geo-location. 74 <p>This command accepts a longitude and latitude in decimal degrees, and 75 an optional altitude in meters. For example:</p> 76 <pre>geo fix -121.45356 46.51119 4392</pre> 77 </li> 78 <li><code>geo nmea</code> to send an NMEA 0183 sentence. 79 <p>This command accepts a single NMEA sentence of type '$GPGGA' (fix data) or '$GPRMC' (transit data). 80 For example:</p> 81 <pre>geo nmea $GPRMC,081836,A,3751.65,S,14507.36,E,000.0,360.0,130998,011.3,E*62</pre> 82 </li> 83</ul> 84 85 86<h2 id="maps">com.google.android.maps</h2> 87 88<p>This package introduces a number of classes related to 89rendering, controlling, and overlaying customized information on your own 90Google Mapified Activity. The most important of which is the 91{@link-fixme com.google.android.maps.MapView} class, which automagically draws you a 92basic Google Map when you add a MapView to your layout. Note that, if you 93want to do so, then your Activity that handles the 94MapView must extend {@link-fixme com.google.android.maps.MapActivity}. </p> 95 96<p>Also note that you must obtain a MapView API Key from the Google Maps 97service, before your MapView can load maps data. For more information, see 98<a href="{@docRoot}guide/developing/mapkey.html">Obtaining a MapView API Key</a>.</p> 99 100<p>Once you've created a MapView, you'll probably want to use 101{@link-fixme com.google.android.maps.MapView#getController()} to 102retrieve a {@link-fixme com.google.android.maps.MapController}, for controlling and 103animating the map, and {@link-fixme com.google.android.maps.ItemizedOverlay} to 104draw {@link-fixme com.google.android.maps.Overlay}s and other information on the Map.</p> 105 106<p>This is not a standard package in the Android library. In order to use it, you must add the following node to your Android Manifest file, as a child of the 107<code><application></code> element:</p> 108<pre><uses-library android:name="com.google.android.maps" /></pre> 109 110