Skip to content

Commit

Permalink
Add support for desiredAccuracy on Android
Browse files Browse the repository at this point in the history
  • Loading branch information
matt-oakes committed Dec 29, 2018
1 parent 96865d8 commit 89fc408
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 37 deletions.
9 changes: 6 additions & 3 deletions README.md
Expand Up @@ -116,12 +116,15 @@ You can call `configure` multiple times at it will only change the setting which

```javascript
RNLocation.configure({
distanceFilter: 0,
distanceFilter: 100, // Meters
desiredAccuracy: {
ios: "best",
android: "balancedPowerAccuracy"
},
// iOS Only
activityType: "other",
allowsBackgroundLocationUpdates: false,
desiredAccuracy: "best",
headingFilter: 1,
headingFilter: 1, // Degrees
headingOrientation: "portrait",
pausesLocationUpdatesAutomatically: false,
showsBackgroundLocationIndicator: false,
Expand Down
Expand Up @@ -70,6 +70,37 @@ public void configure(final Activity activity, final ReadableMap options, final
}
}

// Priority
if (options.hasKey("desiredAccuracy")) {
if (options.getType("desiredAccuracy") == ReadableType.Map) {
ReadableMap desiredAccuracy = options.getMap("desiredAccuracy");
if (desiredAccuracy.hasKey("android")) {
if (desiredAccuracy.getType("android") == ReadableType.String) {
String desiredAccuracyAndroid = desiredAccuracy.getString("android");
if (desiredAccuracyAndroid.equals("balancedPowerAccuracy")) {
locationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
hasChanges = true;
} else if (desiredAccuracyAndroid.equals("highAccuracy")) {
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
hasChanges = true;
} else if (desiredAccuracyAndroid.equals("lowPower")) {
locationRequest.setPriority(LocationRequest.PRIORITY_LOW_POWER);
hasChanges = true;
} else if (desiredAccuracyAndroid.equals("noPower")) {
locationRequest.setPriority(LocationRequest.PRIORITY_NO_POWER);
hasChanges = true;
} else {
Utils.emitWarning(context, "desiredAccuracy.android was passed an unknown value: " + desiredAccuracyAndroid, "401");
}
} else {
Utils.emitWarning(context, "desiredAccuracy.android must be a string", "401");
}
}
} else {
Utils.emitWarning(context, "desiredAccuracy must be an object", "401");
}
}

// Return early if no changes were made
if (!hasChanges) {
promise.resolve(null);
Expand Down
24 changes: 9 additions & 15 deletions example/App.js
Expand Up @@ -46,8 +46,8 @@ export default class App extends React.PureComponent {

_startUpdatingLocation = () => {
this.locationSubscription = RNLocation.subscribeToLocationUpdates(
location => {
this.setState({ location });
locations => {
this.setState({ location: locations[0] });
}
);
};
Expand Down Expand Up @@ -101,21 +101,21 @@ export default class App extends React.PureComponent {
<View style={[styles.detailBox, styles.third]}>
<Text style={styles.valueTitle}>Course</Text>
<Text style={[styles.detail, styles.largeDetail]}>
{location.coords.course}
{location.course}
</Text>
</View>

<View style={[styles.detailBox, styles.third]}>
<Text style={styles.valueTitle}>Speed</Text>
<Text style={[styles.detail, styles.largeDetail]}>
{location.coords.speed}
{location.speed}
</Text>
</View>

<View style={[styles.detailBox, styles.third]}>
<Text style={styles.valueTitle}>Altitude</Text>
<Text style={[styles.detail, styles.largeDetail]}>
{location.coords.altitude}
{location.altitude}
</Text>
</View>
</View>
Expand All @@ -124,31 +124,25 @@ export default class App extends React.PureComponent {
<View style={styles.row}>
<View style={[styles.detailBox, styles.half]}>
<Text style={styles.valueTitle}>Latitude</Text>
<Text style={styles.detail}>
{location.coords.latitude}
</Text>
<Text style={styles.detail}>{location.latitude}</Text>
</View>

<View style={[styles.detailBox, styles.half]}>
<Text style={styles.valueTitle}>Longitude</Text>
<Text style={styles.detail}>
{location.coords.longitude}
</Text>
<Text style={styles.detail}>{location.longitude}</Text>
</View>
</View>

<View style={styles.row}>
<View style={[styles.detailBox, styles.half]}>
<Text style={styles.valueTitle}>Accuracy</Text>
<Text style={styles.detail}>
{location.coords.accuracy}
</Text>
<Text style={styles.detail}>{location.accuracy}</Text>
</View>

<View style={[styles.detailBox, styles.half]}>
<Text style={styles.valueTitle}>Altitude Accuracy</Text>
<Text style={styles.detail}>
{location.coords.altitudeAccuracy}
{location.altitudeAccuracy}
</Text>
</View>
</View>
Expand Down
31 changes: 17 additions & 14 deletions ios/RNLocation.m
Expand Up @@ -141,17 +141,20 @@ - (void)stopObserving
}

// Desired accuracy
NSString *desiredAccuracy = [RCTConvert NSString:options[@"desiredAccuracy"]];
if ([desiredAccuracy isEqualToString:@"bestForNavigation"]) {
self.locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
} else if ([desiredAccuracy isEqualToString:@"best"]) {
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
} else if ([desiredAccuracy isEqualToString:@"nearestTenMeters"]) {
self.locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
} else if ([desiredAccuracy isEqualToString:@"hundredMeters"]) {
self.locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;
} else if ([desiredAccuracy isEqualToString:@"threeKilometers"]) {
self.locationManager.desiredAccuracy = kCLLocationAccuracyThreeKilometers;
NSDictionary *desiredAccuracy = [RCTConvert NSDictionary:options[@"desiredAccuracy"]];
if (desiredAccuracy != nil) {
NSString *desiredAccuracyIOS = [RCTConvert NSString:desiredAccuracy[@"ios"]];
if ([desiredAccuracyIOS isEqualToString:@"bestForNavigation"]) {
self.locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
} else if ([desiredAccuracyIOS isEqualToString:@"best"]) {
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
} else if ([desiredAccuracyIOS isEqualToString:@"nearestTenMeters"]) {
self.locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
} else if ([desiredAccuracyIOS isEqualToString:@"hundredMeters"]) {
self.locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;
} else if ([desiredAccuracyIOS isEqualToString:@"threeKilometers"]) {
self.locationManager.desiredAccuracy = kCLLocationAccuracyThreeKilometers;
}
}

// Distance filter
Expand All @@ -171,11 +174,11 @@ - (void)stopObserving
NSString *headingOrientation = [RCTConvert NSString:options[@"headingOrientation"]];
if ([headingOrientation isEqualToString:@"portrait"]) {
self.locationManager.headingOrientation = CLDeviceOrientationPortrait;
} else if ([desiredAccuracy isEqualToString:@"portraitUpsideDown"]) {
} else if ([headingOrientation isEqualToString:@"portraitUpsideDown"]) {
self.locationManager.headingOrientation = CLDeviceOrientationPortraitUpsideDown;
} else if ([desiredAccuracy isEqualToString:@"landscapeLeft"]) {
} else if ([headingOrientation isEqualToString:@"landscapeLeft"]) {
self.locationManager.headingOrientation = CLDeviceOrientationLandscapeLeft;
} else if ([desiredAccuracy isEqualToString:@"landscapeRight"]) {
} else if ([headingOrientation isEqualToString:@"landscapeRight"]) {
self.locationManager.headingOrientation = CLDeviceOrientationLandscapeRight;
}

Expand Down
24 changes: 19 additions & 5 deletions src/types.ts
Expand Up @@ -16,11 +16,21 @@ export type LocationActivityType =
| "otherNavigation"
| "airborne"; // iOS 12+
/**
* The accuracy of a geographical coordinate.
* The accuracy of the location responses for Android.
* @platform ios
* @see [Apple Docs](https://developer.apple.com/documentation/corelocation/cllocationaccuracy?language=objc)
*/
export type LocationAccuracy =
export type LocationPriorityAndroid =
| "balancedPowerAccuracy"
| "highAccuracy"
| "lowPower"
| "noPower";
/**
* The accuracy of a geographical coordinate for iOS.
* @platform ios
* @see [Apple Docs](https://developer.apple.com/documentation/corelocation/cllocationaccuracy?language=objc)
*/
export type LocationAccuracyIOS =
| "bestForNavigation"
| "best"
| "nearestTenMeters"
Expand Down Expand Up @@ -132,11 +142,15 @@ export interface ConfigureOptions {
*/
allowsBackgroundLocationUpdates?: boolean | void;
/**
* The accuracy of the location data. Defaults to `best`.
* @platform ios
* The accuracy of the location data. Defaults to `best` on iOS and `balancedPowerAccuracy` on Android.
* @platform android ios
* @see [Apple Docs](https://developer.apple.com/documentation/corelocation/cllocationmanager/1423836-desiredaccuracy)
* @see [Android Docs](https://developers.google.com/android/reference/com/google/android/gms/location/LocationRequest.html#setPriority(int))
*/
desiredAccuracy?: LocationAccuracy | void;
desiredAccuracy?: {
ios?: LocationAccuracyIOS | void;
android?: LocationPriorityAndroid | void;
} | void;
/**
* The minimum angle in degrees that the device heading needs to change before the heading update callback in your app is called. Defaults to `0` for no filtering.
* @platform ios
Expand Down

0 comments on commit 89fc408

Please sign in to comment.