您需要使用 Google 几何 API
https://developers.google.com/maps/documentation/javascript/geometry 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.json 并tsconfig.spec.json
并将 googlemaps 添加到 compilerOptions 对象内部的 types 数组中。
注意:我只把它放在tsconfig.app.json
文件有些人遇到了问题,必须将其添加到我上面提到的两个文件中。
"compilerOptions": {
"outDir": "./out-tsc/app",
"types": ["googlemaps"]
},
or
请注意 @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']
}),