mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-16 03:31:45 +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">
|
||||
<DependentUpon>details-page.xml</DependentUpon>
|
||||
</TypeScriptCompile>
|
||||
<TypeScriptCompile Include="apps\tests\app\binding_tests.ts">
|
||||
<DependentUpon>binding_tests.xml</DependentUpon>
|
||||
<TypeScriptCompile Include="apps\template-master-detail\main-page.ts">
|
||||
<DependentUpon>main-page.xml</DependentUpon>
|
||||
</TypeScriptCompile>
|
||||
<TypeScriptCompile Include="apps\tests\app\location-example.ts">
|
||||
<DependentUpon>location-example.xml</DependentUpon>
|
||||
</TypeScriptCompile>
|
||||
<TypeScriptCompile Include="apps\tests\app\location-example.ts" />
|
||||
<TypeScriptCompile Include="apps\tests\app\style_props.ts" />
|
||||
<TypeScriptCompile Include="apps\tests\console-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="js-libs\easysax\easysax.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\pages\navigation\pageA-new-activity.ts" />
|
||||
<TypeScriptCompile Include="apps\tests\pages\navigation\pageA.ts" />
|
||||
@ -527,13 +538,6 @@
|
||||
</TypeScriptCompile>
|
||||
<TypeScriptCompile Include="local-settings\local-settings-common.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>
|
||||
<TypeScriptCompile Include="libjs.d.ts" />
|
||||
@ -549,7 +553,7 @@
|
||||
<Content Include="apps\template-master-detail\main-page.minWH600.xml" />
|
||||
<Content Include="apps\TelerikNEXT\images\background.jpg" />
|
||||
<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\pages\files\other.xml" />
|
||||
<Content Include="apps\tests\pages\files\test.android.phone.xml" />
|
||||
@ -699,9 +703,6 @@
|
||||
<DependentUpon>file-system.d.ts</DependentUpon>
|
||||
</TypeScriptCompile>
|
||||
<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="apps\wrap-layout-demo\app.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,94 +1,69 @@
|
||||
import pages = require("ui/page");
|
||||
import gridLayoutModule = require("ui/layouts/grid-layout");
|
||||
import bm = require("ui/button");
|
||||
import locationModule = require("location");
|
||||
import dialogsModule = require("ui/dialogs");
|
||||
import trace = require("trace");
|
||||
import platformModule = require("platform");
|
||||
import textViewModule = require("ui/text-view");
|
||||
import observable = require("data/observable");
|
||||
import appModule = require("application");
|
||||
|
||||
export function createPage() {
|
||||
function printLocation(location: locationModule.Location) {
|
||||
return "longitude: " + location.longitude + ", " +
|
||||
"latitude: " + location.latitude + ", " +
|
||||
"timeStamp: " + location.timestamp + ", " +
|
||||
"horizontalAccuracy: " + location.horizontalAccuracy + ", " +
|
||||
"verticalAccuracy: " + location.verticalAccuracy;
|
||||
}
|
||||
var locationManager = new locationModule.LocationManager();
|
||||
|
||||
var locationManager = new locationModule.LocationManager();
|
||||
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 });
|
||||
}
|
||||
|
||||
var textView = new textViewModule.TextView();
|
||||
textView.editable = false;
|
||||
textView.style.fontSize = 8;
|
||||
function printLocation(location: locationModule.Location) {
|
||||
return "longitude: " + location.longitude + ", " +
|
||||
"latitude: " + location.latitude + ", " +
|
||||
"timeStamp: " + location.timestamp + ", " +
|
||||
"horizontalAccuracy: " + location.horizontalAccuracy + ", " +
|
||||
"verticalAccuracy: " + location.verticalAccuracy;
|
||||
}
|
||||
|
||||
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});
|
||||
});
|
||||
export function onStopButtonTap(args: observable.EventData) {
|
||||
locationManager.stopLocationMonitoring();
|
||||
}
|
||||
|
||||
var stopButton = new bm.Button();
|
||||
stopButton.text = "Stop";
|
||||
stopButton.on(bm.knownEvents.tap, function (eventData) {
|
||||
locationManager.stopLocationMonitoring();
|
||||
});
|
||||
export function onRequestButtonTap(args: observable.EventData) {
|
||||
checkLocationService(page);
|
||||
}
|
||||
|
||||
var grid = new gridLayoutModule.GridLayout();
|
||||
var page: pages.Page;
|
||||
|
||||
grid.addRow(new gridLayoutModule.ItemSpec(1, gridLayoutModule.GridUnitType.auto));
|
||||
grid.addRow(new gridLayoutModule.ItemSpec());
|
||||
|
||||
grid.addColumn(new gridLayoutModule.ItemSpec(100, gridLayoutModule.GridUnitType.pixel));
|
||||
grid.addColumn(new gridLayoutModule.ItemSpec());
|
||||
export function pageLoaded(args: observable.EventData) {
|
||||
page = <pages.Page>(args.object);
|
||||
}
|
||||
|
||||
gridLayoutModule.GridLayout.setColumn(startButton, 0);
|
||||
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()) {
|
||||
trace.write("LocationManager.isEnabled", trace.categories.Test, trace.messageType.info);
|
||||
}
|
||||
else {
|
||||
trace.write("LocationManager.is not Enabled", trace.categories.Test, trace.messageType.info);
|
||||
if (page.android) {
|
||||
dialogsModule.confirm({
|
||||
message: "Location service is not enabled!",
|
||||
okButtonText: "Open location settings ...",
|
||||
cancelButtonText: "Cancel", title: "Confirm"
|
||||
}).then((r) => {
|
||||
if (r) {
|
||||
(<android.app.Activity>page._context).startActivityForResult(new android.content.Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS), 0);
|
||||
}
|
||||
}, null);
|
||||
}
|
||||
else if (page.ios) {
|
||||
if (platformModule.device.osVersion.indexOf("8") === 0) {
|
||||
var iosLocationManager = CLLocationManager.alloc().init();
|
||||
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;
|
||||
var checkLocationService = function (page: pages.Page) {
|
||||
if (locationModule.LocationManager.isEnabled()) {
|
||||
console.log("LocationManager.isEnabled");
|
||||
}
|
||||
else {
|
||||
console.log("LocationManager.is not Enabled");
|
||||
if (page.android) {
|
||||
dialogsModule.confirm({
|
||||
message: "Location service is not enabled!",
|
||||
okButtonText: "Open location settings ...",
|
||||
cancelButtonText: "Cancel", title: "Confirm"
|
||||
}).then((r) => {
|
||||
if (r) {
|
||||
// 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);
|
||||
}
|
||||
else if (page.ios) {
|
||||
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();
|
||||
iosLocationManager.requestWhenInUseAuthorization();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
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 () {
|
||||
if (!locationIsEnabled) {
|
||||
console.log("Location service is not enabled!!!");
|
||||
return;
|
||||
}
|
||||
// <snippet module="location" title="location">
|
||||
@ -182,7 +183,7 @@ export var testSnippet = function (done) {
|
||||
//
|
||||
// ``` JavaScript
|
||||
// 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) {
|
||||
//console.log('Location received: ' + location);
|
||||
// <hide>
|
||||
|
@ -1,14 +1,15 @@
|
||||
|
||||
import timer = require("timer");
|
||||
import locationManagerModule = require("location/location-manager");
|
||||
import timer = require("timer");
|
||||
//import locationManagerModule = require("location/location-manager");
|
||||
import defModule = require("location");
|
||||
|
||||
// merge the exports of the types module with the exports of this file
|
||||
import merger = require("utils/module-merge");
|
||||
declare var exports;
|
||||
merger.merge(locationManagerModule, exports);
|
||||
//import merger = require("utils/module-merge");
|
||||
//declare var exports;
|
||||
//merger.merge(locationManagerModule, exports);
|
||||
|
||||
export class Location {
|
||||
var defaultGetLocationTimeout = 20000;
|
||||
|
||||
export class Location implements defModule.Location {
|
||||
public latitude: number;
|
||||
public longitude: number;
|
||||
|
||||
@ -29,7 +30,7 @@ export class Location {
|
||||
|
||||
export var getLocation = function (options?: defModule.Options): Promise<defModule.Location> {
|
||||
var timerId;
|
||||
var locationManager = new locationManagerModule.LocationManager();
|
||||
var locationManager = new defModule.LocationManager();
|
||||
|
||||
if (options && (0 === options.timeout)) {
|
||||
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) => {
|
||||
if (!locationManagerModule.LocationManager.isEnabled()) {
|
||||
if (!defModule.LocationManager.isEnabled()) {
|
||||
return reject(new Error("Location service is disabled"));
|
||||
}
|
||||
locationManager.startLocationMonitoring(function (location: defModule.Location) {
|
||||
@ -89,7 +90,7 @@ export var getLocation = function (options?: defModule.Options): Promise<defModu
|
||||
timerId = timer.setTimeout(function () {
|
||||
locationManager.stopLocationMonitoring();
|
||||
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 appModule = require("application");
|
||||
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
|
||||
public desiredAccuracy: number;
|
||||
|
||||
// The minimum distance (measured in meters) a device must move horizontally before an update event is generated.
|
||||
public updateDistance: number;
|
||||
|
||||
// minimum time interval between location updates, in milliseconds (android only)
|
||||
public minimumUpdateTime: number;
|
||||
|
||||
private androidLocationManager: any;
|
||||
private androidLocationManager: locationModule.AndroidLocationManager;
|
||||
|
||||
private _locationListener: any;
|
||||
|
||||
@ -60,7 +71,6 @@ export class LocationManager {
|
||||
location.direction = androidLocation.getBearing();
|
||||
location.timestamp = new Date(androidLocation.getTime());
|
||||
location.android = androidLocation;
|
||||
//console.dump(location);
|
||||
return location;
|
||||
}
|
||||
|
||||
@ -108,17 +118,18 @@ export class LocationManager {
|
||||
}
|
||||
|
||||
constructor() {
|
||||
//super();
|
||||
this.desiredAccuracy = enums.Accuracy.any;
|
||||
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) {
|
||||
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.desiredAccuracy) {
|
||||
this.desiredAccuracy = options.desiredAccuracy;
|
||||
@ -127,15 +138,17 @@ export class LocationManager {
|
||||
this.updateDistance = options.updateDistance;
|
||||
}
|
||||
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._onError = onError;
|
||||
this.locationListener.maximumAge = (options && ("number" === typeof options.maximumAge)) ? options.maximumAge : undefined;
|
||||
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) {
|
||||
if (onError) {
|
||||
@ -145,21 +158,22 @@ export class LocationManager {
|
||||
}
|
||||
|
||||
public stopLocationMonitoring() {
|
||||
this.androidLocationManager.removeUpdates(this.locationListener);
|
||||
this.androidLocationManager.manager.removeUpdates(this.locationListener);
|
||||
this.isStarted = false;
|
||||
}
|
||||
|
||||
get lastKnownLocation(): locationModule.Location {
|
||||
var criteria = new android.location.Criteria();
|
||||
criteria.setAccuracy((this.desiredAccuracy === enums.Accuracy.high) ? android.location.Criteria.ACCURACY_FINE : android.location.Criteria.ACCURACY_COARSE);
|
||||
try {
|
||||
var providers = this.androidLocationManager.getProviders(criteria, false);
|
||||
var providers = this.androidLocationManager.manager.getProviders(criteria, false);
|
||||
var it = providers.iterator();
|
||||
var location: android.location.Location;
|
||||
var tempLocation: android.location.Location;
|
||||
while (it.hasNext()) {
|
||||
var element = it.next();
|
||||
//console.log('found provider: ' + element);
|
||||
tempLocation = this.androidLocationManager.getLastKnownLocation(element);
|
||||
tempLocation = this.androidLocationManager.manager.getLastKnownLocation(element);
|
||||
if (!location) {
|
||||
location = tempLocation;
|
||||
}
|
||||
@ -179,4 +193,25 @@ export class LocationManager {
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
53
location/location.d.ts
vendored
53
location/location.d.ts
vendored
@ -121,11 +121,6 @@ declare module "location" {
|
||||
*/
|
||||
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.
|
||||
*/
|
||||
@ -148,13 +143,49 @@ declare module "location" {
|
||||
* Returns last known location from device's location services or null of no known last 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.
|
||||
* 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.
|
||||
* @param options An optional object specifying location update settings.
|
||||
*/
|
||||
/**
|
||||
* 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.
|
||||
* 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.
|
||||
*/
|
||||
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 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 {
|
||||
public static ObjCProtocols = [CLLocationManagerDelegate];
|
||||
@ -44,22 +49,30 @@ class LocationListenerImpl extends NSObject implements CLLocationManagerDelegate
|
||||
}
|
||||
|
||||
public locationManagerDidFailWithError(manager, error): void {
|
||||
// console.error('location error received ' + error.localizedDescription);
|
||||
if (this._onError) {
|
||||
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
|
||||
// we might need some predefined values here like 'any' and 'high'
|
||||
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;
|
||||
|
||||
private iosLocationManager: CLLocationManager;
|
||||
private iosLocationManager: locationModule.iOSLocationManager;
|
||||
private listener: any;
|
||||
|
||||
public static _locationFromCLLocation(clLocation: CLLocation): locationModule.Location {
|
||||
@ -71,9 +84,9 @@ export class LocationManager {
|
||||
location.verticalAccuracy = clLocation.verticalAccuracy;
|
||||
location.speed = clLocation.speed;
|
||||
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;
|
||||
//console.dump(location);
|
||||
return location;
|
||||
}
|
||||
|
||||
@ -110,9 +123,11 @@ export class LocationManager {
|
||||
}
|
||||
|
||||
constructor() {
|
||||
//super();
|
||||
this.desiredAccuracy = enums.Accuracy.any;
|
||||
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) {
|
||||
@ -127,24 +142,39 @@ export class LocationManager {
|
||||
}
|
||||
|
||||
this.listener = LocationListenerImpl.new().initWithLocationErrorOptions(onLocation, onError, options);
|
||||
this.iosLocationManager.delegate = this.listener;
|
||||
this.iosLocationManager.desiredAccuracy = this.desiredAccuracy;
|
||||
this.iosLocationManager.distanceFilter = this.updateDistance;
|
||||
this.iosLocationManager.startUpdatingLocation();
|
||||
this.iosLocationManager.manager.delegate = this.listener;
|
||||
this.iosLocationManager.manager.desiredAccuracy = this.desiredAccuracy;
|
||||
this.iosLocationManager.manager.distanceFilter = this.updateDistance;
|
||||
this.iosLocationManager.manager.startUpdatingLocation();
|
||||
this.isStarted = true;
|
||||
}
|
||||
}
|
||||
|
||||
public stopLocationMonitoring() {
|
||||
this.iosLocationManager.stopUpdatingLocation();
|
||||
this.iosLocationManager.delegate = null;
|
||||
this.iosLocationManager.manager.stopUpdatingLocation();
|
||||
this.iosLocationManager.manager.delegate = null;
|
||||
this.listener = null;
|
||||
this.isStarted = false;
|
||||
}
|
||||
|
||||
get lastKnownLocation(): locationModule.Location {
|
||||
var clLocation = this.iosLocationManager.location;
|
||||
var clLocation = this.iosLocationManager.manager.location;
|
||||
if (clLocation) {
|
||||
return LocationManager._locationFromCLLocation(clLocation);
|
||||
}
|
||||
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