Skip to content

Commit b2752fc

Browse files
author
Nedyalko Nikolov
committed
Merge pull request NativeScript#72 from NativeScript/nnikolov/DocsImprovements
Almost new location article.
2 parents 341faff + 0f9c6d2 commit b2752fc

File tree

2 files changed

+102
-4
lines changed

2 files changed

+102
-4
lines changed

bindings.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ A great way of using bindings is the option to create a custom expression (that
161161
```
162162

163163
As seen from the example adding an expression extends a binding syntax a little bit. Actually it is a full binding syntax - first parameter is the source property (which will be listened for changes), second parameter is the expression that will be evaluated, there is one more (third) parameter which states if the binding is twoWay or not (as mentioned earlier by default xml declaration creates a `twoWay` binding). The result of the upper example is a TextField element that will display the value of the `sourceProperty` followed by " some static text" string.
164-
Speaking of a `twoWay` binding there is a common problem that origins in the different way of storing and displaying data. For example date objects are generally stored as number or a sequence of numbers and this is not the best possible option for displaying date to the end user. Also there is another problem end user could type a date (in a way convenient to its culture for example using a `DD.MM.YYYY` format) and underlying data should convert it to a correct value. Lets see how this could be handled with NativeScript binding.
164+
Speaking of a `twoWay` binding there is a common problem that origins in the different way of storing and displaying data. Probably the best example here is the date and time objects. Generally date and time information is stored as a number or a sequence of numbers (very useful for indexing, searching and other database operations), but this is not the best possible option for displaying date to the application user. Also there is another problem when user inputs a date (in our example user types into a TextField). The result of user input will be a string (TextField.text property) - representation of a date in a convenient to user's culture way usually using the same format as for display `DD.MM.YYYY`. This string should be converted to a correct date object. Lets see how this could be handled with NativeScript binding.
165165

166166
``` XML
167167
<Page>

location.md

Lines changed: 101 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,112 @@
11
---
2-
nav-title: Location in NativeScript
2+
nav-title: Location in NativeScript
33
title: "App: Location"
44
description: How to work with geographical location data in NativeScript.
55
position: 11
66
---
77

88
# Location
99

10-
The NativeScript location module uses an accuracy criteria approach to deliver geolocation. This means that getting (or monitoring) a location is powered by the most accurate location provider that is available (for example if GPS signal is available and the GPS provider is enabled, then it will be used; if GPS is not connected, the device falls back to other available providers such as Wi-Fi networks or cell towers).
10+
The NativeScript location module uses an accuracy criteria approach to deliver geolocation. This means that getting (or monitoring) a location is powered by the most accurate location provider that is available (for example if GPS signal is available and the GPS provider is enabled, then it will be used; if GPS is not connected, the device falls back to other available providers such as Wi-Fi networks or cell towers).
1111

1212
This approach does not limit location monitoring only to a specific location provider; it can still work with all of them.
1313

14-
More information about the NativeScript location module can be found in the [API Reference](./ApiReference/location/location.md). You are advised to start with the [How-to](./ApiReference/location/HOW-TO.md) article.
14+
It is a good idea to start with the [How-to](./ApiReference/location/HOW-TO.md) article. Here are some further explanations of how API is designed to work.
15+
16+
### Getting an information about location service
17+
18+
NativeScript has an universal way to check if location service is turned on. This can be done by static `isEnabled` method of the LocationManager class.
19+
20+
> Note: This method will get an information about if the location service is enabled (in android) (any accuracy level), or if the location service is enabled for the application (iOS) (either for foreground or background use).
21+
22+
### Request a permission to use location service
23+
24+
Unfortunately here NativeScript cannot give a common approach, since Android and iOS platforms differ significantly on that.
25+
The good thing is that you can always use native API from NativeScript.
26+
27+
``` JavaScript
28+
var buttonModule = require("ui/button");
29+
var appModule = require("application");
30+
var platformModule = require("platform");
31+
32+
var onRequestButtonTap = function(args: observable.EventData) {
33+
var button = <buttonModule.Button>(args.object);
34+
if (button.android) {
35+
console.log("Application run on Android");
36+
(<android.app.Activity>appModule.android.currentContext).startActivityForResult(new android.content.Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS), 0);
37+
}
38+
else if (button.ios) {
39+
console.log("Application run on iOS");
40+
if (platformModule.device.osVersion.indexOf("8") === 0) {
41+
console.log("iOS version is greater or equal to 8.0");
42+
var iosLocationManager = CLLocationManager.alloc().init();
43+
iosLocationManager.requestWhenInUseAuthorization();
44+
}
45+
}
46+
}
47+
```
48+
``` TypeScript
49+
import buttonModule = require("ui/button");
50+
import appModule = require("application");
51+
import platformModule = require("platform");
52+
53+
var onRequestButtonTap = function(args: observable.EventData) {
54+
var button = <buttonModule.Button>(args.object);
55+
if (button.android) {
56+
console.log("Application run on Android");
57+
(<android.app.Activity>appModule.android.currentContext).startActivityForResult(new android.content.Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS), 0);
58+
}
59+
else if (button.ios) {
60+
console.log("Application run on iOS");
61+
if (platformModule.device.osVersion.indexOf("8") === 0) {
62+
console.log("iOS version is greater or equal to 8.0");
63+
var iosLocationManager = CLLocationManager.alloc().init();
64+
iosLocationManager.requestWhenInUseAuthorization();
65+
}
66+
}
67+
}
68+
```
69+
70+
> Note (iOS): For iOS there is a change introduced in 8.0 version that changes the way location service is requested. There are two modes for using location service from an application (`WhenInUse` - denotes using location service only when application is running, and `Always` mode which allows using location service in a background application). According to business scenario location service request could be done via `requestWhenInUseAuthorization` or `requestAlwaysAuthorization` methods. Both methods require a specific setting in your application to be set in `application.plist` file. Application `plist` file should contain `NSLocationWhenInUseUsageDescrition` or `NSLocationAlwaysUsageDescription` string values respectively. For iOS versions below 8.0 there is a similar string value named `NSLocationUsageDescription` which is not mandatory.
71+
72+
> Note (Android): In order to request a location service in android a special permission should be set - either `android.permission.ACCESS_COARSE_LOCATION` or `android.permission.ACCESS_FINE_LOCATION`. Fine location permission enables usage of the GPS sensor, so this is the recommended option.
73+
74+
### Getting a location
75+
For getting a location information NativeScript provides two different approaches.
76+
77+
* First approach is available using the `location` module, where there is a `getLocation` method which can be used to get a single location. This method accepts [`location options`](./ApiReference/location/Options.md) parameter. The `timeout` parameter denotes the time in milliseconds when location monitoring will be stopped (default value is 20 seconds). When `timeout` is set to 0 (zero), then last known location (if is newer than maximum age) will be returned.
78+
79+
* Second approach is using the location manager `startLocationMonitoring` method. The difference here is that location monitoring will not be stopped automatically (useful for a GPS log like application). `stopLocationMonitoring` method is used to stop location monitoring.
80+
81+
``` JavaScript
82+
var locationModule = require("location");
83+
var locationManager = new locationModule.LocationManager();
84+
var locationOptions = {
85+
desiredAccuracy: 3,
86+
updateDistance: 0,
87+
minimumUpdateTime: 5000,
88+
maximumAge: 20000
89+
};
90+
locationManager.startLocationMonitoring(function(location){
91+
console.log("Location received: " + location);
92+
}, function (error) {
93+
console.log("Location error received: " + error);
94+
}, locationOptions);
95+
```
96+
``` TypeScript
97+
import locationModule = require("location");
98+
var locationManager = new locationModule.LocationManager();
99+
var locationOptions = {
100+
desiredAccuracy: 3,
101+
updateDistance: 0,
102+
minimumUpdateTime: 5000,
103+
maximumAge: 20000
104+
};
105+
locationManager.startLocationMonitoring(function(location){
106+
console.log("Location received: " + location);
107+
}, function (error) {
108+
console.log("Location error received: " + error);
109+
}, locationOptions);
110+
```
111+
112+
More information about the NativeScript location module can be found in the [API Reference](./ApiReference/location/location.md).

0 commit comments

Comments
 (0)