Here is my GPS location code, the outdoor can generally get the current latitude and longitude, please heroes inner chamber to more blocks to get the current position of a! Not my own use public comment or Sina Weibo, in the interior can be faster to get the current position! Please heroes wing!
/ / location listener
locationListener = new LocationListener () {
public void onStatusChanged (String provider, int status, Bundle extras)
{
}
public void onProviderEnabled (String provider)
{
}
public void onProviderDisabled (String provider) {
}
/ / triggered when the location changes
public void onLocationChanged (Location location)
{
/ / use the new location update TextView display
updateLocation (location);
}
};
String contextService = Context.LOCATION_SERVICE;
/ / through the system services, access LocationManager object
lm = (LocationManager) getSystemService (contextService);
LoginStateSetting lss = (LoginStateSetting) getApplicationContext ();
location = lss.getLocation ();
if (location! = null)
{
updateLocation (location);
}
else if (isGPSEnable (this))
{
/ / use the standard set, so that the system automatically selects the best available location provider that provides location
Criteria criteria = new Criteria ();
criteria.setAccuracy (Criteria.ACCURACY_FINE) ;/ / precision
criteria.setAltitudeRequired (false) ;/ / do not require elevation
criteria.setBearingRequired (false) ;/ / do not require orientation
criteria.setCostAllowed (true) ;/ / is allowed to spend
criteria.setPowerRequirement (Criteria.POWER_LOW) ;/ / Low Power
try
{
/ / from the available location provider in matching the above criteria best provider
String provider = lm.getBestProvider (criteria, true);
/ / get the last change location
location = lm.getLastKnownLocation (provider);
/ / displayed in the TextView
updateLocation (location);
/ / listening position changed once every 2 seconds, more than 10 meters away
lm.requestLocationUpdates (provider, 1000, 0, locationListener);
}
catch (Exception e)
{
e.printStackTrace ();
}
}
else
{
Toast.makeText (this, "Please start the GPS, and then view nearby businesses!", Toast.LENGTH_SHORT). show ();
/ / into the GPS settings page
Intent intent = new Intent ();
intent.setAction (Settings.ACTION_LOCATION_SOURCE_SETTINGS);
intent.setFlags (Intent.FLAG_ACTIVITY_NEW_TASK);
try
{
startActivityForResult (intent, 0); / / This is the setting for the interface returns to
}
catch (ActivityNotFoundException ex)
{
intent.setAction (Settings.ACTION_SETTINGS);
try
{
startActivity (intent);
}
catch (Exception e) {
}
}
}
/ / update the location information to the tv show in
private void updateLocation (Location location) {
if (location! = null)
{
LoginStateSetting lss = (LoginStateSetting) getApplicationContext ();
lss.setLocation (location);
Log.i ("gps succ", "gps succ");
Log.i ("start parser", "parser location to street");
double latitude = location.getLatitude ();
double longitude = location.getLongitude ();
/ / because the address resolution time-consuming, so open an asynchronous thread to resolve addresses
String [] params = {latitude + "", longitude + ""};
new AsyncAddrParser (). execute (params);
}
else
{
System.out.println ("did not get to the positioning object Location");
}
}
------ Solution --------------------------------- -----------
This experience is very important ~ ~ LZ-sharing is like
------ reference ------------------------- --------------
two days finally solved the problem of GPS indoor positioning, speed control in less than 3s, using Baidu mobile maps api, locate the idea is to first using GPS positioning, if the GPS can not get on the use NET_WORK to locate, if net_work not get, on the use of base station location, combining these three ways, will be able to get your current location as latitude and longitude location!
------ reference --------------------------------------- < br> Thank you for sharing By the way the landlord indoor positioning accuracy is about how much ah
------ reference --------- ------------------------------
I made a listening event but did not always update gps
lm = (LocationManager) context.getSystemService (Context.LOCATION_SERVICE);
currentLocation = lm.getLastKnownLocation (LocationManager.GPS_PROVIDER);
updateView (currentLocation);
lm.requestLocationUpdates (LocationManager.GPS_PROVIDER, 1000, 0, gpsListener);
private LocationListener gpsListener = new LocationListener () {
/ **
* triggered when changing the location information
* /
@ Override
public void onLocationChanged (Location location) {
if (isBetterLocation (location, currentLocation)) {
currentLocation = location;
updateView (location);
Toast.makeText (context, "location information changes trigger", Toast.LENGTH_LONG). show ();
}
}
/ **
* GPS is disabled when the trigger
* /
@ Override
public void onProviderDisabled (String provider) {
updateView (null);
}
/ **
* GPS is turned on trigger
* /
@ Override
public void onProviderEnabled (String provider) {
Location location = lm.getLastKnownLocation (provider);
updateView (location);
}
/ **
* GPS triggered when the state changes
* /
@ Override
public void onStatusChanged (String provider, int status, Bundle extras) {
}
};
how to solve
------ reference ---------------------------------- -----
I also use this method, first using gps positioning, but there is a problem, by the outdoor to indoor, gps positioning latitude and longitude appear normal but the show is the last location latitude and longitude , how to monitor it from the outdoors indoors, we need a network positioning it?
------ reference --------------------------------------- < br>
how can I judge fails to obtain the GPS coordinates it?
没有评论:
发表评论