This commit is contained in:
wangshiming
2022-02-18 10:45:58 +08:00
parent 2660fea880
commit 35d19d4d7e
4 changed files with 86 additions and 50 deletions

View File

@ -1,3 +1,13 @@
/*
* @Description :
* @Version : 1.0
* @Author : Shiming
* @Date : 2021-12-24 15:37:00
* @LastEditors : Shiming
* @LastEditTime : 2022-02-18 10:45:06
* @FilePath : \\tms-obc-web\\src\\app\\shared\\components\\amap\\amap.service.ts
* Copyright (C) 2022 huzhenhong. All rights reserved.
*/
import { Injectable } from '@angular/core';
import { Observable, Subject, throwError } from 'rxjs';
import AMapLoader from '@amap/amap-jsapi-loader';
@ -16,18 +26,18 @@ export class AmapService {
sub = new Subject<any>();
currentSub = new Subject<any>();
//计算路径驾车最优路线的长度与所需时间
drivingCompute(starts: any[], ends: any[]): Observable<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 };
return { keyword: item.detailedAddress, 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) };
const repData = { distance: (result?.routes?.[0]?.distance / 1000).toFixed(2), time: (result?.routes?.[0]?.time / 60 / 60).toFixed(2) };
this.sub.next(repData);
});
});