mirror of
https://github.com/zhigang1992/react-native.git
synced 2026-03-26 07:04:05 +08:00
[ReactNative][tmp] Rename GeoLocation2 files back to Geolocation
This commit is contained in:
98
Libraries/Geolocation/Geolocation.ios.js
Normal file
98
Libraries/Geolocation/Geolocation.ios.js
Normal file
@@ -0,0 +1,98 @@
|
||||
/**
|
||||
* Copyright 2004-present Facebook. All Rights Reserved.
|
||||
*
|
||||
* @providesModule Geolocation
|
||||
*/
|
||||
'use strict';
|
||||
|
||||
var RCTDeviceEventEmitter = require('RCTDeviceEventEmitter');
|
||||
var RCTLocationObserver = require('NativeModulesDeprecated').RKLocationObserver;
|
||||
|
||||
var invariant = require('invariant');
|
||||
var logError = require('logError');
|
||||
var warning = require('warning');
|
||||
|
||||
var subscriptions = [];
|
||||
|
||||
var updatesEnabled = false;
|
||||
|
||||
/**
|
||||
* /!\ ATTENTION /!\
|
||||
* You need to add NSLocationWhenInUseUsageDescription key
|
||||
* in Info.plist to enable geolocation, otherwise it's going
|
||||
* to *fail silently*!
|
||||
* \!/ \!/
|
||||
*
|
||||
* Geolocation follows the MDN specification:
|
||||
* https://developer.mozilla.org/en-US/docs/Web/API/Geolocation
|
||||
*/
|
||||
var Geolocation = {
|
||||
|
||||
getCurrentPosition: function(geo_success, geo_error, geo_options) {
|
||||
invariant(
|
||||
typeof geo_success === 'function',
|
||||
'Must provide a valid geo_success callback.'
|
||||
);
|
||||
RCTLocationObserver.getCurrentPosition(
|
||||
geo_success,
|
||||
geo_error || logError,
|
||||
geo_options || {}
|
||||
);
|
||||
},
|
||||
|
||||
watchPosition: function(success, error, options) {
|
||||
if (!updatesEnabled) {
|
||||
RCTLocationObserver.startObserving(options || {});
|
||||
updatesEnabled = true;
|
||||
}
|
||||
var watchID = subscriptions.length;
|
||||
subscriptions.push([
|
||||
RCTDeviceEventEmitter.addListener(
|
||||
'geolocationDidChange',
|
||||
success
|
||||
),
|
||||
error ? RCTDeviceEventEmitter.addListener(
|
||||
'geolocationError',
|
||||
error
|
||||
) : null,
|
||||
]);
|
||||
return watchID;
|
||||
},
|
||||
|
||||
clearWatch: function(watchID) {
|
||||
var sub = subscriptions[watchID];
|
||||
if (!sub) {
|
||||
// Silently exit when the watchID is invalid or already cleared
|
||||
// This is consistent with timers
|
||||
return;
|
||||
}
|
||||
sub[0].remove();
|
||||
sub[1] && sub[1].remove();
|
||||
subscriptions[watchID] = undefined;
|
||||
var noWatchers = true;
|
||||
for (var ii = 0; ii < subscriptions.length; ii++) {
|
||||
if (subscriptions[ii]) {
|
||||
noWatchers = false; // still valid subscriptions
|
||||
}
|
||||
}
|
||||
if (noWatchers) {
|
||||
Geolocation.stopObserving();
|
||||
}
|
||||
},
|
||||
|
||||
stopObserving: function() {
|
||||
if (updatesEnabled) {
|
||||
RCTLocationObserver.stopObserving();
|
||||
updatesEnabled = false;
|
||||
for (var ii = 0; ii < subscriptions.length; ii++) {
|
||||
if (subscriptions[ii]) {
|
||||
warning('Called stopObserving with existing subscriptions.');
|
||||
subscriptions[ii].remove();
|
||||
}
|
||||
}
|
||||
subscriptions = [];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Geolocation;
|
||||
256
Libraries/Geolocation/RCTGeolocation.xcodeproj/project.pbxproj
Normal file
256
Libraries/Geolocation/RCTGeolocation.xcodeproj/project.pbxproj
Normal file
@@ -0,0 +1,256 @@
|
||||
// !$*UTF8*$!
|
||||
{
|
||||
archiveVersion = 1;
|
||||
classes = {
|
||||
};
|
||||
objectVersion = 46;
|
||||
objects = {
|
||||
|
||||
/* Begin PBXBuildFile section */
|
||||
134814061AA4E45400B7C361 /* RCTLocationObserver.m in Sources */ = {isa = PBXBuildFile; fileRef = 134814051AA4E45400B7C361 /* RCTLocationObserver.m */; };
|
||||
/* End PBXBuildFile section */
|
||||
|
||||
/* Begin PBXCopyFilesBuildPhase section */
|
||||
58B511D91A9E6C8500147676 /* CopyFiles */ = {
|
||||
isa = PBXCopyFilesBuildPhase;
|
||||
buildActionMask = 2147483647;
|
||||
dstPath = "include/$(PRODUCT_NAME)";
|
||||
dstSubfolderSpec = 16;
|
||||
files = (
|
||||
);
|
||||
runOnlyForDeploymentPostprocessing = 0;
|
||||
};
|
||||
/* End PBXCopyFilesBuildPhase section */
|
||||
|
||||
/* Begin PBXFileReference section */
|
||||
134814041AA4E45400B7C361 /* RCTLocationObserver.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RCTLocationObserver.h; sourceTree = "<group>"; };
|
||||
134814051AA4E45400B7C361 /* RCTLocationObserver.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RCTLocationObserver.m; sourceTree = "<group>"; };
|
||||
134814201AA4EA6300B7C361 /* libRCTGeolocation.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libRCTGeolocation.a; sourceTree = BUILT_PRODUCTS_DIR; };
|
||||
/* End PBXFileReference section */
|
||||
|
||||
/* Begin PBXFrameworksBuildPhase section */
|
||||
58B511D81A9E6C8500147676 /* Frameworks */ = {
|
||||
isa = PBXFrameworksBuildPhase;
|
||||
buildActionMask = 2147483647;
|
||||
files = (
|
||||
);
|
||||
runOnlyForDeploymentPostprocessing = 0;
|
||||
};
|
||||
/* End PBXFrameworksBuildPhase section */
|
||||
|
||||
/* Begin PBXGroup section */
|
||||
134814211AA4EA7D00B7C361 /* Products */ = {
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
134814201AA4EA6300B7C361 /* libRCTGeolocation.a */,
|
||||
);
|
||||
name = Products;
|
||||
sourceTree = "<group>";
|
||||
};
|
||||
58B511D21A9E6C8500147676 = {
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
134814041AA4E45400B7C361 /* RCTLocationObserver.h */,
|
||||
134814051AA4E45400B7C361 /* RCTLocationObserver.m */,
|
||||
134814211AA4EA7D00B7C361 /* Products */,
|
||||
);
|
||||
sourceTree = "<group>";
|
||||
};
|
||||
/* End PBXGroup section */
|
||||
|
||||
/* Begin PBXNativeTarget section */
|
||||
58B511DA1A9E6C8500147676 /* RCTGeolocation */ = {
|
||||
isa = PBXNativeTarget;
|
||||
buildConfigurationList = 58B511EF1A9E6C8500147676 /* Build configuration list for PBXNativeTarget "RCTGeolocation" */;
|
||||
buildPhases = (
|
||||
58B511D71A9E6C8500147676 /* Sources */,
|
||||
58B511D81A9E6C8500147676 /* Frameworks */,
|
||||
58B511D91A9E6C8500147676 /* CopyFiles */,
|
||||
);
|
||||
buildRules = (
|
||||
);
|
||||
dependencies = (
|
||||
);
|
||||
name = RCTGeolocation;
|
||||
productName = RCTDataManager;
|
||||
productReference = 134814201AA4EA6300B7C361 /* libRCTGeolocation.a */;
|
||||
productType = "com.apple.product-type.library.static";
|
||||
};
|
||||
/* End PBXNativeTarget section */
|
||||
|
||||
/* Begin PBXProject section */
|
||||
58B511D31A9E6C8500147676 /* Project object */ = {
|
||||
isa = PBXProject;
|
||||
attributes = {
|
||||
LastUpgradeCheck = 0610;
|
||||
ORGANIZATIONNAME = Facebook;
|
||||
TargetAttributes = {
|
||||
58B511DA1A9E6C8500147676 = {
|
||||
CreatedOnToolsVersion = 6.1.1;
|
||||
};
|
||||
};
|
||||
};
|
||||
buildConfigurationList = 58B511D61A9E6C8500147676 /* Build configuration list for PBXProject "RCTGeolocation" */;
|
||||
compatibilityVersion = "Xcode 3.2";
|
||||
developmentRegion = English;
|
||||
hasScannedForEncodings = 0;
|
||||
knownRegions = (
|
||||
en,
|
||||
);
|
||||
mainGroup = 58B511D21A9E6C8500147676;
|
||||
productRefGroup = 58B511D21A9E6C8500147676;
|
||||
projectDirPath = "";
|
||||
projectRoot = "";
|
||||
targets = (
|
||||
58B511DA1A9E6C8500147676 /* RCTGeolocation */,
|
||||
);
|
||||
};
|
||||
/* End PBXProject section */
|
||||
|
||||
/* Begin PBXSourcesBuildPhase section */
|
||||
58B511D71A9E6C8500147676 /* Sources */ = {
|
||||
isa = PBXSourcesBuildPhase;
|
||||
buildActionMask = 2147483647;
|
||||
files = (
|
||||
134814061AA4E45400B7C361 /* RCTLocationObserver.m in Sources */,
|
||||
);
|
||||
runOnlyForDeploymentPostprocessing = 0;
|
||||
};
|
||||
/* End PBXSourcesBuildPhase section */
|
||||
|
||||
/* Begin XCBuildConfiguration section */
|
||||
58B511ED1A9E6C8500147676 /* Debug */ = {
|
||||
isa = XCBuildConfiguration;
|
||||
buildSettings = {
|
||||
ALWAYS_SEARCH_USER_PATHS = NO;
|
||||
CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x";
|
||||
CLANG_CXX_LIBRARY = "libc++";
|
||||
CLANG_ENABLE_MODULES = YES;
|
||||
CLANG_ENABLE_OBJC_ARC = YES;
|
||||
CLANG_WARN_BOOL_CONVERSION = YES;
|
||||
CLANG_WARN_CONSTANT_CONVERSION = YES;
|
||||
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
|
||||
CLANG_WARN_EMPTY_BODY = YES;
|
||||
CLANG_WARN_ENUM_CONVERSION = YES;
|
||||
CLANG_WARN_INT_CONVERSION = YES;
|
||||
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
|
||||
CLANG_WARN_UNREACHABLE_CODE = YES;
|
||||
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
|
||||
COPY_PHASE_STRIP = NO;
|
||||
ENABLE_STRICT_OBJC_MSGSEND = YES;
|
||||
GCC_C_LANGUAGE_STANDARD = gnu99;
|
||||
GCC_DYNAMIC_NO_PIC = NO;
|
||||
GCC_OPTIMIZATION_LEVEL = 0;
|
||||
GCC_PREPROCESSOR_DEFINITIONS = (
|
||||
"DEBUG=1",
|
||||
"$(inherited)",
|
||||
);
|
||||
GCC_SYMBOLS_PRIVATE_EXTERN = NO;
|
||||
GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
|
||||
GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR;
|
||||
GCC_WARN_UNDECLARED_SELECTOR = YES;
|
||||
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
|
||||
GCC_WARN_UNUSED_FUNCTION = YES;
|
||||
GCC_WARN_UNUSED_VARIABLE = YES;
|
||||
IPHONEOS_DEPLOYMENT_TARGET = 8.1;
|
||||
MTL_ENABLE_DEBUG_INFO = YES;
|
||||
ONLY_ACTIVE_ARCH = YES;
|
||||
SDKROOT = iphoneos;
|
||||
};
|
||||
name = Debug;
|
||||
};
|
||||
58B511EE1A9E6C8500147676 /* Release */ = {
|
||||
isa = XCBuildConfiguration;
|
||||
buildSettings = {
|
||||
ALWAYS_SEARCH_USER_PATHS = NO;
|
||||
CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x";
|
||||
CLANG_CXX_LIBRARY = "libc++";
|
||||
CLANG_ENABLE_MODULES = YES;
|
||||
CLANG_ENABLE_OBJC_ARC = YES;
|
||||
CLANG_WARN_BOOL_CONVERSION = YES;
|
||||
CLANG_WARN_CONSTANT_CONVERSION = YES;
|
||||
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
|
||||
CLANG_WARN_EMPTY_BODY = YES;
|
||||
CLANG_WARN_ENUM_CONVERSION = YES;
|
||||
CLANG_WARN_INT_CONVERSION = YES;
|
||||
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
|
||||
CLANG_WARN_UNREACHABLE_CODE = YES;
|
||||
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
|
||||
COPY_PHASE_STRIP = YES;
|
||||
ENABLE_NS_ASSERTIONS = NO;
|
||||
ENABLE_STRICT_OBJC_MSGSEND = YES;
|
||||
GCC_C_LANGUAGE_STANDARD = gnu99;
|
||||
GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
|
||||
GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR;
|
||||
GCC_WARN_UNDECLARED_SELECTOR = YES;
|
||||
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
|
||||
GCC_WARN_UNUSED_FUNCTION = YES;
|
||||
GCC_WARN_UNUSED_VARIABLE = YES;
|
||||
IPHONEOS_DEPLOYMENT_TARGET = 8.1;
|
||||
MTL_ENABLE_DEBUG_INFO = NO;
|
||||
SDKROOT = iphoneos;
|
||||
VALIDATE_PRODUCT = YES;
|
||||
};
|
||||
name = Release;
|
||||
};
|
||||
58B511F01A9E6C8500147676 /* Debug */ = {
|
||||
isa = XCBuildConfiguration;
|
||||
buildSettings = {
|
||||
HEADER_SEARCH_PATHS = (
|
||||
"$(inherited)",
|
||||
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include,
|
||||
"$(SRCROOT)/../../ReactKit/**",
|
||||
);
|
||||
LIBRARY_SEARCH_PATHS = (
|
||||
"$(inherited)",
|
||||
"/Users/nicklockwood/fbobjc-hg/Libraries/FBReactKit/js/react-native-github/ReactKit/build/Debug-iphoneos",
|
||||
);
|
||||
OTHER_LDFLAGS = "-ObjC";
|
||||
PRODUCT_NAME = RCTGeolocation;
|
||||
SKIP_INSTALL = YES;
|
||||
};
|
||||
name = Debug;
|
||||
};
|
||||
58B511F11A9E6C8500147676 /* Release */ = {
|
||||
isa = XCBuildConfiguration;
|
||||
buildSettings = {
|
||||
HEADER_SEARCH_PATHS = (
|
||||
"$(inherited)",
|
||||
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include,
|
||||
"$(SRCROOT)/../../ReactKit/**",
|
||||
);
|
||||
LIBRARY_SEARCH_PATHS = (
|
||||
"$(inherited)",
|
||||
"/Users/nicklockwood/fbobjc-hg/Libraries/FBReactKit/js/react-native-github/ReactKit/build/Debug-iphoneos",
|
||||
);
|
||||
OTHER_LDFLAGS = "-ObjC";
|
||||
PRODUCT_NAME = RCTGeolocation;
|
||||
SKIP_INSTALL = YES;
|
||||
};
|
||||
name = Release;
|
||||
};
|
||||
/* End XCBuildConfiguration section */
|
||||
|
||||
/* Begin XCConfigurationList section */
|
||||
58B511D61A9E6C8500147676 /* Build configuration list for PBXProject "RCTGeolocation" */ = {
|
||||
isa = XCConfigurationList;
|
||||
buildConfigurations = (
|
||||
58B511ED1A9E6C8500147676 /* Debug */,
|
||||
58B511EE1A9E6C8500147676 /* Release */,
|
||||
);
|
||||
defaultConfigurationIsVisible = 0;
|
||||
defaultConfigurationName = Release;
|
||||
};
|
||||
58B511EF1A9E6C8500147676 /* Build configuration list for PBXNativeTarget "RCTGeolocation" */ = {
|
||||
isa = XCConfigurationList;
|
||||
buildConfigurations = (
|
||||
58B511F01A9E6C8500147676 /* Debug */,
|
||||
58B511F11A9E6C8500147676 /* Release */,
|
||||
);
|
||||
defaultConfigurationIsVisible = 0;
|
||||
defaultConfigurationName = Release;
|
||||
};
|
||||
/* End XCConfigurationList section */
|
||||
};
|
||||
rootObject = 58B511D31A9E6C8500147676 /* Project object */;
|
||||
}
|
||||
7
Libraries/Geolocation/RCTLocationObserver.h
Normal file
7
Libraries/Geolocation/RCTLocationObserver.h
Normal file
@@ -0,0 +1,7 @@
|
||||
// Copyright 2004-present Facebook. All Rights Reserved.
|
||||
|
||||
#import "RCTBridgeModule.h"
|
||||
|
||||
@interface RCTLocationObserver : NSObject<RCTBridgeModule>
|
||||
|
||||
@end
|
||||
319
Libraries/Geolocation/RCTLocationObserver.m
Normal file
319
Libraries/Geolocation/RCTLocationObserver.m
Normal file
@@ -0,0 +1,319 @@
|
||||
// Copyright 2004-present Facebook. All Rights Reserved.
|
||||
|
||||
#import "RCTLocationObserver.h"
|
||||
|
||||
#import <CoreLocation/CLError.h>
|
||||
#import <CoreLocation/CLLocationManager.h>
|
||||
#import <CoreLocation/CLLocationManagerDelegate.h>
|
||||
|
||||
#import "RCTAssert.h"
|
||||
#import "RCTBridge.h"
|
||||
#import "RCTConvert.h"
|
||||
#import "RCTEventDispatcher.h"
|
||||
#import "RCTLog.h"
|
||||
|
||||
typedef NS_ENUM(NSInteger, RCTPositionErrorCode) {
|
||||
RCTPositionErrorDenied = 1,
|
||||
RCTPositionErrorUnavailable,
|
||||
RCTPositionErrorTimeout,
|
||||
};
|
||||
|
||||
#define RCT_DEFAULT_LOCATION_ACCURACY kCLLocationAccuracyHundredMeters
|
||||
|
||||
typedef struct {
|
||||
NSTimeInterval timeout;
|
||||
NSTimeInterval maximumAge;
|
||||
CLLocationAccuracy accuracy;
|
||||
} RCTLocationOptions;
|
||||
|
||||
static RCTLocationOptions RCTLocationOptionsWithJSON(id json)
|
||||
{
|
||||
NSDictionary *options = [RCTConvert NSDictionary:json];
|
||||
return (RCTLocationOptions){
|
||||
.timeout = [RCTConvert NSTimeInterval:options[@"timeout"]] ?: INFINITY,
|
||||
.maximumAge = [RCTConvert NSTimeInterval:options[@"maximumAge"]] ?: INFINITY,
|
||||
.accuracy = [RCTConvert BOOL:options[@"enableHighAccuracy"]] ? kCLLocationAccuracyBest : RCT_DEFAULT_LOCATION_ACCURACY
|
||||
};
|
||||
}
|
||||
|
||||
static NSDictionary *RCTPositionError(RCTPositionErrorCode code, NSString *msg /* nil for default */)
|
||||
{
|
||||
if (!msg) {
|
||||
switch (code) {
|
||||
case RCTPositionErrorDenied:
|
||||
msg = @"User denied access to location services.";
|
||||
break;
|
||||
case RCTPositionErrorUnavailable:
|
||||
msg = @"Unable to retrieve location.";
|
||||
break;
|
||||
case RCTPositionErrorTimeout:
|
||||
msg = @"The location request timed out.";
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return @{
|
||||
@"code": @(code),
|
||||
@"message": msg,
|
||||
@"PERMISSION_DENIED": @(RCTPositionErrorDenied),
|
||||
@"POSITION_UNAVAILABLE": @(RCTPositionErrorUnavailable),
|
||||
@"TIMEOUT": @(RCTPositionErrorTimeout)
|
||||
};
|
||||
}
|
||||
|
||||
@interface RCTLocationRequest : NSObject
|
||||
|
||||
@property (nonatomic, copy) RCTResponseSenderBlock successBlock;
|
||||
@property (nonatomic, copy) RCTResponseSenderBlock errorBlock;
|
||||
@property (nonatomic, assign) RCTLocationOptions options;
|
||||
@property (nonatomic, strong) NSTimer *timeoutTimer;
|
||||
|
||||
@end
|
||||
|
||||
@implementation RCTLocationRequest
|
||||
|
||||
- (void)dealloc
|
||||
{
|
||||
[_timeoutTimer invalidate];
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
@interface RCTLocationObserver () <CLLocationManagerDelegate>
|
||||
|
||||
@end
|
||||
|
||||
@implementation RCTLocationObserver
|
||||
{
|
||||
CLLocationManager *_locationManager;
|
||||
NSDictionary *_lastLocationEvent;
|
||||
NSMutableArray *_pendingRequests;
|
||||
BOOL _observingLocation;
|
||||
RCTLocationOptions _observerOptions;
|
||||
}
|
||||
|
||||
@synthesize bridge = _bridge;
|
||||
|
||||
#pragma mark - Lifecycle
|
||||
|
||||
- (instancetype)init
|
||||
{
|
||||
if ((self = [super init])) {
|
||||
|
||||
_locationManager = [[CLLocationManager alloc] init];
|
||||
_locationManager.distanceFilter = RCT_DEFAULT_LOCATION_ACCURACY;
|
||||
_locationManager.delegate = self;
|
||||
|
||||
_pendingRequests = [[NSMutableArray alloc] init];
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (void)dealloc
|
||||
{
|
||||
[_locationManager stopUpdatingLocation];
|
||||
}
|
||||
|
||||
#pragma mark - Private API
|
||||
|
||||
- (void)beginLocationUpdates
|
||||
{
|
||||
// Request location access permission
|
||||
if ([_locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
|
||||
[_locationManager requestWhenInUseAuthorization];
|
||||
}
|
||||
|
||||
// Start observing location
|
||||
[_locationManager startUpdatingLocation];
|
||||
}
|
||||
|
||||
#pragma mark - Timeout handler
|
||||
|
||||
- (void)timeout:(NSTimer *)timer
|
||||
{
|
||||
RCTLocationRequest *request = timer.userInfo;
|
||||
NSString *message = [NSString stringWithFormat: @"Unable to fetch location within %zds.", (NSInteger)(timer.timeInterval * 1000.0)];
|
||||
request.errorBlock(@[RCTPositionError(RCTPositionErrorTimeout, message)]);
|
||||
[_pendingRequests removeObject:request];
|
||||
|
||||
// Stop updating if no pending requests
|
||||
if (_pendingRequests.count == 0 && !_observingLocation) {
|
||||
[_locationManager stopUpdatingLocation];
|
||||
}
|
||||
}
|
||||
|
||||
#pragma mark - Public API
|
||||
|
||||
- (void)startObserving:(NSDictionary *)optionsJSON
|
||||
{
|
||||
RCT_EXPORT();
|
||||
|
||||
dispatch_async(dispatch_get_main_queue(), ^{
|
||||
|
||||
// Select best options
|
||||
_observerOptions = RCTLocationOptionsWithJSON(optionsJSON);
|
||||
for (RCTLocationRequest *request in _pendingRequests) {
|
||||
_observerOptions.accuracy = MIN(_observerOptions.accuracy, request.options.accuracy);
|
||||
}
|
||||
|
||||
_locationManager.desiredAccuracy = _observerOptions.accuracy;
|
||||
[self beginLocationUpdates];
|
||||
_observingLocation = YES;
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
- (void)stopObserving
|
||||
{
|
||||
RCT_EXPORT();
|
||||
|
||||
dispatch_async(dispatch_get_main_queue(), ^{
|
||||
|
||||
// Stop observing
|
||||
_observingLocation = NO;
|
||||
|
||||
// Stop updating if no pending requests
|
||||
if (_pendingRequests.count == 0) {
|
||||
[_locationManager stopUpdatingLocation];
|
||||
}
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
- (void)getCurrentPosition:(RCTResponseSenderBlock)successBlock
|
||||
withErrorCallback:(RCTResponseSenderBlock)errorBlock
|
||||
options:(NSDictionary *)optionsJSON
|
||||
{
|
||||
RCT_EXPORT();
|
||||
|
||||
if (!successBlock) {
|
||||
RCTLogError(@"%@.getCurrentPosition called with nil success parameter.", [self class]);
|
||||
return;
|
||||
}
|
||||
|
||||
dispatch_async(dispatch_get_main_queue(), ^{
|
||||
|
||||
if (![CLLocationManager locationServicesEnabled]) {
|
||||
if (errorBlock) {
|
||||
errorBlock(@[
|
||||
RCTPositionError(RCTPositionErrorUnavailable, @"Location services disabled.")
|
||||
]);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (![CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied) {
|
||||
if (errorBlock) {
|
||||
errorBlock(@[
|
||||
RCTPositionError(RCTPositionErrorDenied, nil)
|
||||
]);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// Get options
|
||||
RCTLocationOptions options = RCTLocationOptionsWithJSON(optionsJSON);
|
||||
|
||||
// Check if previous recorded location exists and is good enough
|
||||
if (_lastLocationEvent &&
|
||||
CFAbsoluteTimeGetCurrent() - [RCTConvert NSTimeInterval:_lastLocationEvent[@"timestamp"]] < options.maximumAge &&
|
||||
[_lastLocationEvent[@"coords"][@"accuracy"] doubleValue] >= options.accuracy) {
|
||||
|
||||
// Call success block with most recent known location
|
||||
successBlock(@[_lastLocationEvent]);
|
||||
return;
|
||||
}
|
||||
|
||||
// Create request
|
||||
RCTLocationRequest *request = [[RCTLocationRequest alloc] init];
|
||||
request.successBlock = successBlock;
|
||||
request.errorBlock = errorBlock ?: ^(NSArray *args){};
|
||||
request.options = options;
|
||||
request.timeoutTimer = [NSTimer scheduledTimerWithTimeInterval:options.timeout
|
||||
target:self
|
||||
selector:@selector(timeout:)
|
||||
userInfo:request
|
||||
repeats:NO];
|
||||
[_pendingRequests addObject:request];
|
||||
|
||||
// Configure location manager and begin updating location
|
||||
_locationManager.desiredAccuracy = MIN(_locationManager.desiredAccuracy, options.accuracy);
|
||||
[self beginLocationUpdates];
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
#pragma mark - CLLocationManagerDelegate
|
||||
|
||||
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
|
||||
{
|
||||
// Create event
|
||||
CLLocation *location = [locations lastObject];
|
||||
_lastLocationEvent = @{
|
||||
@"coords": @{
|
||||
@"latitude": @(location.coordinate.latitude),
|
||||
@"longitude": @(location.coordinate.longitude),
|
||||
@"altitude": @(location.altitude),
|
||||
@"accuracy": @(location.horizontalAccuracy),
|
||||
@"altitudeAccuracy": @(location.verticalAccuracy),
|
||||
@"heading": @(location.course),
|
||||
@"speed": @(location.speed),
|
||||
},
|
||||
@"timestamp": @(CFAbsoluteTimeGetCurrent() * 1000.0) // in ms
|
||||
};
|
||||
|
||||
// Send event
|
||||
if (_observingLocation) {
|
||||
[_bridge.eventDispatcher sendDeviceEventWithName:@"geolocationDidChange"
|
||||
body:_lastLocationEvent];
|
||||
}
|
||||
|
||||
// Fire all queued callbacks
|
||||
for (RCTLocationRequest *request in _pendingRequests) {
|
||||
request.successBlock(@[_lastLocationEvent]);
|
||||
}
|
||||
[_pendingRequests removeAllObjects];
|
||||
|
||||
// Stop updating if not not observing
|
||||
if (!_observingLocation) {
|
||||
[_locationManager stopUpdatingLocation];
|
||||
}
|
||||
|
||||
// Reset location accuracy
|
||||
_locationManager.desiredAccuracy = RCT_DEFAULT_LOCATION_ACCURACY;
|
||||
}
|
||||
|
||||
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
|
||||
{
|
||||
// Check error type
|
||||
NSDictionary *jsError = nil;
|
||||
switch (error.code) {
|
||||
case kCLErrorDenied:
|
||||
jsError = RCTPositionError(RCTPositionErrorDenied, nil);
|
||||
break;
|
||||
case kCLErrorNetwork:
|
||||
jsError = RCTPositionError(RCTPositionErrorUnavailable, @"Unable to retrieve location due to a network failure");
|
||||
break;
|
||||
case kCLErrorLocationUnknown:
|
||||
default:
|
||||
jsError = RCTPositionError(RCTPositionErrorUnavailable, nil);
|
||||
break;
|
||||
}
|
||||
|
||||
// Send event
|
||||
if (_observingLocation) {
|
||||
[_bridge.eventDispatcher sendDeviceEventWithName:@"geolocationError"
|
||||
body:jsError];
|
||||
}
|
||||
|
||||
// Fire all queued error callbacks
|
||||
for (RCTLocationRequest *request in _pendingRequests) {
|
||||
request.errorBlock(@[jsError]);
|
||||
}
|
||||
[_pendingRequests removeAllObjects];
|
||||
|
||||
// Reset location accuracy
|
||||
_locationManager.desiredAccuracy = RCT_DEFAULT_LOCATION_ACCURACY;
|
||||
}
|
||||
|
||||
@end
|
||||
Reference in New Issue
Block a user