Skip to content

Commit

Permalink
Improved Geolocation API
Browse files Browse the repository at this point in the history
  • Loading branch information
nicklockwood committed Mar 9, 2015
1 parent c5e6f55 commit 705a8e0
Show file tree
Hide file tree
Showing 12 changed files with 399 additions and 232 deletions.
10 changes: 5 additions & 5 deletions Examples/UIExplorer/GeoLocationExample.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* Copyright 2004-present Facebook. All Rights Reserved.
*
* @providesModule GeoLocationExample
* @providesModule GeolocationExample
*/
/* eslint no-console: 0 */
'use strict';
Expand All @@ -15,19 +15,19 @@ var {
} = React;

exports.framework = 'React';
exports.title = 'GeoLocation';
exports.description = 'Examples of using the GeoLocation API.';
exports.title = 'Geolocation';
exports.description = 'Examples of using the Geolocation API.';

exports.examples = [
{
title: 'navigator.geolocation',
render: function() {
return <GeoLocationExample />;
return <GeolocationExample />;
},
}
];

var GeoLocationExample = React.createClass({
var GeolocationExample = React.createClass({
getInitialState: function() {
return {
initialPosition: 'unknown',
Expand Down
30 changes: 30 additions & 0 deletions Examples/UIExplorer/UIExplorer.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
134180011AA9153C003F314A /* libRCTText.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 13417FEF1AA914B8003F314A /* libRCTText.a */; };
134180021AA9153C003F314A /* libReactKit.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 13417FFF1AA91531003F314A /* libReactKit.a */; };
1341802C1AA9178B003F314A /* libRCTNetwork.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 1341802B1AA91779003F314A /* libRCTNetwork.a */; };
134A8A2A1AACED7A00945AAE /* libRCTGeolocation.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 134A8A251AACED6A00945AAE /* libRCTGeolocation.a */; };
13B07FBC1A68108700A75B9A /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 13B07FB01A68108700A75B9A /* AppDelegate.m */; };
13B07FBD1A68108700A75B9A /* LaunchScreen.xib in Resources */ = {isa = PBXBuildFile; fileRef = 13B07FB11A68108700A75B9A /* LaunchScreen.xib */; };
13B07FBF1A68108700A75B9A /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 13B07FB51A68108700A75B9A /* Images.xcassets */; };
Expand Down Expand Up @@ -46,13 +47,21 @@
remoteGlobalIDString = 58B511DB1A9E6C8500147676;
remoteInfo = RCTNetwork;
};
134A8A241AACED6A00945AAE /* PBXContainerItemProxy */ = {
isa = PBXContainerItemProxy;
containerPortal = 134A8A201AACED6A00945AAE /* RCTGeolocation.xcodeproj */;
proxyType = 2;
remoteGlobalIDString = 134814201AA4EA6300B7C361;
remoteInfo = RCTGeolocation;
};
/* End PBXContainerItemProxy section */

