acorn-nativescript-geolocation
nativescript-geolocation 插件的分支。
npm i --save acorn-nativescript-geolocation

NativeScript Geolocation apple android

npm npm Build Status

用于获取当前位置、监控移动等功能的地理位置插件。

安装

在命令提示符/终端中导航到您的应用程序根目录并运行

tns plugin add nativescript-geolocation

使用方法

探索插件使用方法最好的方式是检查插件根目录中的示例应用程序。在 demo 文件夹中,您可以找到用于 TypeScript 非 Angular 应用程序的插件使用示例。请参阅 demo/app/main-page.ts

简而言之,以下是步骤

导入插件

类型脚本

import * as geolocation from "nativescript-geolocation";
import { Accuracy } from "tns-core-modules/ui/enums"; // used to describe at what accuracy the location should be get

JavaScript

var geolocation = require("nativescript-geolocation");

请求权限

geolocation.enableLocationRequest();

调用插件方法

// Get current location with high accuracy
geolocation.getCurrentLocation({ desiredAccuracy: Accuracy.high, maximumAge: 5000, timeout: 20000 })

API

属性

位置

属性 默认值 描述
latitude - 地理位置的纬度,以度为单位。
longitude - 地理位置的经度,以度为单位。
altitude - (如果可用)的海拔高度,以海平面以上的米为单位。
horizontalAccuracy - 水平精度,以米为单位。
verticalAccuracy - 垂直精度,以米为单位。
speed - 在地面上以米/秒的速度。
timestamp - 确定此位置的时间。

选项

属性 默认值 描述
desiredAccuracy? Accuracy.high 这将返回可用的最佳位置,但比 any 选项使用更多电力。 Accuracy.any 被认为是大约 100 米的精度。使用这种粗略精度通常消耗更少的电力。
updateDistance 无过滤 以米为单位的更新距离过滤器。指定更新位置的时间间隔。请参阅 Apple 文档 和/或 Google 文档
updateTime 1 分钟 位置更新的时间间隔,以毫秒为单位(iOS 上忽略)。请参阅 Google 文档
minimumUpdateTime 5 秒 位置更新之间的最小时间间隔,以毫秒为单位(iOS 上忽略)。请参阅 Google 文档
maximumAge - 以毫秒为单位接收的最旧位置。
timeout 5 分钟 等待位置的时长,以毫秒为单位。
iosAllowsBackgroundLocationUpdates false 如果启用,则需要 info.plist 中的 UIBackgroundModes 键(检查下面的提示)。允许应用程序在后台接收位置更新(Android 上忽略)。请参阅 Apple 文档
iosPausesLocationUpdatesAutomatically true 允许停用位置更新的自动暂停(Android 上忽略)。请参阅 Apple 文档
iosOpenSettingsIfLocationHasBeenDenied false enableLocationRequest 参数上的参数。如果为 true,iOS 上的设置应用程序将打开,以便用户可以更改位置服务权限。

如果iosAllowsBackgroundLocationUpdates设置为true,则info.plist文件中需要以下代码:

<key>UIBackgroundModes</key>
<array>
<string>location</string>
</array>

方法

方法 返回 描述
getCurrentLocation(options: Options) Promise 根据指定的选项(如有)获取当前位置。
watchLocation(successCallback: successCallbackType, errorCallback: errorCallbackType, options: Options) 数字 监控位置变化。
clearWatch(watchId: number) void 停止监控位置变化。期望参数为从watchLocation返回的watchId。
enableLocationRequest(always?: boolean) Promise 请求使用位置服务的权限。选项always仅适用于iOS。对于IOS的自定义提示消息,需要以下键。NSLocationAlwaysUsageDescription,NSLocationUsageDescription和NSLocationWhenInUseUsageDescription 了解更多关于其使用
isEnabled Promise 根据位置服务的可用性解析为truefalse
distance(loc1: Location, loc2: Location) 数字 计算两个位置之间的距离。返回距离(米)。

已知问题

版本冲突 – Google Play Services

如果您安装了多个使用Google Play Services的插件,可能会遇到版本冲突。为了修复此问题,您可能需要在您的app/App_Resources/Android/app.gradle文件中固定版本号。

android {  
// other stuff here

project.ext {
googlePlayServicesVersion = "11.2.+"
}
}

贡献

我们喜欢Pull Requests!查看贡献指南。如果您想贡献,但不确定从哪里开始,请寻找标记为help wanted问题

获取帮助

请,严格使用github issues报告错误请求新功能。对于一般问题和支持,请查看Stack Overflow或在我们的NativeScript社区Slack频道中向我们的专家提问。