mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-17 04:41:36 +08:00
Some improvements to location module API.
This commit is contained in:
@ -99,10 +99,12 @@
|
|||||||
<TypeScriptCompile Include="apps\template-master-detail\details-page.ts">
|
<TypeScriptCompile Include="apps\template-master-detail\details-page.ts">
|
||||||
<DependentUpon>details-page.xml</DependentUpon>
|
<DependentUpon>details-page.xml</DependentUpon>
|
||||||
</TypeScriptCompile>
|
</TypeScriptCompile>
|
||||||
<TypeScriptCompile Include="apps\tests\app\binding_tests.ts">
|
<TypeScriptCompile Include="apps\template-master-detail\main-page.ts">
|
||||||
<DependentUpon>binding_tests.xml</DependentUpon>
|
<DependentUpon>main-page.xml</DependentUpon>
|
||||||
|
</TypeScriptCompile>
|
||||||
|
<TypeScriptCompile Include="apps\tests\app\location-example.ts">
|
||||||
|
<DependentUpon>location-example.xml</DependentUpon>
|
||||||
</TypeScriptCompile>
|
</TypeScriptCompile>
|
||||||
<TypeScriptCompile Include="apps\tests\app\location-example.ts" />
|
|
||||||
<TypeScriptCompile Include="apps\tests\app\style_props.ts" />
|
<TypeScriptCompile Include="apps\tests\app\style_props.ts" />
|
||||||
<TypeScriptCompile Include="apps\tests\console-tests.ts" />
|
<TypeScriptCompile Include="apps\tests\console-tests.ts" />
|
||||||
<TypeScriptCompile Include="apps\tests\file-name-resolver-tests\file-name-resolver-tests.ts" />
|
<TypeScriptCompile Include="apps\tests\file-name-resolver-tests\file-name-resolver-tests.ts" />
|
||||||
@ -248,6 +250,15 @@
|
|||||||
<TypeScriptCompile Include="image-source\image-source.d.ts" />
|
<TypeScriptCompile Include="image-source\image-source.d.ts" />
|
||||||
<TypeScriptCompile Include="js-libs\easysax\easysax.d.ts" />
|
<TypeScriptCompile Include="js-libs\easysax\easysax.d.ts" />
|
||||||
<TypeScriptCompile Include="location\location.d.ts" />
|
<TypeScriptCompile Include="location\location.d.ts" />
|
||||||
|
<TypeScriptCompile Include="location\location-common.ts" >
|
||||||
|
<DependentUpon>location.d.ts</DependentUpon>
|
||||||
|
</TypeScriptCompile>
|
||||||
|
<TypeScriptCompile Include="location\location.android.ts" >
|
||||||
|
<DependentUpon>location.d.ts</DependentUpon>
|
||||||
|
</TypeScriptCompile>
|
||||||
|
<TypeScriptCompile Include="location\location.ios.ts" >
|
||||||
|
<DependentUpon>location.d.ts</DependentUpon>
|
||||||
|
</TypeScriptCompile>
|
||||||
<TypeScriptCompile Include="apps\tests\app\pageNavigation.ts" />
|
<TypeScriptCompile Include="apps\tests\app\pageNavigation.ts" />
|
||||||
<TypeScriptCompile Include="apps\tests\pages\navigation\pageA-new-activity.ts" />
|
<TypeScriptCompile Include="apps\tests\pages\navigation\pageA-new-activity.ts" />
|
||||||
<TypeScriptCompile Include="apps\tests\pages\navigation\pageA.ts" />
|
<TypeScriptCompile Include="apps\tests\pages\navigation\pageA.ts" />
|
||||||
@ -527,13 +538,6 @@
|
|||||||
</TypeScriptCompile>
|
</TypeScriptCompile>
|
||||||
<TypeScriptCompile Include="local-settings\local-settings-common.ts" />
|
<TypeScriptCompile Include="local-settings\local-settings-common.ts" />
|
||||||
<TypeScriptCompile Include="apps\tests\timer-tests.ts" />
|
<TypeScriptCompile Include="apps\tests\timer-tests.ts" />
|
||||||
<TypeScriptCompile Include="location\location-manager.android.ts">
|
|
||||||
<DependentUpon>location-manager.d.ts</DependentUpon>
|
|
||||||
</TypeScriptCompile>
|
|
||||||
<TypeScriptCompile Include="location\location-manager.d.ts" />
|
|
||||||
<TypeScriptCompile Include="location\location-manager.ios.ts">
|
|
||||||
<DependentUpon>location-manager.d.ts</DependentUpon>
|
|
||||||
</TypeScriptCompile>
|
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<TypeScriptCompile Include="libjs.d.ts" />
|
<TypeScriptCompile Include="libjs.d.ts" />
|
||||||
@ -549,7 +553,7 @@
|
|||||||
<Content Include="apps\template-master-detail\main-page.minWH600.xml" />
|
<Content Include="apps\template-master-detail\main-page.minWH600.xml" />
|
||||||
<Content Include="apps\TelerikNEXT\images\background.jpg" />
|
<Content Include="apps\TelerikNEXT\images\background.jpg" />
|
||||||
<Content Include="apps\template-settings\app.css" />
|
<Content Include="apps\template-settings\app.css" />
|
||||||
<Content Include="apps\tests\app\binding_tests.xml" />
|
<Content Include="apps\tests\app\location-example.xml" />
|
||||||
<Content Include="apps\tests\ui\label\label-tests-wrong.css" />
|
<Content Include="apps\tests\ui\label\label-tests-wrong.css" />
|
||||||
<Content Include="apps\tests\pages\files\other.xml" />
|
<Content Include="apps\tests\pages\files\other.xml" />
|
||||||
<Content Include="apps\tests\pages\files\test.android.phone.xml" />
|
<Content Include="apps\tests\pages\files\test.android.phone.xml" />
|
||||||
@ -699,9 +703,6 @@
|
|||||||
<DependentUpon>file-system.d.ts</DependentUpon>
|
<DependentUpon>file-system.d.ts</DependentUpon>
|
||||||
</TypeScriptCompile>
|
</TypeScriptCompile>
|
||||||
<TypeScriptCompile Include="file-system\file-system.d.ts" />
|
<TypeScriptCompile Include="file-system\file-system.d.ts" />
|
||||||
<TypeScriptCompile Include="location\location.ts">
|
|
||||||
<DependentUpon>location.d.ts</DependentUpon>
|
|
||||||
</TypeScriptCompile>
|
|
||||||
<TypeScriptCompile Include="data\observable\observable.d.ts" />
|
<TypeScriptCompile Include="data\observable\observable.d.ts" />
|
||||||
<TypeScriptCompile Include="apps\wrap-layout-demo\app.ts" />
|
<TypeScriptCompile Include="apps\wrap-layout-demo\app.ts" />
|
||||||
<TypeScriptCompile Include="apps\wrap-layout-demo\mainPage.ts" />
|
<TypeScriptCompile Include="apps\wrap-layout-demo\mainPage.ts" />
|
||||||
|
@ -1,100 +0,0 @@
|
|||||||
import pageModule = require("ui/page");
|
|
||||||
//import stackLayoutModule = require("ui/layouts/stack-layout");
|
|
||||||
//import textFieldModule = require("ui/text-field");
|
|
||||||
import buttonModule = require("ui/button");
|
|
||||||
import observableModule = require("data/observable");
|
|
||||||
import observableArray = require("data/observable-array");
|
|
||||||
import bindableModule = require("ui/core/bindable");
|
|
||||||
//import enums = require("ui/enums");
|
|
||||||
import trace = require("trace");
|
|
||||||
trace.setCategories(trace.categories.Test + "," + trace.categories.Binding);
|
|
||||||
trace.enable();
|
|
||||||
|
|
||||||
export function pageLoaded(args: observableModule.EventData) {
|
|
||||||
var page: pageModule.Page = <pageModule.Page>args.object;
|
|
||||||
var model = new observableModule.Observable();
|
|
||||||
var tasks = new observableArray.ObservableArray();
|
|
||||||
//tasks.push("tralala");
|
|
||||||
//var model = page.bindingContext;
|
|
||||||
model.set("tasks", tasks);
|
|
||||||
model.set("paramProperty", "%%%");
|
|
||||||
var toUpperConverter: bindableModule.ValueConverter = {
|
|
||||||
toModel: function (value, param1) {
|
|
||||||
return param1 + value.toLowerCase();
|
|
||||||
},
|
|
||||||
toView: function (value, param1) {
|
|
||||||
if (value === 0) {
|
|
||||||
return "no items";
|
|
||||||
}
|
|
||||||
return value + " items";
|
|
||||||
}
|
|
||||||
};
|
|
||||||
model.set("toUpper", toUpperConverter);
|
|
||||||
model.set("testProperty", "Alabala");
|
|
||||||
|
|
||||||
var dateConverter = {
|
|
||||||
toView: function (value, format) {
|
|
||||||
var result = format;
|
|
||||||
var day = value.getDate();
|
|
||||||
result = result.replace("dd", month < 10 ? "0" + day : day);
|
|
||||||
var month = value.getMonth() + 1;
|
|
||||||
result = result.replace("mm", month < 10 ? "0" + month : month);
|
|
||||||
result = result.replace("yyyy", value.getFullYear());
|
|
||||||
return result;
|
|
||||||
},
|
|
||||||
toModel: function (value, format) {
|
|
||||||
var ddIndex = format.indexOf("dd");
|
|
||||||
var day = parseInt(value.substr(ddIndex, 2));
|
|
||||||
var mmIndex = format.indexOf("mm");
|
|
||||||
var month = parseInt(value.substr(mmIndex, 2));
|
|
||||||
var yyyyIndex = format.indexOf("yyyy");
|
|
||||||
var year = parseInt(value.substr(yyyyIndex, 4));
|
|
||||||
var result = new Date(year, month - 1, day);
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
model.set("dateConverter", dateConverter);
|
|
||||||
model.set("testDate", new Date());
|
|
||||||
|
|
||||||
page.bindingContext = model;
|
|
||||||
}
|
|
||||||
|
|
||||||
export function onTap(args: observableModule.EventData) {
|
|
||||||
var button: buttonModule.Button = <buttonModule.Button>args.object;
|
|
||||||
trace.write("tasks: " + button.bindingContext.get("testDate"), trace.categories.Test, trace.messageType.info);
|
|
||||||
//button.bindingContext.get("tasks").push("alabala");
|
|
||||||
}
|
|
||||||
|
|
||||||
//export function createPage() {
|
|
||||||
// var stackLayout = new stackLayoutModule.StackLayout();
|
|
||||||
// var firstTextField = new textFieldModule.TextField();
|
|
||||||
// firstTextField.updateTextTrigger = enums.UpdateTextTrigger.textChanged;
|
|
||||||
// var secondTextField = new textFieldModule.TextField();
|
|
||||||
// secondTextField.updateTextTrigger = enums.UpdateTextTrigger.textChanged;
|
|
||||||
|
|
||||||
// var model = new observableModule.Observable();
|
|
||||||
|
|
||||||
// var bindOptions: bindableModule.BindingOptions = {
|
|
||||||
// sourceProperty: "testProperty",
|
|
||||||
// targetProperty: "text",
|
|
||||||
// twoWay: true,
|
|
||||||
// expression: "testProperty | toUpper('$$$')"
|
|
||||||
// };
|
|
||||||
|
|
||||||
// firstTextField.bind(bindOptions, model);
|
|
||||||
// secondTextField.bind({
|
|
||||||
// sourceProperty: "testProperty",
|
|
||||||
// targetProperty: "text",
|
|
||||||
// twoWay: true
|
|
||||||
// }, model);
|
|
||||||
|
|
||||||
// stackLayout.addChild(firstTextField);
|
|
||||||
// stackLayout.addChild(secondTextField);
|
|
||||||
|
|
||||||
// var page = new pageModule.Page();
|
|
||||||
// page.on("loaded", pageLoaded);
|
|
||||||
// page.content = stackLayout;
|
|
||||||
// page.bindingContext = model;
|
|
||||||
// return page;
|
|
||||||
//}
|
|
@ -1,8 +0,0 @@
|
|||||||
<Page xmlns="http://www.nativescript.org/tns.xsd" loaded="pageLoaded">
|
|
||||||
<StackLayout padding="7">
|
|
||||||
<!--<TextField text="{{ tasks.length, tasks.length === 0 ? 'zero items' : tasks.length + ' items', false }}" />-->
|
|
||||||
<TextField text="{{ testDate, testDate | dateConverter('dd.mm.yyyy') }}" updateTextTrigger="textChanged"/>
|
|
||||||
<TextField text="{{ testDate }}" />
|
|
||||||
<Button text="Click" tap="onTap" />
|
|
||||||
</StackLayout>
|
|
||||||
</Page>
|
|
@ -1,65 +1,50 @@
|
|||||||
import pages = require("ui/page");
|
import pages = require("ui/page");
|
||||||
import gridLayoutModule = require("ui/layouts/grid-layout");
|
|
||||||
import bm = require("ui/button");
|
|
||||||
import locationModule = require("location");
|
import locationModule = require("location");
|
||||||
import dialogsModule = require("ui/dialogs");
|
import dialogsModule = require("ui/dialogs");
|
||||||
import trace = require("trace");
|
|
||||||
import platformModule = require("platform");
|
import platformModule = require("platform");
|
||||||
import textViewModule = require("ui/text-view");
|
import textViewModule = require("ui/text-view");
|
||||||
|
import observable = require("data/observable");
|
||||||
|
import appModule = require("application");
|
||||||
|
|
||||||
export function createPage() {
|
var locationManager = new locationModule.LocationManager();
|
||||||
function printLocation(location: locationModule.Location) {
|
|
||||||
|
export function onStartButtonTap(args: observable.EventData) {
|
||||||
|
console.log("Start button tapped");
|
||||||
|
var textView = <textViewModule.TextView>(page.getViewById("textView"));
|
||||||
|
locationManager.startLocationMonitoring(function (location) {
|
||||||
|
console.log("location-example.location: " + printLocation(location));
|
||||||
|
textView.text = printLocation(location) + "\r\n" + textView.text;
|
||||||
|
}, null, { desiredAccuracy: 300, maximumAge: 20000, minimumUpdateTime: 5000, updateDistance: 20 });
|
||||||
|
}
|
||||||
|
|
||||||
|
function printLocation(location: locationModule.Location) {
|
||||||
return "longitude: " + location.longitude + ", " +
|
return "longitude: " + location.longitude + ", " +
|
||||||
"latitude: " + location.latitude + ", " +
|
"latitude: " + location.latitude + ", " +
|
||||||
"timeStamp: " + location.timestamp + ", " +
|
"timeStamp: " + location.timestamp + ", " +
|
||||||
"horizontalAccuracy: " + location.horizontalAccuracy + ", " +
|
"horizontalAccuracy: " + location.horizontalAccuracy + ", " +
|
||||||
"verticalAccuracy: " + location.verticalAccuracy;
|
"verticalAccuracy: " + location.verticalAccuracy;
|
||||||
}
|
}
|
||||||
|
|
||||||
var locationManager = new locationModule.LocationManager();
|
export function onStopButtonTap(args: observable.EventData) {
|
||||||
|
|
||||||
var textView = new textViewModule.TextView();
|
|
||||||
textView.editable = false;
|
|
||||||
textView.style.fontSize = 8;
|
|
||||||
|
|
||||||
var startButton = new bm.Button();
|
|
||||||
startButton.text = "Start";
|
|
||||||
startButton.on(bm.knownEvents.tap, function (eventData) {
|
|
||||||
locationManager.startLocationMonitoring(function (location) {
|
|
||||||
trace.write("location: " + printLocation(location), trace.categories.Test, trace.messageType.info);
|
|
||||||
textView.text = printLocation(location) + "\r\n" + textView.text;
|
|
||||||
}, null, {desiredAccuracy: 3, maximumAge: 20000, minimumUpdateTime: 5000, updateDistance: 0});
|
|
||||||
});
|
|
||||||
|
|
||||||
var stopButton = new bm.Button();
|
|
||||||
stopButton.text = "Stop";
|
|
||||||
stopButton.on(bm.knownEvents.tap, function (eventData) {
|
|
||||||
locationManager.stopLocationMonitoring();
|
locationManager.stopLocationMonitoring();
|
||||||
});
|
}
|
||||||
|
|
||||||
var grid = new gridLayoutModule.GridLayout();
|
export function onRequestButtonTap(args: observable.EventData) {
|
||||||
|
checkLocationService(page);
|
||||||
|
}
|
||||||
|
|
||||||
grid.addRow(new gridLayoutModule.ItemSpec(1, gridLayoutModule.GridUnitType.auto));
|
var page: pages.Page;
|
||||||
grid.addRow(new gridLayoutModule.ItemSpec());
|
|
||||||
|
|
||||||
grid.addColumn(new gridLayoutModule.ItemSpec(100, gridLayoutModule.GridUnitType.pixel));
|
export function pageLoaded(args: observable.EventData) {
|
||||||
grid.addColumn(new gridLayoutModule.ItemSpec());
|
page = <pages.Page>(args.object);
|
||||||
|
}
|
||||||
|
|
||||||
gridLayoutModule.GridLayout.setColumn(startButton, 0);
|
var checkLocationService = function (page: pages.Page) {
|
||||||
gridLayoutModule.GridLayout.setColumn(stopButton, 1);
|
|
||||||
gridLayoutModule.GridLayout.setRow(textView, 1);
|
|
||||||
gridLayoutModule.GridLayout.setColumnSpan(textView, 2);
|
|
||||||
|
|
||||||
grid.addChild(startButton);
|
|
||||||
grid.addChild(stopButton);
|
|
||||||
grid.addChild(textView);
|
|
||||||
|
|
||||||
var checkLocationService = function (page: pages.Page) {
|
|
||||||
if (locationModule.LocationManager.isEnabled()) {
|
if (locationModule.LocationManager.isEnabled()) {
|
||||||
trace.write("LocationManager.isEnabled", trace.categories.Test, trace.messageType.info);
|
console.log("LocationManager.isEnabled");
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
trace.write("LocationManager.is not Enabled", trace.categories.Test, trace.messageType.info);
|
console.log("LocationManager.is not Enabled");
|
||||||
if (page.android) {
|
if (page.android) {
|
||||||
dialogsModule.confirm({
|
dialogsModule.confirm({
|
||||||
message: "Location service is not enabled!",
|
message: "Location service is not enabled!",
|
||||||
@ -67,28 +52,18 @@ export function createPage() {
|
|||||||
cancelButtonText: "Cancel", title: "Confirm"
|
cancelButtonText: "Cancel", title: "Confirm"
|
||||||
}).then((r) => {
|
}).then((r) => {
|
||||||
if (r) {
|
if (r) {
|
||||||
(<android.app.Activity>page._context).startActivityForResult(new android.content.Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS), 0);
|
// do not forget to add android.permission.ACCESS_FINE_LOCATION in your manifest file
|
||||||
|
(<android.app.Activity>appModule.android.currentContext).startActivityForResult(new android.content.Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS), 0);
|
||||||
}
|
}
|
||||||
}, null);
|
}, null);
|
||||||
}
|
}
|
||||||
else if (page.ios) {
|
else if (page.ios) {
|
||||||
if (platformModule.device.osVersion.indexOf("8") === 0) {
|
if (platformModule.device.osVersion.indexOf("8") === 0) {
|
||||||
|
// we need special handling of iOS 8.0 version, since some breaking changes regarding location
|
||||||
|
// do not forget to put NSLocationWhenInUseUsageDescription in app.plist file
|
||||||
var iosLocationManager = CLLocationManager.alloc().init();
|
var iosLocationManager = CLLocationManager.alloc().init();
|
||||||
iosLocationManager.requestWhenInUseAuthorization();
|
iosLocationManager.requestWhenInUseAuthorization();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
var page = new pages.Page();
|
|
||||||
|
|
||||||
page.on("loaded", function () {
|
|
||||||
checkLocationService(page);
|
|
||||||
page.frame.on("loaded", function () {
|
|
||||||
checkLocationService(page);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
page.content = grid;
|
|
||||||
return page;
|
|
||||||
}
|
}
|
8
apps/tests/app/location-example.xml
Normal file
8
apps/tests/app/location-example.xml
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
<Page loaded="pageLoaded">
|
||||||
|
<GridLayout rows="50, *" columns="*, *, *">
|
||||||
|
<Button text="Request" tap="onRequestButtonTap" col="0" row="0" />
|
||||||
|
<Button text="Start" tap="onStartButtonTap" col="1" row="0" />
|
||||||
|
<Button text="Stop" tap="onStopButtonTap" col="2" row="0" />
|
||||||
|
<TextView id="textView" colSpan="3" row="1" editable="false" style.fontSize="8"/>
|
||||||
|
</GridLayout>
|
||||||
|
</Page>
|
@ -27,6 +27,7 @@ export function tearDown() {
|
|||||||
|
|
||||||
export var testIsEnabled = function () {
|
export var testIsEnabled = function () {
|
||||||
if (!locationIsEnabled) {
|
if (!locationIsEnabled) {
|
||||||
|
console.log("Location service is not enabled!!!");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// <snippet module="location" title="location">
|
// <snippet module="location" title="location">
|
||||||
@ -182,7 +183,7 @@ export var testSnippet = function (done) {
|
|||||||
//
|
//
|
||||||
// ``` JavaScript
|
// ``` JavaScript
|
||||||
// var locationModule = require("location");
|
// var locationModule = require("location");
|
||||||
//// options can also look like { maximumAge: 2000, timeout: 20 }
|
//// options can also look like { maximumAge: 2000, timeout: 20 * 1000 }
|
||||||
locationModule.getLocation({ maximumAge: 30000, timeout: 0 }).then(function (location) {
|
locationModule.getLocation({ maximumAge: 30000, timeout: 0 }).then(function (location) {
|
||||||
//console.log('Location received: ' + location);
|
//console.log('Location received: ' + location);
|
||||||
// <hide>
|
// <hide>
|
||||||
|
@ -1,14 +1,15 @@
|
|||||||
|
import timer = require("timer");
|
||||||
import timer = require("timer");
|
//import locationManagerModule = require("location/location-manager");
|
||||||
import locationManagerModule = require("location/location-manager");
|
|
||||||
import defModule = require("location");
|
import defModule = require("location");
|
||||||
|
|
||||||
// merge the exports of the types module with the exports of this file
|
// merge the exports of the types module with the exports of this file
|
||||||
import merger = require("utils/module-merge");
|
//import merger = require("utils/module-merge");
|
||||||
declare var exports;
|
//declare var exports;
|
||||||
merger.merge(locationManagerModule, exports);
|
//merger.merge(locationManagerModule, exports);
|
||||||
|
|
||||||
export class Location {
|
var defaultGetLocationTimeout = 20000;
|
||||||
|
|
||||||
|
export class Location implements defModule.Location {
|
||||||
public latitude: number;
|
public latitude: number;
|
||||||
public longitude: number;
|
public longitude: number;
|
||||||
|
|
||||||
@ -29,7 +30,7 @@ export class Location {
|
|||||||
|
|
||||||
export var getLocation = function (options?: defModule.Options): Promise<defModule.Location> {
|
export var getLocation = function (options?: defModule.Options): Promise<defModule.Location> {
|
||||||
var timerId;
|
var timerId;
|
||||||
var locationManager = new locationManagerModule.LocationManager();
|
var locationManager = new defModule.LocationManager();
|
||||||
|
|
||||||
if (options && (0 === options.timeout)) {
|
if (options && (0 === options.timeout)) {
|
||||||
return new Promise<defModule.Location>((resolve, reject) => {
|
return new Promise<defModule.Location>((resolve, reject) => {
|
||||||
@ -54,7 +55,7 @@ export var getLocation = function (options?: defModule.Options): Promise<defModu
|
|||||||
}
|
}
|
||||||
|
|
||||||
return new Promise<defModule.Location>((resolve, reject) => {
|
return new Promise<defModule.Location>((resolve, reject) => {
|
||||||
if (!locationManagerModule.LocationManager.isEnabled()) {
|
if (!defModule.LocationManager.isEnabled()) {
|
||||||
return reject(new Error("Location service is disabled"));
|
return reject(new Error("Location service is disabled"));
|
||||||
}
|
}
|
||||||
locationManager.startLocationMonitoring(function (location: defModule.Location) {
|
locationManager.startLocationMonitoring(function (location: defModule.Location) {
|
||||||
@ -89,7 +90,7 @@ export var getLocation = function (options?: defModule.Options): Promise<defModu
|
|||||||
timerId = timer.setTimeout(function () {
|
timerId = timer.setTimeout(function () {
|
||||||
locationManager.stopLocationMonitoring();
|
locationManager.stopLocationMonitoring();
|
||||||
reject(new Error("timeout searching for location"));
|
reject(new Error("timeout searching for location"));
|
||||||
}, options.timeout);
|
}, options.timeout || defaultGetLocationTimeout);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
92
location/location-manager.d.ts
vendored
92
location/location-manager.d.ts
vendored
@ -1,92 +0,0 @@
|
|||||||
//@private
|
|
||||||
export declare class Location {
|
|
||||||
latitude: number;
|
|
||||||
longitude: number;
|
|
||||||
|
|
||||||
altitude: number; // in meters
|
|
||||||
|
|
||||||
horizontalAccuracy: number; // in meters
|
|
||||||
verticalAccuracy: number; // in meters
|
|
||||||
|
|
||||||
speed: number; // in m/s
|
|
||||||
|
|
||||||
direction: number; // in degrees
|
|
||||||
|
|
||||||
timestamp: Date;
|
|
||||||
|
|
||||||
public android: any; // android Location
|
|
||||||
public ios: any; // iOS CLLocation
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface Options {
|
|
||||||
/**
|
|
||||||
* Specifies desired accuracy in meters. Defaults to DesiredAccuracy.HIGH
|
|
||||||
*/
|
|
||||||
desiredAccuracy?: number;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Update distance filter in meters. Specifies how often to update. Default on iOS is no filter, on Android it is 0 meters
|
|
||||||
*/
|
|
||||||
updateDistance?: number;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Minimum time interval between location updates, in milliseconds (ignored on iOS)
|
|
||||||
*/
|
|
||||||
minimumUpdateTime?: number;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* how old locations to receive in ms.
|
|
||||||
*/
|
|
||||||
maximumAge?: number;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* how long to wait for a location in ms.
|
|
||||||
*/
|
|
||||||
timeout?: number;
|
|
||||||
}
|
|
||||||
|
|
||||||
export declare class LocationManager {
|
|
||||||
/**
|
|
||||||
* Report are location services switched ON for this device (on Android) or application (iOS)
|
|
||||||
*/
|
|
||||||
static isEnabled(): boolean;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Measure distance in meters between two locations
|
|
||||||
*/
|
|
||||||
static distance(loc1: Location, loc2: Location): number;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Specifies desired accuracy in meters. Defaults to DesiredAccuracy.HIGH
|
|
||||||
*/
|
|
||||||
desiredAccuracy: number;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Update distance filter in meters. Specifies how often to update. Default on iOS is no filter, on Android it is 0 meters
|
|
||||||
*/
|
|
||||||
updateDistance: number;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Minimum time interval between location updates, in milliseconds (ignored on iOS)
|
|
||||||
*/
|
|
||||||
minimumUpdateTime: number;
|
|
||||||
|
|
||||||
// monitoring
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Starts location monitoring.
|
|
||||||
*/
|
|
||||||
startLocationMonitoring(onLocation: (location: Location) => any, onError?: (error: Error) => any, options?: Options);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Stops location monitoring
|
|
||||||
*/
|
|
||||||
stopLocationMonitoring();
|
|
||||||
|
|
||||||
// other
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns last known location from device's location services or null of no known last location
|
|
||||||
*/
|
|
||||||
lastKnownLocation: Location;
|
|
||||||
}
|
|
@ -1,18 +1,29 @@
|
|||||||
import enums = require("ui/enums");
|
import enums = require("ui/enums");
|
||||||
import appModule = require("application");
|
import appModule = require("application");
|
||||||
import locationModule = require("location");
|
import locationModule = require("location");
|
||||||
|
import common = require("location/location-common");
|
||||||
|
|
||||||
export class LocationManager {
|
import merger = require("utils/module-merge");
|
||||||
|
declare var exports;
|
||||||
|
merger.merge(common, exports);
|
||||||
|
|
||||||
|
export class LocationManager implements locationModule.LocationManager {
|
||||||
|
get android(): locationModule.AndroidLocationManager {
|
||||||
|
return this.androidLocationManager;
|
||||||
|
}
|
||||||
|
|
||||||
|
get ios(): locationModule.iOSLocationManager {
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
public isStarted: boolean;
|
||||||
// in meters
|
// in meters
|
||||||
public desiredAccuracy: number;
|
public desiredAccuracy: number;
|
||||||
|
|
||||||
// The minimum distance (measured in meters) a device must move horizontally before an update event is generated.
|
// The minimum distance (measured in meters) a device must move horizontally before an update event is generated.
|
||||||
public updateDistance: number;
|
public updateDistance: number;
|
||||||
|
|
||||||
// minimum time interval between location updates, in milliseconds (android only)
|
private androidLocationManager: locationModule.AndroidLocationManager;
|
||||||
public minimumUpdateTime: number;
|
|
||||||
|
|
||||||
private androidLocationManager: any;
|
|
||||||
|
|
||||||
private _locationListener: any;
|
private _locationListener: any;
|
||||||
|
|
||||||
@ -60,7 +71,6 @@ export class LocationManager {
|
|||||||
location.direction = androidLocation.getBearing();
|
location.direction = androidLocation.getBearing();
|
||||||
location.timestamp = new Date(androidLocation.getTime());
|
location.timestamp = new Date(androidLocation.getTime());
|
||||||
location.android = androidLocation;
|
location.android = androidLocation;
|
||||||
//console.dump(location);
|
|
||||||
return location;
|
return location;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -108,17 +118,18 @@ export class LocationManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
|
//super();
|
||||||
this.desiredAccuracy = enums.Accuracy.any;
|
this.desiredAccuracy = enums.Accuracy.any;
|
||||||
this.updateDistance = 0;
|
this.updateDistance = 0;
|
||||||
this.minimumUpdateTime = 200;
|
|
||||||
|
|
||||||
this.androidLocationManager = appModule.android.context.getSystemService(android.content.Context.LOCATION_SERVICE);
|
//this.androidLocationManager = appModule.android.context.getSystemService(android.content.Context.LOCATION_SERVICE);
|
||||||
|
var alm = appModule.android.context.getSystemService(android.content.Context.LOCATION_SERVICE);
|
||||||
|
this.androidLocationManager = new AndroidLocationManager(alm);
|
||||||
|
this.androidLocationManager.minimumUpdateTime = 200;
|
||||||
}
|
}
|
||||||
|
|
||||||
public startLocationMonitoring(onLocation: (location: locationModule.Location) => any, onError?: (error: Error) => any, options?: locationModule.Options) {
|
public startLocationMonitoring(onLocation: (location: locationModule.Location) => any, onError?: (error: Error) => any, options?: locationModule.Options) {
|
||||||
var criteria = new android.location.Criteria();
|
var criteria = new android.location.Criteria();
|
||||||
criteria.setAccuracy((this.desiredAccuracy === enums.Accuracy.high) ? android.location.Criteria.ACCURACY_FINE : android.location.Criteria.ACCURACY_COARSE);
|
|
||||||
|
|
||||||
if (options) {
|
if (options) {
|
||||||
if (options.desiredAccuracy) {
|
if (options.desiredAccuracy) {
|
||||||
this.desiredAccuracy = options.desiredAccuracy;
|
this.desiredAccuracy = options.desiredAccuracy;
|
||||||
@ -127,15 +138,17 @@ export class LocationManager {
|
|||||||
this.updateDistance = options.updateDistance;
|
this.updateDistance = options.updateDistance;
|
||||||
}
|
}
|
||||||
if (options.minimumUpdateTime) {
|
if (options.minimumUpdateTime) {
|
||||||
this.minimumUpdateTime = options.minimumUpdateTime;
|
this.androidLocationManager.minimumUpdateTime = options.minimumUpdateTime;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
criteria.setAccuracy((this.desiredAccuracy === enums.Accuracy.high) ? android.location.Criteria.ACCURACY_FINE : android.location.Criteria.ACCURACY_COARSE);
|
||||||
|
|
||||||
this.locationListener._onLocation = onLocation;
|
this.locationListener._onLocation = onLocation;
|
||||||
this.locationListener._onError = onError;
|
this.locationListener._onError = onError;
|
||||||
this.locationListener.maximumAge = (options && ("number" === typeof options.maximumAge)) ? options.maximumAge : undefined;
|
this.locationListener.maximumAge = (options && ("number" === typeof options.maximumAge)) ? options.maximumAge : undefined;
|
||||||
try {
|
try {
|
||||||
this.androidLocationManager.requestLocationUpdates(long(this.minimumUpdateTime), float(this.updateDistance), criteria, this.locationListener, null);
|
this.androidLocationManager.manager.requestLocationUpdates(this.androidLocationManager.minimumUpdateTime, this.updateDistance, criteria, this.locationListener, null);
|
||||||
|
this.isStarted = true;
|
||||||
}
|
}
|
||||||
catch (e) {
|
catch (e) {
|
||||||
if (onError) {
|
if (onError) {
|
||||||
@ -145,21 +158,22 @@ export class LocationManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public stopLocationMonitoring() {
|
public stopLocationMonitoring() {
|
||||||
this.androidLocationManager.removeUpdates(this.locationListener);
|
this.androidLocationManager.manager.removeUpdates(this.locationListener);
|
||||||
|
this.isStarted = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
get lastKnownLocation(): locationModule.Location {
|
get lastKnownLocation(): locationModule.Location {
|
||||||
var criteria = new android.location.Criteria();
|
var criteria = new android.location.Criteria();
|
||||||
criteria.setAccuracy((this.desiredAccuracy === enums.Accuracy.high) ? android.location.Criteria.ACCURACY_FINE : android.location.Criteria.ACCURACY_COARSE);
|
criteria.setAccuracy((this.desiredAccuracy === enums.Accuracy.high) ? android.location.Criteria.ACCURACY_FINE : android.location.Criteria.ACCURACY_COARSE);
|
||||||
try {
|
try {
|
||||||
var providers = this.androidLocationManager.getProviders(criteria, false);
|
var providers = this.androidLocationManager.manager.getProviders(criteria, false);
|
||||||
var it = providers.iterator();
|
var it = providers.iterator();
|
||||||
var location: android.location.Location;
|
var location: android.location.Location;
|
||||||
var tempLocation: android.location.Location;
|
var tempLocation: android.location.Location;
|
||||||
while (it.hasNext()) {
|
while (it.hasNext()) {
|
||||||
var element = it.next();
|
var element = it.next();
|
||||||
//console.log('found provider: ' + element);
|
//console.log('found provider: ' + element);
|
||||||
tempLocation = this.androidLocationManager.getLastKnownLocation(element);
|
tempLocation = this.androidLocationManager.manager.getLastKnownLocation(element);
|
||||||
if (!location) {
|
if (!location) {
|
||||||
location = tempLocation;
|
location = tempLocation;
|
||||||
}
|
}
|
||||||
@ -180,3 +194,24 @@ export class LocationManager {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export class AndroidLocationManager implements locationModule.AndroidLocationManager {
|
||||||
|
private _manager: android.location.LocationManager;
|
||||||
|
private _minimumUpdateTime: number;
|
||||||
|
|
||||||
|
public set minimumUpdateTime(value: number) {
|
||||||
|
this._minimumUpdateTime = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public get minimumUpdateTime(): number {
|
||||||
|
return this._minimumUpdateTime;
|
||||||
|
}
|
||||||
|
|
||||||
|
public get manager(): android.location.LocationManager {
|
||||||
|
return this._manager;
|
||||||
|
}
|
||||||
|
|
||||||
|
constructor(manager: android.location.LocationManager) {
|
||||||
|
this._manager = manager;
|
||||||
|
}
|
||||||
|
}
|
45
location/location.d.ts
vendored
45
location/location.d.ts
vendored
@ -121,11 +121,6 @@ declare module "location" {
|
|||||||
*/
|
*/
|
||||||
updateDistance: number;
|
updateDistance: number;
|
||||||
|
|
||||||
/**
|
|
||||||
* The minimum time interval between subsequent location updates, in milliseconds (ignored on iOS).
|
|
||||||
*/
|
|
||||||
minimumUpdateTime: number;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* True if the location listener is already started. In this case all other start requests will be ignored.
|
* True if the location listener is already started. In this case all other start requests will be ignored.
|
||||||
*/
|
*/
|
||||||
@ -148,13 +143,49 @@ declare module "location" {
|
|||||||
* Returns last known location from device's location services or null of no known last location.
|
* Returns last known location from device's location services or null of no known last location.
|
||||||
*/
|
*/
|
||||||
lastKnownLocation: Location;
|
lastKnownLocation: Location;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* An android-specific data for location.
|
||||||
|
*/
|
||||||
|
android: AndroidLocationManager;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* An ios-specific data for location.
|
||||||
|
*/
|
||||||
|
ios: iOSLocationManager;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Fires a single shot location search. If you specify timeout in options, location search will fail on timeout.
|
* Provides Android specific data related to location.
|
||||||
|
*/
|
||||||
|
export interface AndroidLocationManager {
|
||||||
|
/**
|
||||||
|
* The android-specific location manager [LocationManager](http://developer.android.com/reference/android/location/LocationManager.html)
|
||||||
|
*/
|
||||||
|
manager: android.location.LocationManager;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The minimum time interval between subsequent location updates, in milliseconds.
|
||||||
|
*/
|
||||||
|
minimumUpdateTime: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fires a single shot location search. If you specify timeout in options (milliseconds), location search will stop on timeout.
|
||||||
* If you specify timeout = 0 it just requests the last known location.
|
* If you specify timeout = 0 it just requests the last known location.
|
||||||
* However if you specify maximumAge and the location received is older it won't be received.
|
* However if you specify maximumAge and the location received is older it won't be received.
|
||||||
* @param options An optional object specifying location update settings.
|
* @param options - An optional object specifying location update settings.
|
||||||
*/
|
*/
|
||||||
export function getLocation(options?: Options): Promise<Location>;
|
export function getLocation(options?: Options): Promise<Location>;
|
||||||
|
|
||||||
|
/* tslint:disable */
|
||||||
|
/**
|
||||||
|
* Provides iOS specific data related to location.
|
||||||
|
*/
|
||||||
|
export interface iOSLocationManager {
|
||||||
|
/**
|
||||||
|
* The ios-specific location manager [CLLocationManager](https://developer.apple.com/library/ios/documentation/CoreLocation/Reference/CLLocationManager_Class/)
|
||||||
|
*/
|
||||||
|
manager: CLLocationManager;
|
||||||
|
}
|
||||||
}
|
}
|
@ -1,5 +1,10 @@
|
|||||||
import enums = require("ui/enums");
|
import enums = require("ui/enums");
|
||||||
import locationModule = require("location");
|
import locationModule = require("location");
|
||||||
|
import common = require("location/location-common");
|
||||||
|
|
||||||
|
import merger = require("utils/module-merge");
|
||||||
|
declare var exports;
|
||||||
|
merger.merge(common, exports);
|
||||||
|
|
||||||
class LocationListenerImpl extends NSObject implements CLLocationManagerDelegate {
|
class LocationListenerImpl extends NSObject implements CLLocationManagerDelegate {
|
||||||
public static ObjCProtocols = [CLLocationManagerDelegate];
|
public static ObjCProtocols = [CLLocationManagerDelegate];
|
||||||
@ -44,14 +49,22 @@ class LocationListenerImpl extends NSObject implements CLLocationManagerDelegate
|
|||||||
}
|
}
|
||||||
|
|
||||||
public locationManagerDidFailWithError(manager, error): void {
|
public locationManagerDidFailWithError(manager, error): void {
|
||||||
// console.error('location error received ' + error.localizedDescription);
|
|
||||||
if (this._onError) {
|
if (this._onError) {
|
||||||
this._onError(new Error(error.localizedDescription));
|
this._onError(new Error(error.localizedDescription));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export class LocationManager {
|
export class LocationManager implements locationModule.LocationManager {
|
||||||
|
get android(): locationModule.AndroidLocationManager {
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
get ios(): locationModule.iOSLocationManager {
|
||||||
|
return this.iosLocationManager;
|
||||||
|
}
|
||||||
|
|
||||||
|
public isStarted: boolean;
|
||||||
// in meters
|
// in meters
|
||||||
// we might need some predefined values here like 'any' and 'high'
|
// we might need some predefined values here like 'any' and 'high'
|
||||||
public desiredAccuracy: number;
|
public desiredAccuracy: number;
|
||||||
@ -59,7 +72,7 @@ export class LocationManager {
|
|||||||
// The minimum distance (measured in meters) a device must move horizontally before an update event is generated.
|
// The minimum distance (measured in meters) a device must move horizontally before an update event is generated.
|
||||||
public updateDistance: number;
|
public updateDistance: number;
|
||||||
|
|
||||||
private iosLocationManager: CLLocationManager;
|
private iosLocationManager: locationModule.iOSLocationManager;
|
||||||
private listener: any;
|
private listener: any;
|
||||||
|
|
||||||
public static _locationFromCLLocation(clLocation: CLLocation): locationModule.Location {
|
public static _locationFromCLLocation(clLocation: CLLocation): locationModule.Location {
|
||||||
@ -71,9 +84,9 @@ export class LocationManager {
|
|||||||
location.verticalAccuracy = clLocation.verticalAccuracy;
|
location.verticalAccuracy = clLocation.verticalAccuracy;
|
||||||
location.speed = clLocation.speed;
|
location.speed = clLocation.speed;
|
||||||
location.direction = clLocation.course;
|
location.direction = clLocation.course;
|
||||||
location.timestamp = new Date(clLocation.timestamp.timeIntervalSince1970 * 1000);
|
var timeIntervalSince1970 = NSDate.dateWithTimeIntervalSinceDate(0, clLocation.timestamp).timeIntervalSince1970;
|
||||||
|
location.timestamp = new Date(timeIntervalSince1970 * 1000);
|
||||||
location.ios = clLocation;
|
location.ios = clLocation;
|
||||||
//console.dump(location);
|
|
||||||
return location;
|
return location;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -110,9 +123,11 @@ export class LocationManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
|
//super();
|
||||||
this.desiredAccuracy = enums.Accuracy.any;
|
this.desiredAccuracy = enums.Accuracy.any;
|
||||||
this.updateDistance = kCLDistanceFilterNone;
|
this.updateDistance = kCLDistanceFilterNone;
|
||||||
this.iosLocationManager = new CLLocationManager();
|
var iosLocManager = new CLLocationManager();
|
||||||
|
this.iosLocationManager = new iOSLocationManager(iosLocManager);
|
||||||
}
|
}
|
||||||
|
|
||||||
public startLocationMonitoring(onLocation: (location: locationModule.Location) => any, onError?: (error: Error) => any, options?: locationModule.Options) {
|
public startLocationMonitoring(onLocation: (location: locationModule.Location) => any, onError?: (error: Error) => any, options?: locationModule.Options) {
|
||||||
@ -127,24 +142,39 @@ export class LocationManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
this.listener = LocationListenerImpl.new().initWithLocationErrorOptions(onLocation, onError, options);
|
this.listener = LocationListenerImpl.new().initWithLocationErrorOptions(onLocation, onError, options);
|
||||||
this.iosLocationManager.delegate = this.listener;
|
this.iosLocationManager.manager.delegate = this.listener;
|
||||||
this.iosLocationManager.desiredAccuracy = this.desiredAccuracy;
|
this.iosLocationManager.manager.desiredAccuracy = this.desiredAccuracy;
|
||||||
this.iosLocationManager.distanceFilter = this.updateDistance;
|
this.iosLocationManager.manager.distanceFilter = this.updateDistance;
|
||||||
this.iosLocationManager.startUpdatingLocation();
|
this.iosLocationManager.manager.startUpdatingLocation();
|
||||||
|
this.isStarted = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public stopLocationMonitoring() {
|
public stopLocationMonitoring() {
|
||||||
this.iosLocationManager.stopUpdatingLocation();
|
this.iosLocationManager.manager.stopUpdatingLocation();
|
||||||
this.iosLocationManager.delegate = null;
|
this.iosLocationManager.manager.delegate = null;
|
||||||
this.listener = null;
|
this.listener = null;
|
||||||
|
this.isStarted = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
get lastKnownLocation(): locationModule.Location {
|
get lastKnownLocation(): locationModule.Location {
|
||||||
var clLocation = this.iosLocationManager.location;
|
var clLocation = this.iosLocationManager.manager.location;
|
||||||
if (clLocation) {
|
if (clLocation) {
|
||||||
return LocationManager._locationFromCLLocation(clLocation);
|
return LocationManager._locationFromCLLocation(clLocation);
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* tslint:disable */
|
||||||
|
export class iOSLocationManager implements locationModule.iOSLocationManager {
|
||||||
|
private _manager: CLLocationManager;
|
||||||
|
|
||||||
|
public get manager(): CLLocationManager {
|
||||||
|
return this._manager;
|
||||||
|
}
|
||||||
|
|
||||||
|
constructor(manager: CLLocationManager) {
|
||||||
|
this._manager = manager;
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user