32 lines
1008 B
TypeScript
32 lines
1008 B
TypeScript
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;
|
||
}
|
||
}
|