How Android obtains latitude and longitude

Source: Internet
Author: User

Mobile is the biggest feature of mobile phones and handheld devices, you can simulate the location change of the device through the eclipse's DDMS view, change the latitude and longitude, click Send, then run the program, in the application, dynamically get the device location, and then display the current location information.
  
Getting location information is divided into three steps:
  
1. Add system permissions to support access to the LBS hardware
  
Java code:

< uses-permission android:name= "Android.permission.ACCESS_FINE_LOCATION" ></uses-permission>

2. Locationmanager objects that get system services
  
Java code:

Locationmanager Loctionmanager; String contextservice=context.location_service; // through the system service, obtains the Locationmanager object loctionmanager= (Locationmanager) Getsystemservice (Contextservice);

3. Get the location provider, through the location provider, to get location information, you can specify a specific location provider, you can also provide a standard set, so that the system according to the criteria to match the most suitable location provider, location information is provided by the location.

A. Obtaining a location via the GPS location provider (specifying a specific location provider)

Java code:

String provider== loctionmanager.getlastknownlocation (provider);

B. Use a standard collection, which allows the system to automatically select the best available location provider, providing a location

Java code:

Criteria =NewCriteria (); criteria.setaccuracy (criteria.accuracy_fine);//High PrecisionCriteria.setaltituderequired (false);//elevation not requiredCriteria.setbearingrequired (false);//No azimuth requiredCriteria.setcostallowed (true);//allowed to be spentCriteria.setpowerrequirement (Criteria.power_low);//Low power consumption//The best provider to match the above criteria from the available location providersString Provider = Loctionmanager.getbestprovider (criteria,true);//get the position of the last changeLocation location = loctionmanager.getlastknownlocation (provider);


Finally, the location information is displayed in the TextView,

Change of listening position

Monitor position change, 2 seconds, distance 10 meters
Java code:

Loctionmanager.requestlocationupdates (Provider, 2000, 10, Locationlistener);//Position ListenerPrivate FinalLocationlistener Locationlistener =NewLocationlistener () {@Override Public voidOnstatuschanged (String provider,intstatus, Bundle extras) {} @Override Public voidonproviderenabled (String provider) {} @Override Public voidonproviderdisabled (String provider) {}//triggered when position changes@Override Public voidonlocationchanged (location location) {//update TextView display with the new locationupdatewithnewlocation (location);}};

How Android obtains latitude and longitude

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.