/* Begin PBXFileReference section */
13417FE31AA91428003F314A /* RCTImage.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = RCTImage.xcodeproj; path = ../../Libraries/Image/RCTImage.xcodeproj; sourceTree = "<group>"; };
13417FEA1AA914B8003F314A /* RCTText.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = RCTText.xcodeproj; path = ../../Libraries/Text/RCTText.xcodeproj; sourceTree = "<group>"; };
13417FFA1AA91531003F314A /* ReactKit.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = ReactKit.xcodeproj; path = ../../ReactKit/ReactKit.xcodeproj; sourceTree = "<group>"; };
134180261AA91779003F314A /* RCTNetwork.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = RCTNetwork.xcodeproj; path = ../../Libraries/Network/RCTNetwork.xcodeproj; sourceTree = "<group>"; };
134A8A201AACED6A00945AAE /* RCTGeolocation.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = RCTGeolocation.xcodeproj; path = ../../Libraries/Geolocation/RCTGeolocation.xcodeproj; sourceTree = "<group>"; };
13B07F961A680F5B00A75B9A /* UIExplorer.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = UIExplorer.app; sourceTree = BUILT_PRODUCTS_DIR; };
13B07FAF1A68108700A75B9A /* AppDelegate.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = "<group>"; };
13B07FB01A68108700A75B9A /* AppDelegate.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = "<group>"; };
Expand All @@ -67,6 +76,7 @@
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
134A8A2A1AACED7A00945AAE /* libRCTGeolocation.a in Frameworks */,
1341802C1AA9178B003F314A /* libRCTNetwork.a in Frameworks */,
134180011AA9153C003F314A /* libRCTText.a in Frameworks */,
134180021AA9153C003F314A /* libReactKit.a in Frameworks */,
Expand All @@ -80,6 +90,7 @@
1316A21D1AA397F400C0188E /* Libraries */ = {
isa = PBXGroup;
children = (
134A8A201AACED6A00945AAE /* RCTGeolocation.xcodeproj */,
13417FFA1AA91531003F314A /* ReactKit.xcodeproj */,
134180261AA91779003F314A /* RCTNetwork.xcodeproj */,
13417FEA1AA914B8003F314A /* RCTText.xcodeproj */,
Expand Down Expand Up @@ -120,6 +131,14 @@
name = Products;
sourceTree = "<group>";
};
134A8A211AACED6A00945AAE /* Products */ = {
isa = PBXGroup;
children = (
134A8A251AACED6A00945AAE /* libRCTGeolocation.a */,
);
name = Products;
sourceTree = "<group>";
};
13B07FAE1A68108700A75B9A /* UIExplorer */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -191,6 +210,10 @@
productRefGroup = 83CBBA001A601CBA00E9B192 /* Products */;
projectDirPath = "";
projectReferences = (
{
ProductGroup = 134A8A211AACED6A00945AAE /* Products */;
ProjectRef = 134A8A201AACED6A00945AAE /* RCTGeolocation.xcodeproj */;
},
{
ProductGroup = 13417FE41AA91428003F314A /* Products */;
ProjectRef = 13417FE31AA91428003F314A /* RCTImage.xcodeproj */;
Expand Down Expand Up @@ -244,6 +267,13 @@
remoteRef = 1341802A1AA91779003F314A /* PBXContainerItemProxy */;
sourceTree = BUILT_PRODUCTS_DIR;
};
134A8A251AACED6A00945AAE /* libRCTGeolocation.a */ = {
isa = PBXReferenceProxy;
fileType = archive.ar;
path = libRCTGeolocation.a;
remoteRef = 134A8A241AACED6A00945AAE /* PBXContainerItemProxy */;
sourceTree = BUILT_PRODUCTS_DIR;
};
/* End PBXReferenceProxy section */

/* Begin PBXResourcesBuildPhase section */
Expand Down
2 changes: 1 addition & 1 deletion Examples/UIExplorer/UIExplorerList.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ var EXAMPLES = [
require('./ActivityIndicatorExample'),
require('./ScrollViewExample'),
require('./DatePickerExample'),
require('./GeoLocationExample'),
require('./GeolocationExample'),
require('./TabBarExample'),
];

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/**
* Copyright 2004-present Facebook. All Rights Reserved.
*
* @providesModule GeoLocation
* @providesModule Geolocation
*/
'use strict';

var RCTDeviceEventEmitter = require('RCTDeviceEventEmitter');
var RCTLocationObserver = require('NativeModules').RKLocationObserver;
var RCTLocationObserver = require('NativeModulesDeprecated').RKLocationObserver;

var invariant = require('invariant');
var logError = require('logError');
Expand All @@ -16,57 +16,58 @@ var subscriptions = [];

var updatesEnabled = false;

var ensureObserving = function() {
if (!updatesEnabled) {
RCTLocationObserver.startObserving();
updatesEnabled = true;
}
};

/**
* /!\ 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:
* Geolocation follows the MDN specification:
* https://developer.mozilla.org/en-US/docs/Web/API/Geolocation
*/
class GeoLocation {
static getCurrentPosition(geo_success, geo_error, geo_options) {
var Geolocation = {

getCurrentPosition: function(geo_success, geo_error, geo_options) {
invariant(
typeof geo_success === 'function',
'Must provide a valid geo_success callback.'
);
if (geo_options) {
warning('geo_options are not yet supported.');
}
ensureObserving();
RCTLocationObserver.getCurrentPosition(
geo_success,
geo_error || logError
geo_error || logError,
geo_options || {}
);
}
static watchPosition(callback) {
ensureObserving();
},

watchPosition: function(success, error, options) {
if (!updatesEnabled) {
RCTLocationObserver.startObserving(options || {});
updatesEnabled = true;
}
var watchID = subscriptions.length;
subscriptions.push(
subscriptions.push([
RCTDeviceEventEmitter.addListener(
'geoLocationDidChange',
callback
)
);
'geolocationDidChange',
success
),
error ? RCTDeviceEventEmitter.addListener(
'geolocationError',
error
) : null,
]);
return watchID;
}
static clearWatch(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.remove();
sub[0].remove();
sub[1] && sub[1].remove();
subscriptions[watchID] = undefined;
var noWatchers = true;
for (var ii = 0; ii < subscriptions.length; ii++) {
Expand All @@ -75,10 +76,11 @@ class GeoLocation {
}
}
if (noWatchers) {
GeoLocation.stopObserving();
Geolocation.stopObserving();
}
}
static stopObserving() {
},

stopObserving: function() {
if (updatesEnabled) {
RCTLocationObserver.stopObserving();
updatesEnabled = false;
Expand All @@ -89,10 +91,8 @@ class GeoLocation {
}
}
subscriptions = [];
} else {
warning('Tried to stop observing when not observing.');
}
}
}

module.exports = GeoLocation;
module.exports = Geolocation;
File renamed without changes.
Loading

0 comments on commit 705a8e0

Please sign in to comment.