Files
bbq/src/app/shared/components/amap/amap.service.ts
Taric Xin 62978c681e edit
2022-04-06 14:10:58 +08:00

144 lines
4.0 KiB
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.

/*
* @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';
import { amapConf } from '@conf/amap.config';
import { formatDate } from '@angular/common';
import { DateTimePickerUtil } from '@delon/util';
declare var AMap: any;
declare var AMapUI: any;
const CONFIG = amapConf;
@Injectable({
providedIn: 'root'
})
export class AmapService {
constructor() {}
sub = new Subject<any>();
currentSub = 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.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)
};
this.sub.next(repData);
});
});
return this.sub;
}
getCurrentPosition(): Observable<any> {
AMapLoader.load({
key: CONFIG.key,
version: CONFIG.version,
plugins: [
// 需要使用的的插件列表,如比例尺'AMap.Scale'等
'AMap.PathSimplifier'
],
AMapUI: {
version: CONFIG.AMapUIVersion,
plugins: ['misc/PathSimplifier'] // 需要加载的 AMapUI ui插件
}
})
.then(AMap => {
AMap.plugin('AMap.Geolocation', () => {
let driving = new AMap.Geolocation({
enableHighAccuracy: true, //是否使用高精度定位,默认:true
timeout: 10000, //超过10秒后停止定位默认5s
buttonPosition: 'RB', //定位按钮的停靠位置
buttonOffset: new AMap.Pixel(10, 20), //定位按钮与设置的停靠位置的偏移量默认Pixel(10, 20)
zoomToAccuracy: true //定位成功后是否自动调整地图视野到定位点
});
driving.getCurrentPosition((status: string, result: any) => {
if (status == 'complete') {
this.currentSub.next(result);
} else {
console.log('定位获取失败', result);
}
});
});
})
.catch(e => {
throwError(e);
});
return this.currentSub;
}
/**
* 增加标记点
*
* @param poi
*/
setPOI(poi: POI, aMap: any) {
AMapUI.loadUI(['overlay/SimpleMarker'], (SimpleMarker: any) => {
//启动页面
new SimpleMarker({
//普通文本
iconLabel: {
//普通文本
innerHTML: poi.markerLabel,
//设置样式
style: {
color: '#fff',
fontSize: '110%',
marginTop: '2px'
}
},
iconStyle: poi.color,
map: aMap,
position: poi.position
});
});
}
formatTime(time: string): string {
return `${time.slice(0, 4)}-${time.slice(4, 6)}-${time.slice(6, 8)} ${time.slice(9, 11)}:${time.slice(11, 13)}:${time.slice(13, 15)}`;
}
}
export interface POI {
markerLabel?: string;
title: string;
color: string;
position: string[];
}
export interface InfoItem {
title?: string;
content?: string;
position: string[];
}
export interface MapList {
name: string;
time: string;
lnglat: string[];
}
export interface PathList {
name: string;
points: MapList[];
}