angular - Angular 6如何获取两个位置AGM之间的距离
问题描述
我使用此参考https://www.npmjs.com/package/agm-direction获得两点之间的方向 现在我想获取/计算两个位置之间的距离,但我很困惑如何做到这一点。
在我的模块中
AgmCoreModule.forRoot({
apiKey: 'My API Key....',
libraries: ['geometry']
})
在我的组件中
getDirection() {
this.origin = { lat: this.pick_latitude, lng: this.pick_longitude }
this.destination = { lat: this.drop_latitude, lng: this.drop_longitude }
}
我的问题:当我使用上述参考来获取方向路径时,如何获得两点之间的距离?请给我建议(谷歌地图 API 等)。我正在使用 Angular 6
解决方案
您需要使用 Google Geometry API
https://developers.google.com/maps/documentation/javascript/geometry
import {} from '@types/googlemaps';
import { AgmCoreModule, MapsAPILoader } from "@agm/core";
calculateDistance() {
const mexicoCity = new google.maps.LatLng(19.432608, -99.133209.);
const jacksonville = new google.maps.LatLng(40.730610, -73.935242.);
const distance = google.maps.geometry.spherical.computeDistanceBetween(nyc, london);
}
对于 Angular 6+
要添加的类型允许 Angular 使用 Google 地图类型。在您的终端运行:
npm i -D @types/googlemaps
是时候告诉 Angular 我们已经准备好使用 Google 地图类型了。打开您的 tsconfig.app.jsontsconfig.spec.json
并将 googlemaps 添加到 compilerOptions 对象内的 types 数组。
注意:我只把它放在tsconfig.app.json
一些人遇到问题的文件中,并且必须将它添加到我上面提到的两个文件中。
"compilerOptions": {
"outDir": "./out-tsc/app",
"types": ["googlemaps"]
},
或者
注意@Abdelsalam Shahlol 评论:
对于 Angular 6+,你不能这样做 import {} from '@types/googlemaps'; 而是将其放在 TS 文件的顶部(第一行)。/// /node_modules/@types/googlemaps/index.d.ts"/>
在你的 app.module.ts
AgmCoreModule.forRoot({
apiKey: 'YOUR API KEY',
libraries: ['geometry']
}),
推荐阅读
- macros - 从 sicp 宏中的周围抽象中捕获变量
- javascript - 如何防止 safari 在容器的孩子之外滚动捕捉?
- flutter - 我似乎无法理解的错误
- vue.js - 从 Vue 隐藏对象属性
- java - Java 异常只打印消息
- visual-studio-2019 - 无法安装 Visual Studio 2019 - 引导安装程序失败
- excel - 隐藏列切换
- python - 如何添加一个整数值上升每个项目python
- twitter - 使用 JSON 的 Hugo Shortcode 抛出“坏字符 U+0022 错误”
- python - 如何自动从 Heroku 上的 git 子模块中提取最新提交?