mirror of
https://github.com/mayswind/ezbookkeeping.git
synced 2026-05-17 08:14:25 +08:00
support amap
This commit is contained in:
+3
-1
@@ -2,10 +2,12 @@ const baseApiUrlPath = '/api';
|
||||
const baseProxyUrlPath = '/proxy';
|
||||
const googleMapJavascriptUrl = 'https://maps.googleapis.com/maps/api/js';
|
||||
const baiduMapJavascriptUrl = 'https://api.map.baidu.com/api?v=3.0';
|
||||
const amapJavascriptUrl = 'https://webapi.amap.com/maps?v=2.0';
|
||||
|
||||
export default {
|
||||
baseApiUrlPath: baseApiUrlPath,
|
||||
baseProxyUrlPath: baseProxyUrlPath,
|
||||
googleMapJavascriptUrl: googleMapJavascriptUrl,
|
||||
baiduMapJavascriptUrl: baiduMapJavascriptUrl
|
||||
baiduMapJavascriptUrl: baiduMapJavascriptUrl,
|
||||
amapJavascriptUrl: amapJavascriptUrl
|
||||
}
|
||||
|
||||
@@ -0,0 +1,165 @@
|
||||
import { asyncLoadAssets } from "@/lib/misc.js";
|
||||
import services from "@/lib/services.js";
|
||||
import settings from "@/lib/settings.js";
|
||||
import logger from '@/lib/logger.js';
|
||||
|
||||
const amapHolder = {
|
||||
AMap: null
|
||||
};
|
||||
|
||||
export function loadAmapAssets() {
|
||||
if (amapHolder.AMap) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!window._AMapSecurityConfig) {
|
||||
const amapSecurityConfig = {};
|
||||
|
||||
if (settings.getAmapSecurityVerificationMethod() === 'plain') {
|
||||
amapSecurityConfig.securityJsCode = settings.getAmapApplicationSecret();
|
||||
}
|
||||
|
||||
window._AMapSecurityConfig = amapSecurityConfig;
|
||||
}
|
||||
|
||||
if (!window.onAMapCallback) {
|
||||
window.onAMapCallback = () => {
|
||||
amapHolder.AMap = window.AMap;
|
||||
};
|
||||
}
|
||||
|
||||
return asyncLoadAssets('js', services.generateAmapJavascriptUrl('onAMapCallback'));
|
||||
}
|
||||
|
||||
export function createAmapHolder() {
|
||||
return {
|
||||
mapProvider: 'amap',
|
||||
dependencyLoaded: !!amapHolder.AMap,
|
||||
inited: false,
|
||||
defaultZoomLevel: 14,
|
||||
minZoomLevel: 1,
|
||||
amapInstance: null,
|
||||
amapToolbar: null,
|
||||
amapCenterPosition: null,
|
||||
amapCenterMarker: null
|
||||
};
|
||||
}
|
||||
|
||||
export function createAmapInstance(mapHolder, mapContainer, options) {
|
||||
if (!amapHolder.AMap) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const AMap = amapHolder.AMap;
|
||||
const amapInstance = new AMap.Map(mapContainer, {
|
||||
zoom: options.zoomLevel,
|
||||
center: [ options.initCenter.longitude, options.initCenter.latitude ],
|
||||
zooms: [ 1, 19 ],
|
||||
jogEnable: false
|
||||
});
|
||||
|
||||
AMap.plugin([
|
||||
'AMap.ToolBar'
|
||||
], function() {
|
||||
mapHolder.amapToolbar = new AMap.ToolBar({
|
||||
position: 'LT'
|
||||
});
|
||||
|
||||
amapInstance.addControl(mapHolder.amapToolbar);
|
||||
});
|
||||
|
||||
mapHolder.amapInstance = amapInstance;
|
||||
mapHolder.inited = true;
|
||||
}
|
||||
|
||||
export function setAmapCenterTo(mapHolder, center, zoomLevel) {
|
||||
if (!amapHolder.AMap || !mapHolder.amapInstance) {
|
||||
return;
|
||||
}
|
||||
|
||||
const AMap = amapHolder.AMap;
|
||||
|
||||
if (amapHolder.amapCenterPosition
|
||||
&& amapHolder.amapCenterPosition.originalLongitude === center.longitude
|
||||
&& amapHolder.amapCenterPosition.originalLatitude === center.latitude
|
||||
&& amapHolder.amapCenterPosition.convertedLongitude
|
||||
&& amapHolder.amapCenterPosition.convertedLatitude
|
||||
) {
|
||||
mapHolder.amapInstance.setZoomAndCenter(zoomLevel, new AMap.LngLat(amapHolder.amapCenterPosition.convertedLongitude, amapHolder.amapCenterPosition.convertedLatitude));
|
||||
return;
|
||||
}
|
||||
|
||||
amapHolder.amapCenterPosition = {
|
||||
originalLongitude: center.longitude,
|
||||
originalLatitude: center.latitude,
|
||||
convertedLongitude: null,
|
||||
convertedLatitude: null
|
||||
};
|
||||
|
||||
const centerPoint = new AMap.LngLat(center.longitude, center.latitude);
|
||||
|
||||
AMap.convertFrom(centerPoint, 'gps', (status, result) => {
|
||||
let convertedCenterPoint = centerPoint;
|
||||
|
||||
if (result.info !== 'ok' || !result.locations) {
|
||||
logger.warn('amap geo position convert failed');
|
||||
} else {
|
||||
convertedCenterPoint = result.locations[0];
|
||||
amapHolder.amapCenterPosition.convertedLongitude = convertedCenterPoint.getLng();
|
||||
amapHolder.amapCenterPosition.convertedLatitude = convertedCenterPoint.getLat();
|
||||
}
|
||||
|
||||
mapHolder.amapInstance.setZoomAndCenter(zoomLevel, convertedCenterPoint);
|
||||
});
|
||||
}
|
||||
|
||||
export function setAmapCenterMaker(mapHolder, position) {
|
||||
if (!amapHolder.AMap || !mapHolder.amapInstance) {
|
||||
return;
|
||||
}
|
||||
|
||||
const AMap = amapHolder.AMap;
|
||||
const setMaker = function (point) {
|
||||
if (!mapHolder.amapCenterMarker) {
|
||||
mapHolder.amapCenterMarker = new AMap.Marker({
|
||||
position: point
|
||||
});
|
||||
mapHolder.amapInstance.add(mapHolder.amapCenterMarker);
|
||||
} else {
|
||||
mapHolder.amapCenterMarker.setPosition(point);
|
||||
}
|
||||
}
|
||||
|
||||
if (amapHolder.amapCenterPosition
|
||||
&& amapHolder.amapCenterPosition.originalLongitude === position.longitude
|
||||
&& amapHolder.amapCenterPosition.originalLatitude === position.latitude
|
||||
&& amapHolder.amapCenterPosition.convertedLongitude
|
||||
&& amapHolder.amapCenterPosition.convertedLatitude
|
||||
) {
|
||||
setMaker(new AMap.LngLat(amapHolder.amapCenterPosition.convertedLongitude, amapHolder.amapCenterPosition.convertedLatitude));
|
||||
return;
|
||||
}
|
||||
|
||||
const markerPoint = new AMap.LngLat(position.longitude, position.latitude);
|
||||
|
||||
AMap.convertFrom(markerPoint, 'gps', (status, result) => {
|
||||
let convertedMarkPoint = markerPoint;
|
||||
|
||||
if (result.info !== 'ok' || !result.locations) {
|
||||
logger.warn('amap geo position convert failed');
|
||||
} else {
|
||||
convertedMarkPoint = result.locations[0];
|
||||
}
|
||||
|
||||
setMaker(convertedMarkPoint);
|
||||
});
|
||||
}
|
||||
|
||||
export function removeAmapCenterMaker(mapHolder) {
|
||||
if (!mapHolder.amapInstance || !mapHolder.amapCenterMarker) {
|
||||
return;
|
||||
}
|
||||
|
||||
mapHolder.amapInstance.remove(mapHolder.amapCenterMarker);
|
||||
mapHolder.amapCenterMarker = null;
|
||||
}
|
||||
@@ -27,6 +27,15 @@ import {
|
||||
removeBaiduMapCenterMaker
|
||||
} from './baidumap.js';
|
||||
|
||||
import {
|
||||
loadAmapAssets,
|
||||
createAmapHolder,
|
||||
createAmapInstance,
|
||||
setAmapCenterTo,
|
||||
setAmapCenterMaker,
|
||||
removeAmapCenterMaker
|
||||
} from './amap.js';
|
||||
|
||||
export function loadMapAssets(language) {
|
||||
if (settings.getMapProvider() === 'openstreetmap') {
|
||||
return loadLeafletMapAssets(language);
|
||||
@@ -34,6 +43,8 @@ export function loadMapAssets(language) {
|
||||
return loadGoogleMapAssets(language);
|
||||
} else if (settings.getMapProvider() === 'baidumap') {
|
||||
return loadBaiduMapAssets(language);
|
||||
} else if (settings.getMapProvider() === 'amap') {
|
||||
return loadAmapAssets(language);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -44,6 +55,8 @@ export function createMapHolder() {
|
||||
return createGoogleMapHolder();
|
||||
} else if (settings.getMapProvider() === 'baidumap') {
|
||||
return createBaiduMapHolder();
|
||||
} else if (settings.getMapProvider() === 'amap') {
|
||||
return createAmapHolder();
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
@@ -60,6 +73,8 @@ export function initMapInstance(mapHolder, mapContainer, options) {
|
||||
createGoogleMapInstance(mapHolder, mapContainer, options);
|
||||
} else if (mapHolder.mapProvider === 'baidumap') {
|
||||
createBaiduMapInstance(mapHolder, mapContainer, options);
|
||||
} else if (mapHolder.mapProvider === 'amap') {
|
||||
createAmapInstance(mapHolder, mapContainer, options);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -74,6 +89,8 @@ export function setMapCenterTo(mapHolder, center, zoomLevel) {
|
||||
setGoogleMapCenterTo(mapHolder, center, zoomLevel);
|
||||
} else if (mapHolder.mapProvider === 'baidumap') {
|
||||
setBaiduMapCenterTo(mapHolder, center, zoomLevel);
|
||||
} else if (mapHolder.mapProvider === 'amap') {
|
||||
setAmapCenterTo(mapHolder, center, zoomLevel);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -88,6 +105,8 @@ export function setMapCenterMarker(mapHolder, position) {
|
||||
setGoogleMapCenterMaker(mapHolder, position);
|
||||
} else if (mapHolder.mapProvider === 'baidumap') {
|
||||
setBaiduMapCenterMaker(mapHolder, position);
|
||||
} else if (mapHolder.mapProvider === 'amap') {
|
||||
setAmapCenterMaker(mapHolder, position);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -102,5 +121,7 @@ export function removeMapCenterMarker(mapHolder) {
|
||||
removeGoogleMapCenterMaker(mapHolder);
|
||||
} else if (mapHolder.mapProvider === 'baidumap') {
|
||||
removeBaiduMapCenterMaker(mapHolder);
|
||||
} else if (mapHolder.mapProvider === 'amap') {
|
||||
removeAmapCenterMaker(mapHolder);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -420,5 +420,8 @@ export default {
|
||||
},
|
||||
generateBaiduMapJavascriptUrl: (callbackFnName) => {
|
||||
return `${api.baiduMapJavascriptUrl}&ak=${settings.getBaiduMapAK()}&callback=${callbackFnName}`;
|
||||
},
|
||||
generateAmapJavascriptUrl: (callbackFnName) => {
|
||||
return `${api.amapJavascriptUrl}&key=${settings.getAmapApplicationKey()}&callback=${callbackFnName}`;
|
||||
}
|
||||
};
|
||||
|
||||
@@ -171,5 +171,8 @@ export default {
|
||||
isMapDataFetchProxyEnabled: () => getServerSetting('mp') === '1',
|
||||
getGoogleMapAPIKey: () => getServerSetting('gmak'),
|
||||
getBaiduMapAK: () => getServerSetting('bmak'),
|
||||
getAmapApplicationKey: () => getServerSetting('amak'),
|
||||
getAmapSecurityVerificationMethod: () => getServerSetting('amsv'),
|
||||
getAmapApplicationSecret: () => getServerSetting('amas'),
|
||||
clearSettings: clearSettings
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user