Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/google-maps/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 角度6如何获得两个位置AGM之间的距离_Angular_Google Maps_Distance - Fatal编程技术网

Angular 角度6如何获得两个位置AGM之间的距离

Angular 角度6如何获得两个位置AGM之间的距离,angular,google-maps,distance,Angular,Google Maps,Distance,使用此参考,我得到两点之间的方向 现在我想得到/计算两个位置之间的距离,我搞不清楚怎么做 在我的模块中 AgmCoreModule.forRoot({ apiKey: 'My API Key....', libraries: ['geometry'] }) 在我的组件中 getDirection() { this.origin = { lat: this.pick_latitude, lng: this.pick_longitude } this.destination =

使用此参考,我得到两点之间的方向 现在我想得到/计算两个位置之间的距离,我搞不清楚怎么做

在我的模块中

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

用于角度6+

键入以添加允许使用Google Maps类型。在终端运行中:

npm i -D @types/googlemaps
是时候告诉Angular我们已经准备好使用谷歌地图了。打开tsconfig.app.json和
tsconfig.spec.json
,将googlemaps添加到compilerOptions对象内的types数组中

注意:我只把它放在
tsconfig.app.json
文件中,有些人遇到问题,不得不将它添加到我上面提到的两个文件中

   "compilerOptions": {
    "outDir": "./out-tsc/app",
    "types": ["googlemaps"]
   },

关于@Abdelsalam Shahlol评论的注释:


对于Angular 6+,您不能从“@types/googlemaps”导入{}; 而是将其放在TS文件的顶部(第一行)/node_modules/@types/googlemaps/index.d.ts/>

在你的app.module.ts中

AgmCoreModule.forRoot({
  apiKey: 'YOUR API KEY',
  libraries: ['geometry']
}),

你可以做一个直线距离的数学,你不需要使用谷歌API和使用任何“信用”这样做

这是我为Angular 6+编写的一个lat/long-to-lat/long函数:

  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; }

如果你把“英里到米”去掉“倍数,答案是英里。对于道路距离,您只需要谷歌API,而AGM使用的地图API则不需要,相反,您需要谷歌的路线API。我还没有看到路由API的角度组件;大多数人只是用API调用编写服务。

这里已经有答案了@Ravikumar谢谢你帮助我,没有任何Google地图API来获取距离吗?我不这么认为。@Ravikumar这是使用Google地图的正确方法。“他们有一个图书馆来做这个。”帕特里西奥瓦加斯,谢谢!按照你的指示,我得到了'const distance'的值
9616.47631361913
,如果我把它除以1000,我想我可以得到公里的距离,对吗?@Rika正确。你看到的距离以米为单位。
9616.47631361913
你可以除以1000或乘以
0.001
谢谢@PatricioVargas你的回答对我很有帮助,如果你能告诉我有什么方法可以得到像距离一样的持续时间。持续时间是指从a点到b点需要多长时间@对于Angular 6+,您不能从'@types/googlemaps'导入{}将其放在TS文件的顶部(第一行)<代码>/很好的解决方案!非常感谢。
  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; }