Files
bbq/src/app/shared/components/amap/amap.service.ts
wangshiming 5f7df7339a fix bug
2021-12-15 13:19:03 +08:00

32 lines
1008 B
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { Injectable } from '@angular/core';
import { Observable, Subject } from 'rxjs';
declare var AMap: any;
@Injectable({
providedIn: 'root'
})
export class AmapService {
constructor() {}
public length = 0;
public time = 0;
sub = new Subject<any>();
//计算路径驾车最优路线的长度与所需时间
drivingCompute(starts: any[], ends: any[]): Observable<any> {
AMap.plugin('AMap.Driving', () => {
let driving = new AMap.Driving({
// 驾车路线规划策略AMap.DrivingPolicy.LEAST_TIME是最快捷模式
policy: AMap.DrivingPolicy.LEAST_TIME
});
const points = starts.concat(ends).map(item => {
return { keyword: item.address, city: item.city };
});
driving.search(points, (status: any, result: any) => {
const repData = { distance: (result.routes[0].distance / 1000).toFixed(2), time: (result.routes[0].time / 60 / 60).toFixed(2) };
this.sub.next(repData);
});
});
return this.sub;
}
}