Android gets to its current location via GPS

Source: Internet
Author: User

Positioning GPS positioning and base station positioning: GPS positioning is based on satellite positioning, the need to open the head, positioning error in 2-10 meters, the base station network positioning is based on distance from the mobile phone positioning, the error is large, as long as the mobile phone has a signal can be located; Getting GPS information is divided into the following steps:

1) access to location-related services is obtained through the context

Manager = (Locationmanager) context.getsystemservice (Context.location_service);

2) Select one of the best positioning methods in the current state GPS positioning or base station positioning

/** *  * @param manager Location Management Service * @return Best location provider    //GPS//wifi//*/private String Getprovider (Locationmanager man Ager) {//a set of query conditions criteria = new criteria (); criteria.setaccuracy (criteria.accuracy_fine);//get precise position, Criteria.setaltituderequired (false);//not sensitive to elevation criteria.setpowerrequirement (criteria.power_medium);// Power consumption medium criteria.setspeedrequired (TRUE);//Speed Change sensitive criteria.setcostallowed (true);//Generate overhead  communication cost//Return the best location provider   True indicates that only the currently opened location device return  Manager.getbestprovider (criteria, true) is returned;}


3) Location of the listener

4) Register Location Listener

The code is as follows:

/** * Ensure that there is only one instance of this Class 1 privatization construction method (we cannot instantiate an object of this class externally, only the object of a person class can be instantiated inside the Class) * 2 provides a static method for returning instances * * */public class Gpsinfoprovider {Locationmanager manager;//single private static Gpsinfoprovider mgpsinfoprovider;private  Static Context context;//single case private static Myloactionlistener listener;  1. Privatization construction Method Private Gpsinfoprovider () {}; 2. Provides a static method to return an instance of him to ensure that the code must be executed to complete the synchronized gpsinfoproviderpublic static synchronized Gpsinfoprovider Getinstanc E (Context context) {if (mgpsinfoprovider==null) {mgpsinfoprovider = new Gpsinfoprovider (); Gpsinfoprovider.context = context;} return mgpsinfoprovider;} Get GPS Information public String getLocation () {//Get location-related service services are obtained through the context of manager = (Locationmanager) context.getsystemservice (context.location_service);//Get all the positioning methods//manager.getallproviders ();  GPS//wifi////Select one of the best positioning methods in the current state String Provider = Getprovider (manager);//Register the location of the listener/** *provider positioning mode with what device to locate the base station network GPS AGPS * Time GPS how long time to regain the position minimum of 1 minutes * position the shortest displacement position change how much regainTake the position *listener position changes when the corresponding callback method */manager.requestlocationupdates (provider,60000, Getlistener ()); Sharedpreferences sp = context.getsharedpreferences ("config", context.mode_private); String location = sp.getstring (' Location ', ' ""); return location;} Stop GPS monitoring public void Stopgpslistener () {///parameter is Locationlistenermanager.removeupdates (Getlistener ());} Returns listener instance private synchronized Myloactionlistener Getlistener () {if (listener==null) {listener = new Myloactionlistener ();} return listener;} Private class Myloactionlistener Implements locationlistener{/** * method called when the phone location changes */public void onlocationchanged ( Location location) {String latitude = "latitude" + location.getlatitude (); Weidu String longtitude = "Longtitude" + location.getlongitude (); Jingdusharedpreferences sp = context.getsharedpreferences ("config", context.mode_private); Editor editor = Sp.edit (); Editor.putstring ("Location", latitude+ "-" + longtitude); Editor.commit (); The last acquired location information is stored inside the sharedpreference}/** * Status of a certain device occursChange when call available---unavailable---available GPS is available */public void onstatuschanged (String provider, int status, Bun Dle extras) {}/** * a device is turned on the GPS is turned on */public void onproviderenabled (String provider) {}/** a device disabled by GPS disabled * */publi c void onproviderdisabled (String provider) {}}/** * * @param manager Location Management Service * @return Best location Provider//GPS//wifi//*/priv Ate String Getprovider (Locationmanager Manager) {//a set of query criteria criteria = new criteria (); Criteria.setaccuracy ( Criteria.accuracy_fine); Get precise position, criteria.setaltituderequired (false),//not sensitive to altitude criteria.setpowerrequirement (criteria.power_medium);// Power consumption medium criteria.setspeedrequired (TRUE);//Speed Change sensitive criteria.setcostallowed (true);//Generate overhead communication cost//return best location Provider true Indicates that only the currently opened location device return Manager.getbestprovider (criteria, true) is returned;}}


 

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.