- 版本:3.0.0
- GitHub:https://github.com/timdoege/nativescript-geocoding
- NPM:https://npmjs.net.cn/package/nativescript-geocoding
- 下载
- 前一天:0
- 上周:4
- 上个月:31
NativeScript Geocoding

正向地理编码请求接受一个可读的地址,并找到相应的纬度和经度值,还可以返回有关指定位置的其他信息,例如该位置的兴趣点或建筑。
注意 版本 3 是为 NativeScript 7 设计的 - 对于 NativeScript 4/5/6 使用版本 2。
安装
在命令提示符/终端导航到您的应用程序根文件夹,并运行
tns plugin add nativescript-geocoding
使用方法
探索插件使用方法的最简单方法是检查插件根文件夹中的示例应用程序。在 demo-angular
文件夹中,您可以找到用于 Angular 应用程序的插件使用方法(demo-angular/src/app/home/home.component.ts
)。
步骤
导入插件
TypeScript
import * as geocoding from "nativescript-geocoding";
JavaScript
var geocoding = require("nativescript-geocoding");
调用插件
geocoding.getLocationFromName('Copenhagen').then(loc => {
console.log('Found ', loc);
}, function (e) {
console.log("Error: " + (e.message || e));
});
或者如果您可能想要处理多个匹配项
geocoding.getLocationListFromName(searchBar.text, 5).then(locations => {
console.log('Found ', locations.length);
if (locations.length > 0) {
this.location = locations[0];
}
}, function (e) {
console.log('Error: ' + (e.message || e));
});
示例
要运行 Angular 示例
$ cd nativescript-geocoding/src
$ npm run demo.ios
$ npm run demo.android
原生 API
Android 实现
- 详情:https://developer.android.com.cn/reference/android/location/Geocoder
返回一个地址数组,这些地址可以描述命名位置,可能是一个地名,如“Dalvik,冰岛”,一个地址,如“1600 安菲泰拉特公园大道,山景城,加利福尼亚州”,一个机场代码,如“SFO”等。返回的地址将为提供给此类构造函数的区域设置进行本地化。
查询将阻塞,返回的值将通过网络查找获得。结果是最佳猜测,并不保证是有意义或正确的。可能需要从主 UI 线程之外的其他线程调用此方法。
参数 locationName String:用户提供的位置描述 maxResults int:要返回的最大结果数。建议使用较小的数字(1 到 5)
iOS 实现
- 详情:https://developer.apple.com/documentation/corelocation/converting_between_coordinates_and_user-friendly_place_names
根据用户提供的信息的精度,您可能会收到一个或多个结果。例如,传递字符串“100 Main St.,USA”可能会返回许多结果,除非您还指定了搜索区域或附加细节。为了帮助您决定哪个结果是正确的,地理编码器实际上返回 CLPlacemark 对象,这些对象包含坐标和您提供的原始信息。
注意事项
-
对于 Android,最低 API 级别为 21,以便获取详细的位置信息。
-
在 iOS 上,您可能会收到以下消息
Error: iOS CLGeocoder error : The operation couldn’t be completed. (kCLErrorDomain error 8.)
如果 CLGeocoder 无法找到搜索字符串的内容。