Solution:
Achieve different accuracy of Android positioning (based on network and GPS)
The related classes for location services in Android are basically in the Android.location package, where the Location Service Manager (Locationmanager) provides the APIs needed to locate the feature, and here's a key part of implementing the positioning method:
1. The method of instantiating the location Service Manager is as follows:
//Variable definition
private Locationmanager Locationmanager;
//Get Locationmanager
Locationmanager = (locationmanager) this
. Getsystemservice (context.location_service);
2, open the location service monitoring
with Locationmanager, we can start listening to changes in position. We use the methods in Locationmanager:
Locationmanager.requestlocationupdates (String provider, long mintime, float mindistance, Locationlistener Listener)
(The above parameters indicate:
Parameters
Provider the name of the provider with which to register
Mintime the minimum time interval for notifications, in milliseconds. This field was only used as a hint to conserve power, and actual time between location updates could be greater or lesser tha n this value.
Mindistance the minimum distance interval for notifications, in meters
listener a {#link locationlistener} whose onlocationchanged (location) method would be called for each location update)
to set up the listener. Notice the 1th parameter, which has a value of 2 and 1, respectively:
Locationmanager.network_provider
Locationmanager.gps_provider
The former is used in the mobile network to obtain the location, low precision but fast, the latter using GPS positioning, high precision but generally takes 10-60 seconds to start the 1th time positioning, if it is in the interior is basically unable to locate.
turn on position change monitor code:
private Locationlistener gpslistener=null;
private Locationlistener networklistner=null;
private void Registerlocationlistener () {
networklistner=new Mylocationlistner ();
locationmanager.requestlocationupdates (locationmanager.network_provider, 0, Networklistner);
gpslistener=new Mylocationlistner ();
locationmanager.requestlocationupdates (locationmanager.gps_provider, 0, Gpslistener);
}
using Getlastknownlocation (String provider) to pass the corresponding parameters, the location obtained is not the current GPS location information, but the last obtained position information, And Requestlocationupdates is really the update to request location information.
3, location query conditions such as precision setting method
private Criteria Getcriteria () {
Criteria criteria=new criteria ();
//Set positioning accuracy criteria.accuracy_coarse relatively rough, criteria.accuracy_fine is relatively fine
criteria.setaccuracy (criteria.accuracy_fine);//High Precision
//Set whether speed is required
criteria.setspeedrequired (false);
//Set whether carrier charges are allowed
criteria.setcostallowed (false);
//Set if orientation information is required
criteria.setbearingrequired (false);
//Set whether altitude information is required
criteria.setaltituderequired (false);
//Set demand for power supply
criteria.setpowerrequirement (Criteria.power_low);//low power consumption
return criteria;
}
4. Stop position Change monitoring
You can stop listening only if you call the Removeupdates (Locationlistener Listener) method of the Locationmanager object.
Reference Links:
http://blog.sina.com.cn/s/blog_74c22b210100sfix.html
http://fzlihui.iteye.com/blog/713050
http://www.learningandroid.net/blog/foundation/tutorial-location-service/
http://www.cnblogs.com/jh5240/archive/2012/09/08/2676863.html
Achieve different accuracy of Android positioning (based on network and GPS)