michalchudziak / react-native-geolocation

Geolocation APIs for React Native
MIT License
1.28k stars 220 forks source link

Default to PlayServicesLocationManager in case of 'auto' #244

Closed ifarhanpatel closed 1 year ago

ifarhanpatel commented 1 year ago

In the following code block, in the case of auto we default to AndroidLocationProvider instead of Playservices. We have encountered instances where AndroidLocationProvider does not return any location(even when google maps can accurately can). Since FusedLocationProvider is the latest API, we can default to PlayServices if it is available on the device.

 private void onConfigurationChange(Configuration config) {
    ReactApplicationContext reactContext = mLocationManager.mReactContext;
    if (Objects.equals(config.locationProvider, "android") && mLocationManager instanceof PlayServicesLocationManager) {
      mLocationManager = new AndroidLocationManager(reactContext);
    } else if (Objects.equals(config.locationProvider, "playServices") && mLocationManager instanceof AndroidLocationManager) {
      GoogleApiAvailability availability = new GoogleApiAvailability();
      if (availability.isGooglePlayServicesAvailable(reactContext.getApplicationContext()) == ConnectionResult.SUCCESS) {
        mLocationManager = new PlayServicesLocationManager(reactContext);
      }
    }
  }

A newer Code block would look something like:

private void onConfigurationChange(Configuration config) {
    ReactApplicationContext reactContext = mLocationManager.mReactContext;
    if (Objects.equals(config.locationProvider, "auto") && mLocationManager instanceof AndroidLocationManager && isGooglePlayServicesAvailable()) {
      mLocationManager = new PlayServicesLocationManager(reactContext);
    } else if (Objects.equals(config.locationProvider, "android") && mLocationManager instanceof PlayServicesLocationManager) {
      mLocationManager = new AndroidLocationManager(reactContext);
    } else if (Objects.equals(config.locationProvider, "playServices") && mLocationManager instanceof AndroidLocationManager) {
      if (isGooglePlayServicesAvailable) {
        mLocationManager = new PlayServicesLocationManager(reactContext);
      }
    }
  }