Angular 6如何获取两个位置之间的距离 AGM

3

我使用这个参考 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 }
}

我的问题: 当我使用上述参考获取方向路径时,如何获取两个点之间的距离?请给我建议(例如使用 Google 地图 API)。我正在使用 Angular 6。


这个问题已经在这里得到了回答:https://dev59.com/BHVD5IYBdhLWcg3wTJrF#27943 - Ravikumar Rajendran
1
@Ravikumar 谢谢你的帮助,有没有什么谷歌地图API可以获取距离? - Rika
我不这么认为。 - Ravikumar Rajendran
1
@Ravikumar,这是使用Angular Google Maps的正确方法。他们有一个库可以实现这个功能。 - Patricio Vargas
@PatricioVargas 谢谢! - Ravikumar Rajendran
@ Ravikumar 我的意思不是这个。你可以在 Google 地图上查看几何图形,或查看我的回答。 - Patricio Vargas
2个回答

10
您需要使用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 Maps, 在终端中输入以下命令:

npm i -D @types/googlemaps

现在是告诉 Angular 我们已经准备好使用 Google Maps 类型的时间了。 打开你的 tsconfig.app.json 和 tsconfig.spec.json 文件,并在 compilerOptions 对象中的 types 数组中添加 googlemaps。

注意:我只在 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']
}),

1
按照您的指示,我得到了 const distance 的值为 9616.47631361913。如果我将它除以1000,我认为我可以得到以 公里 为单位的距离。我是正确的吗? - Rika
2
@Rika 正确。你看到的距离是以米为单位的 9616.47631361913,你可以除以1000或乘以 0.001 - Patricio Vargas
谢谢@PatricioVargas,您的答案对我非常有帮助。如果可以的话,请告诉我是否有任何方法可以像获取距离一样获取持续时间 - Rika
为什么需要MapsAPILoader?它在哪里被使用了呢?我正在处理同样的问题。 - Tomas Katz
2
对于 Angular 6+,你不能这样做 import {} from '@types/googlemaps';。相反,将以下代码放在 TS 文件的顶部(第一行)。/// <reference path=".<relative_path>/node_modules/@types/googlemaps/index.d.ts"/> - Abdelsalam Shahlol
显示剩余4条评论

3

您可以使用数学计算直线距离,不必使用Google API并消耗任何“信用”。

这是我为在 Angular 6+ 中使用编写的经纬度到经纬度函数:

  asTheCrowFlies(x1: number, y1: number, x2: number, y2: number) {
var result = 0;
const RADIANS: number = 180 / 3.14159265;
const METRES_IN_MILE: number = 1609.34;

if (x1 == x2 && y1 == y2) {
  result = 0;

} else {
  // Calculating Distance between Points
  var lt1 = x1 / RADIANS;
  var lg1 = y1 / RADIANS;
  var lt2 = x2 / RADIANS;
  var lg2 = y2 / RADIANS;

  // radius of earth in miles (3,958.8) * metres in a mile * position on surface of sphere...
  result = (3958.8 * METRES_IN_MILE) * Math.acos(Math.sin(lt1) * Math.sin(lt2) + Math.cos(lt1) * Math.cos(lt2) * Math.cos(lg2 - lg1));
}
return result; }

如果您去掉"英里转米"的乘数,答案就是英里。您只需要使用Google API来计算道路距离,而这并不是使用AGM的MAP API完成的,相反,您需要使用Google的ROUTE API。我还没有看到针对ROUTE API的Angular组件;大多数人只是编写一个带有API调用的服务。

好的解决方案!谢谢。 - e.k

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接