mirror of
https://github.com/status-im/react-native.git
synced 2025-02-26 08:05:34 +00:00
On Android geolocationError
return PositionError object.
Summary: Follow iOS implementation: https://github.com/facebook/react-native/blob/master/Libraries/Geolocation/RCTLocationObserver.m#L325 and FIX #9710 Closes https://github.com/facebook/react-native/pull/9734 Differential Revision: D3819868 Pulled By: mkonicek fbshipit-source-id: 3deb7d3d253d402bfcb88c6a94ca705128998748
This commit is contained in:
parent
1a7c231ef3
commit
99d742c0b7
@ -59,7 +59,7 @@ class GeolocationExample extends React.Component {
|
||||
var initialPosition = JSON.stringify(position);
|
||||
this.setState({initialPosition});
|
||||
},
|
||||
(error) => alert(error),
|
||||
(error) => alert(JSON.stringify(error)),
|
||||
{enableHighAccuracy: true, timeout: 20000, maximumAge: 1000}
|
||||
);
|
||||
this.watchID = navigator.geolocation.watchPosition((position) => {
|
||||
|
@ -9,8 +9,6 @@
|
||||
|
||||
package com.facebook.react.modules.location;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import android.content.Context;
|
||||
import android.location.Location;
|
||||
import android.location.LocationListener;
|
||||
@ -19,10 +17,9 @@ import android.location.LocationProvider;
|
||||
import android.os.Bundle;
|
||||
import android.os.Handler;
|
||||
|
||||
import com.facebook.react.bridge.Callback;
|
||||
import com.facebook.react.bridge.Arguments;
|
||||
import com.facebook.react.bridge.Callback;
|
||||
import com.facebook.react.bridge.ReactApplicationContext;
|
||||
import com.facebook.react.bridge.ReactContext;
|
||||
import com.facebook.react.bridge.ReactContextBaseJavaModule;
|
||||
import com.facebook.react.bridge.ReactMethod;
|
||||
import com.facebook.react.bridge.ReadableMap;
|
||||
@ -31,6 +28,8 @@ import com.facebook.react.common.SystemClock;
|
||||
import com.facebook.react.module.annotations.ReactModule;
|
||||
import com.facebook.react.modules.core.DeviceEventManagerModule.RCTDeviceEventEmitter;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
/**
|
||||
* Native module that exposes Geolocation to JS.
|
||||
*/
|
||||
@ -50,9 +49,9 @@ public class LocationModule extends ReactContextBaseJavaModule {
|
||||
@Override
|
||||
public void onStatusChanged(String provider, int status, Bundle extras) {
|
||||
if (status == LocationProvider.OUT_OF_SERVICE) {
|
||||
emitError("Provider " + provider + " is out of service.");
|
||||
emitError(PositionError.POSITION_UNAVAILABLE, "Provider " + provider + " is out of service.");
|
||||
} else if (status == LocationProvider.TEMPORARILY_UNAVAILABLE) {
|
||||
emitError("Provider " + provider + " is temporarily unavailable.");
|
||||
emitError(PositionError.TIMEOUT, "Provider " + provider + " is temporarily unavailable.");
|
||||
}
|
||||
}
|
||||
|
||||
@ -158,7 +157,7 @@ public class LocationModule extends ReactContextBaseJavaModule {
|
||||
(LocationManager) getReactApplicationContext().getSystemService(Context.LOCATION_SERVICE);
|
||||
String provider = getValidProvider(locationManager, locationOptions.highAccuracy);
|
||||
if (provider == null) {
|
||||
emitError("No location provider available.");
|
||||
emitError(PositionError.PERMISSION_DENIED, "No location provider available.");
|
||||
return;
|
||||
}
|
||||
if (!provider.equals(mWatchedProvider)) {
|
||||
@ -218,9 +217,9 @@ public class LocationModule extends ReactContextBaseJavaModule {
|
||||
return map;
|
||||
}
|
||||
|
||||
private void emitError(String error) {
|
||||
private void emitError(int code, String message) {
|
||||
getReactApplicationContext().getJSModule(RCTDeviceEventEmitter.class)
|
||||
.emit("geolocationError", error);
|
||||
.emit("geolocationError", PositionError.buildError(code, message));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -246,7 +245,7 @@ public class LocationModule extends ReactContextBaseJavaModule {
|
||||
public void run() {
|
||||
synchronized (SingleUpdateRequest.this) {
|
||||
if (!mTriggered) {
|
||||
mError.invoke("Location request timed out");
|
||||
mError.invoke(PositionError.buildError(PositionError.TIMEOUT, "Location request timed out"));
|
||||
mLocationManager.removeUpdates(mLocationListener);
|
||||
mTriggered = true;
|
||||
}
|
||||
|
@ -0,0 +1,45 @@
|
||||
/**
|
||||
* Copyright (c) 2015-present, Facebook, Inc.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This source code is licensed under the BSD-style license found in the
|
||||
* LICENSE file in the root directory of this source tree. An additional grant
|
||||
* of patent rights can be found in the PATENTS file in the same directory.
|
||||
*/
|
||||
|
||||
package com.facebook.react.modules.location;
|
||||
|
||||
import com.facebook.react.bridge.Arguments;
|
||||
import com.facebook.react.bridge.WritableMap;
|
||||
|
||||
/**
|
||||
* @see {https://developer.mozilla.org/en-US/docs/Web/API/PositionError}
|
||||
*/
|
||||
public class PositionError {
|
||||
/**
|
||||
* The acquisition of the geolocation information failed because
|
||||
* the page didn't have the permission to do it.
|
||||
*/
|
||||
public static int PERMISSION_DENIED = 1;
|
||||
|
||||
/**
|
||||
* The acquisition of the geolocation failed because at least one
|
||||
* internal source of position returned an internal error.
|
||||
*/
|
||||
public static int POSITION_UNAVAILABLE = 2;
|
||||
|
||||
/**
|
||||
* The time allowed to acquire the geolocation, defined by
|
||||
* PositionOptions.timeout information was reached before the information was obtained.
|
||||
*/
|
||||
public static int TIMEOUT = 3;
|
||||
|
||||
public static WritableMap buildError(int code, String message) {
|
||||
WritableMap error = Arguments.createMap();
|
||||
error.putInt("code", code);
|
||||
if (message != null) {
|
||||
error.putString("message", message);
|
||||
}
|
||||
return error;
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user