Quantcast
Channel: Active questions tagged react-native+android - Stack Overflow
Viewing all articles
Browse latest Browse all 29452

How to use mock location in React Native - Android

$
0
0

I'm trying to write react native module for changing GPS location in android studio. I'm setting permission and set application name in mock location developer settings. After that also, its not working and also not getting any error. These is my sample code,

    @ReactMethod
    private Context mContext;
    public void mockLocation(String gps, Callback callback) {
        LocationManager mLocationManager = (LocationManager)mContext.getSystemService(Context.LOCATION_SERVICE);
          mLocationManager.removeTestProvider(LocationManager.GPS_PROVIDER);
          mLocationManager.addTestProvider
                  (
                          LocationManager.GPS_PROVIDER,
                          "requiresNetwork" == "",
                          "requiresSatellite" == "",
                          "requiresCell" == "",
                          "hasMonetaryCost" == "",
                          "supportsAltitude" == "",
                          "supportsSpeed" == "",
                          "supportsBearing" == "",

                          android.location.Criteria.POWER_LOW,
                          android.location.Criteria.ACCURACY_FINE
                  );

          Location newLocation = new Location(LocationManager.GPS_PROVIDER);

          newLocation.setLatitude (22.5726);
          newLocation.setLongitude(88.3639);

          newLocation.setAccuracy(500);

          mLocationManager.setTestProviderEnabled
                  (
                          LocationManager.GPS_PROVIDER,
                          true
                  );

          mLocationManager.setTestProviderStatus
                  (
                          LocationManager.GPS_PROVIDER,
                          LocationProvider.AVAILABLE,
                          null,
                          System.currentTimeMillis()
                  );

          mLocationManager.setTestProviderLocation
                  (
                          LocationManager.GPS_PROVIDER,
                          newLocation
                  );
        callback.invoke("Location changed");
      } 

And my react code is,

componentDidMount() {
    RNNativeToastLibrary.mockLocation("gps", (res) => {
      console.log(res)
    });
  }

Viewing all articles
Browse latest Browse all 29452

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>