0

I'm using following code to get position:

LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
String bestProvider = locationManager.getBestProvider(criteria, false);
Location location = locationManager.getLastKnownLocation(bestProvider);
Double lat = location.getLatitude ();
Double lon = location.getLongitude ();

I have so many questions:

  1. If GPS is turned off, is it uses network location? If no, how to implement that?

  2. If GPS is enabled, is it always uses it ignoring network location? If no, how to implement that?

  3. Is it uses, if available, the most precise method to get latitude and longitude? If no, how to implement that?

Thanks in advance for clarifications.

Bob
  • 436
  • 5
  • 13

1 Answers1

0

You can create a Location Listener which can be used to answer question one. The location listener can respond to location change, status change, if the provider is enabled or disabled.

  1. For your first question, if you create a LocationListener for a GPS Location Manager, you can use public void onProviderDisabled(String provider) {} to attempt to use network Location instead.

    // Acquire a reference to the system Location Manager
    LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
    
    // Define a listener that responds to location updates
    LocationListener locationListener = new LocationListener() {
        public void onLocationChanged(Location location) {
          // Called when a new location is found by the network location provider.
          makeUseOfNewLocation(location);
        }
    
        public void onStatusChanged(String provider, int status, Bundle extras) {}
    
        public void onProviderEnabled(String provider) {}
    
        public void onProviderDisabled(String provider) {}
    
      };
    
    // Register the listener with the Location Manager to receive location updates
    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
    
  2. You are able to use both GPS and network location, however it is normally more suitable to decide which will be better in your situation and weight the pros and cons of each. (There are many StackOverflow pages, here is one Android Location Providers - GPS or Network Provider?)

  3. Similar to #2, you should be able to track either GPS and/or Network Location, and generate two values of their accuracy and compare and use which is most accurate for you.

I would strongly suggest looking over the Developer API guides for more information. (http://developer.android.com/guide/topics/location/strategies.html)

Community
  • 1
  • 1
Mark N
  • 326
  • 2
  • 13
  • The same way you declared for GPS location, create another for Network Location. You can use a boolean or something and check whether GPS provider is enabled or not (by using the locationLisener onProviderDisabled(String provider)). Then check this boolean to see if you should use your Network Location manager for the location data. – Mark N Aug 08 '14 at 15:26