您需要使用Google Geometry API。
https://developers.google.com/maps/documentation/javascript/geometryimport {} 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']
}),