mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-11-05 13:26:48 +08:00
Some improvements to location module API.
This commit is contained in:
@@ -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>
|
||||
|
||||
Reference in New Issue
Block a user