Android------beginner GPS Positioning

Source: Internet
Author: User
<span id="Label3"></p><p><p>GPS English is the abbreviation of Global Positioning System gps.</p></p><p><p>Android for GPS feature support specifically provides a locationmanager, location manager. All the services and objects related to GPS positioning will be generated by this Object.</p></p><p><p><br>Get Locationmanager instance:<br>Locationmanager lm = (locationmanager) Getsystemservice (context.location_service)</p></p><p><p></p></p><p><p>Three core api:locationmanager, locationprovider, location</p></p><p><p></p></p><p><p>Locationmanager provides the following methods:</p></p><p><p>Boolean Addgpsstatuslistener (gpsstatus.listener Listener): adds a listener that listens for GPs status;</p></p><p><p>void Addproximityalert (double latitude,double longitude,float radius,long expiration,pendingintent intent): adds a proximity warning;</p></p><p><p>List Getallproviders (): get all the Locationprovider lists;<br>String getbestprovider (criteria Criteria,boolean enabledonly): Returns the optimal Locationprovider object according to the established conditions;</p></p><p><p>Gpsstatus getgpsstatus (gpsstatus status): get GPS status;</p></p><p><p>Location Getlastknownlocation (String provider): Gets the last known location based on locationprovider;</p></p><p><p>Locationprovider getprovider (String name): gets Locationprovider by name;</p></p><p><p>List getproviders (criteria Criteria,boolean enabledonly): The names of all locationprovier that satisfy the conditions are obtained according to the conditions of establishment;<br>List getproviders (boolean enabledonly): gets all available locationprovider;<br>Boolean isproviderenabled (String provider): determines whether the locationprovider of the established name is available;</p></p><p><p>void Removegpsstatuslistener (gpsstatus.listener Listener): Delete GPS status listener;</p></p><p><p>void Removeproximityalert (pendingintent intent): deletes an approaching warning;</p></p><p><p>void Requestlocationupdates (String provider,long mintime,float mindistance,pendingintent intent): The location information is obtained periodically through the specified locationprovider, and the corresponding components are started by intent;</p></p><p><p>void Requestlocationupdates (String provider,long mintime,float mindistance,lcoationlistener listener): Obtains the location information periodically through the specified locationprovider, and triggers the listener corresponding trigger;</p></p><p><p></p></p><p><p>Locationprovider class</p></p><p><p>The abstract identification of the positioning component, through which the relevant information of positioning can be obtained;</p></p><p><p>The following common methods are available:</p></p><p><p>String getName (): Returns the name of the locationprovider;</p></p><p><p>int getaccuracy (): Returns the precision of the locationprovider;</p></p><p><p>int getpowerrequirement (): Returns the power requirement of the locationprovider;</p></p><p><p>Boolean hasmonetarycost (): returns whether the Locationprovider is charged or free;</p></p><p><p>Boolean meetscriteria: determines whether the locationprovider satisfies the criteria criteria;</p></p><p><p>Boolean Requirescell (): determines whether the Locationprovider needs to access the network base station;</p></p><p><p>Boolean Requiresnetword (): Determines whether the Locationprovider requires network data;</p></p><p><p>Boolean Requiresstatellite (): determine if the Locationprovider requires access to the Satellite's positioning system;</p></p><p><p>Boolean Supportsaltitude (): determines whether the Locationprovider supports height information;</p></p><p><p>Boolean supportsbearing (): determines whether the Locationprovider supports directional information;</p></p><p><p>Boolean Supportsspeed (): determines whether the Locationprovider supports speed information;</p></p><p><p><br>Locationlistener: position listener, monitor position change, Monitor device switch and status</p></p><p><p></p></p><p><p>Location class</p></p><p><p>An abstract class representing location information;</p></p><p><p>Provides the following methods to obtain location information:</p></p><p><p>Float getaccuracy (): Get the accuracy of positioning information;</p></p><p><p>Double Getaltitude (): Gets the height of the positioning information;</p></p><p><p>Float getbearing (): Gets the orientation of the positioning information;</p></p><p><p>Double Getlatitude (): Gets the longitude of the location information;</p></p><p><p>Double Getlongitude (): Gets the latitude of the location information;</p></p><p><p>String Getprovider (): Gets the Locationprovider that provides the location information;</p></p><p><p>Float GetSpeed (): Gets the speed of positioning information;</p></p><p><p>Boolean hasaccuracy (): determines whether the location information has longitude information;</p></p><p><p>Boolean Hasaltitude (): determines whether the location information has high information;</p></p><p><p>Boolean hasbearing (): determines whether the location information has direction information;</p></p><p><p>Boolean Hasspeed (): determines whether the location information has speed information;</p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p><br>Locationlistener: position listener, monitor position change, Monitor device switch and status</p></p><p><p></p></p><p><p>Steps:<br>1. Get the Locationmanager object for the system<br>2. Use Locationmanager to obtain positioning information by specifying locationprovider, which is represented by the location object<br>3. Get positioning information from the Location object</p></p><p><p></p></p><p><p></p></p><p><p>Here are a few examples:</p></p><p><p>1. Get all the available locationprovider:</p></p><p><p>Layout file:</p></p><p><p></p></p><pre class="html" name="code"><pre class="html" name="code"><linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools " android:layout_width=" fill_parent " android:layout_height=" fill_parent " android:orientation= "vertical" android:paddingbottom= "@dimen/activity_vertical_margin" android: paddingleft= "@dimen/activity_horizontal_margin" android:paddingright= "@dimen/activity_horizontal_margin" android:paddingtop= "@dimen/activity_vertical_margin" tools:context= " Com.example.allproviderstest.AllProvidersTest "> <textview android:layout_width=" fill_parent " android:layout_height= "wrap_content" android:text= "@string/providerlist"/> <listview android:id= "@+id/providers" android:layout_width= "fill_parent" android:layout_height= "fill_ Parent "> </ListView></LinearLayout></pre></pre><p><p><br></p></p><p><p>Activity:</p></p><p><p></p></p><pre class="html" name="code"><pre class="html" name="code">public class Allproviderstest extends Activity {ListView providers; Locationmanager lm; @Overrideprotected void onCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_all_providers_test);p roviders = (ListView) Findviewbyid (r.id.providers); lm = ( Locationmanager) Getsystemservice (context.location_service); list<string> providernames = lm.getallproviders (); arrayadapter<string> adapter = new Arrayadapter<string> (this,android. r.layout.simple_list_item_1, providernames);p Roviders.setadapter (adapter);}}</pre></pre><p><p></p></p><p><p><br>All available Locationprovider in the simulator are Two:<br>Passive: represented by the Locationprovider.passive_provider constant<br>Gps: represented by the Locationprovider.gps_provider Constant. Represents the Locationprovider object for locating information via GPS</p></p><p><p>There is also a locationprovider called NETWORK, which is represented by the Locationprovider.network_provider constant,<br>Represents a Locationprovider object that obtains location information through a mobile communication Network.</p></p><p><p></p></p><p><p></p></p><p><p>2. Obtain the specified Locationprovider by name:</p></p><p><p></p></p><p><p>For example:</p></p><p><p>Get gps-based Locationprovider:<br>Locationprovider Locprovider = Lm.getprovider (locationmanager.gps_provider)</p></p><p><p></p></p><p><p>Get Locationprovider According to Criteria:</p></p><p><p></p></p><p><p></p></p><p><p>Layout file:</p></p><p><p></p></p><pre class="html" name="code"><pre class="html" name="code"><linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools " android:layout_width=" fill_parent " android:layout_height=" fill_parent " android:orientation= "vertical" android:paddingbottom= "@dimen/activity_vertical_margin" android: paddingleft= "@dimen/activity_horizontal_margin" android:paddingright= "@dimen/activity_horizontal_margin" android:paddingtop= "@dimen/activity_vertical_margin" tools:context= " Com.example.freeproviderstest.FreeProvidersTest "> <textview android:layout_width=" fill_parent " android:layout_height= "wrap_content" android:text= "@string/providerlist"/> <listview android:id= "@+id/providers" android:layout_width= "fill_parent" android:layout_height= "fill_ Parent "/></linearlayout></pre></pre><p><p><br></p></p><p><p>Activity:</p></p><p><p></p></p><pre class="html" name="code"><pre class="html" name="code">public class Freeproviderstest extends Activity {ListView providers; Locationmanager lm; @Overrideprotected void onCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_free_providers_test);p roviders = (ListView) Findviewbyid (r.id.providers); lm = ( Locationmanager) Getsystemservice (context.location_service);//create A locationprovider filter criteria for the criteria CRI = new criteria ( )///setup requires Locationprovider to be free. Cri.setcostallowed (false);//setup requires Locationprovider to provide height information cri.setaltituderequired (true);// Setup requires Locationprovider to provide direction information cri.setbearingrequired (true);//gets the name of the Locationprovider for all compound conditions of the system list<string> Providernames = Lm.getproviders (cri, false); System.out.println (providernames.size ()); arrayadapter<string> adapter = new Arrayadapter<string> (this,android. r.layout.simple_list_item_1, Providernames);//use a ListView to display all available Locationproviderproviders.setadapter (adapter);}}</pre></pre><p><p><br></p></p><p><p></p></p><p><p>3.<br>Get Location Data:</p></p><p><p><br>Send GPS information via simulator:</p></p><p><p>After you start the emulator, you can send GPS location information in the emulator Control panel under Ddms.</p></p><p><p></p></p><p><p>Layout file:</p></p><p><p></p></p><pre class="html" name="code"><pre class="html" name="code"><linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools " android:layout_width=" fill_parent " android:layout_height=" fill_parent " android:orientation= "vertical" android:paddingbottom= "@dimen/activity_vertical_margin" android: paddingleft= "@dimen/activity_horizontal_margin" android:paddingright= "@dimen/activity_horizontal_margin" android:paddingtop= "@dimen/activity_vertical_margin" tools:context= " Com.example.locationtest.LocationTest "> <edittext android:id=" @+id/show " android:layout_ Width= "fill_parent" android:layout_height= "wrap_content" android:cursorvisible= "false" android: Editable= "false"/></linearlayout></pre></pre><p><p><br></p></p><p><p>Activity:</p></p><pre class="html" name="code">public class Locationtest extends Activity {//define Locationmanager object Locationmanager locmanager;// Defines the EditText component in the program interface EditText show; @Overrideprotected void onCreate (Bundle Savedinstancestate) {super.oncreate ( savedinstancestate); Setcontentview (r.layout.activity_location_test); show = (EditText) Findviewbyid (R.id.show); Locmanager = (locationmanager) Getsystemservice (context.location_service);//obtain Nearest nearest location information from GPS Locmanager.getlastknownlocation (locationmanager.gps_provider);//use Location According to the EditText display Updateview (location);// Set the GPS location information to get once every 3 seconds locmanager.requestlocationupdates (locationmanager.gps_provider, 3000,8, new Locationlistener ()// ①{@Overridepublic void onlocationchanged {//when GPS location information is changed, update location updateview;} @Overridepublic void onproviderdisabled (String provider) {updateview (null);} @Overridepublic void onproviderenabled (String provider) {//when The GPS Locationprovider is available, update the location Updateview ( Locmanager.getlastknownlocation (provider));} @Overridepublic void Onstatuschanged (String provider, int status,bundle Extras) {}});} Update EditText content that is displayed public void Updateview (location newlocation) {if (newlocation! = null) {StringBuilder sb = new Stringbu Ilder (); sb.append ("real-time location information: \ n"), sb.append ("longitude:"); sb.append (newlocation.getlongitude ()); sb.append ("\ n latitude:"); Sb.append (newlocation.getlatitude ()); sb.append ("\ n height:"); sb.append (newlocation.getaltitude ()); sb.append ("\ n speed:" ); sb.append (newlocation.getspeed ()); sb.append ("\ n direction:"); sb.append (newlocation.getbearing ()); show.settext ( Sb.tostring ());} else {//if the incoming location object is empty then empty Edittextshow.settext ("");}}</pre><p><p><br><br></p></p><p><p></p></p><p><p>4.<br>Imminent warning:<br>by Locationmanager.addproximityalert (double latitude,double longitude, float radius, long expiration, pendingintent Intent) Add a proximity warning</p></p><p><p>Parameters:</p></p><p><p>Latitude: Specifies the longitude of the fixed point<br>longitude: Specifies the latitude of a fixed point<br>Radius: radius length<br>Expiration: This parameter specifies the number of milliseconds after which the proximity warning Expires.<br>Intent: This parameter specifies the component that the intent corresponds to when it is near the fixed point.</p></p><p><p><br></p></p><p><p>Layout file:</p></p><p><p></p></p><pre class="html" name="code"><pre class="html" name="code"><linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools " android:layout_width=" fill_parent " android:layout_height=" fill_parent " android:orientation= "vertical" android:paddingbottom= "@dimen/activity_vertical_margin" android: paddingleft= "@dimen/activity_horizontal_margin" android:paddingright= "@dimen/activity_horizontal_margin" android:paddingtop= "@dimen/activity_vertical_margin" tools:context= " Com.example.proximitytest.ProximityTest "> <textview android:layout_width=" fill_parent " android:layout_height= "wrap_content" android:text= "@string/hello"/></linearlayout></pre></pre><p><p><br></p></p><p><p>Activity:</p></p><p><p></p></p><pre class="html" name="code"><pre class="html" name="code">public class Proximitytest extends Activity {@Overrideprotected void OnCreate (Bundle Savedinstancestate) { Super.oncreate (savedinstancestate); setcontentview (r.layout.activity_proximity_test);//location Service constant String locService = Context.location_service;//location Service Manager Instance Locationmanager locationmanager;// Obtain Locationmanager Instance Locationmanager = (locationmanager) getsystemservice (locService) by Getsystemservice method;// Define the approximate longitude and latitude of zibo, shandong, double longitude = 117.3;double latitude = 36.5;//defined radius (5 km) Float radius = 5000;//definition intentintent Intent = n EW Intent (this, proximityalertreciever.class);//package Intent as pendingintentpendingintent pi = Pendingintent.getbroadcast (this,-1, intent, 0);//add proximity warning Locationmanager.addproximityalert (latitude, longitude, radius,-1, pi);}}</pre></pre><p><p><br></p></p><p><p>Proximityalertreciever:</p></p><p><p></p></p><pre class="html" name="code"><pre class="html" name="code">public class Proximityalertreciever extends broadcastreceiver {@Overridepublic void onreceive (context context, Intent Intent) {//gets Whether to enter a specified range of Boolean isenter = Intent.getbooleanextra (locationmanager.key_proximity_entering, false); Isenter) {//display Prompt message Toast.maketext (context, "you have entered Tianhe district, guangzhou", toast.length_long). show ();} Else {//display the prompt message Toast.maketext ( context, "you have left Guangzhou Tianhe district", toast.length_long). Show ();}}}</pre></pre><p><p><br></p></p><p><p>Android------beginner GPS Positioning</p></p></span>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.