1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
| import AMapLoader from '@amap/amap-jsapi-loader';
const mapPlugin = [ ];
const key = 'your key';
const styleId = 'your styleId'
const defaultCityCode = '城市号'
export default class Map { constructor(opt) { this.map = null; this.initMap(opt); } destroy() { this.map.destroy(); } initMap({ domId, geolocation }) { return new Promise((resolve, reject) => { AMapLoader.load({ key: key, plugins: mapPlugin, AMapUI: { plugins: [], }, }) .then(AMap => { this.map = new AMap.Map(domId, { mapStyle: `amap://styles/${styleId}`, }); if (geolocation) { this.initGetLocation(); } if (mapPlugin.length > 0) { this.initMapPlugin(mapPlugin); } resolve(); }) .catch(e => { console.log(e); reject(); }); }); } getLocation() { this.geolocation.getCurrentPosition(); } initGetLocation() { this.map.plugin('AMap.Geolocation', () => { this.geolocation = new AMap.Geolocation({ enableHighAccuracy: false, timeout: 1000, maximumAge: 0, convert: true, showButton: false, showMarker: true, showCircle: true, panToLocation: true, zoomToAccuracy: false, }); this.map.addControl(this.geolocation); this.getLocation(); AMap.event.addListener(this.geolocation, 'complete', onComplete => { console.log(onComplete, '定位成功'); this.currentLocation = onComplete; });
AMap.event.addListener(geolocation, 'error', onError => { console.log(onError, '定位失败'); }); }); } initMapPlugin(l) { l.forEach(v => { const f = v.split('.')[1]; this.map.addControl(new AMap[f]()); }); } pathLine(pointList, pathOption) {
const that = this; let pathLineStyle = { strokeStyle: 'red', lineWidth: 6, dirArrowStyle: true, }; Object.assign(pathLineStyle, pathOption); AMapUI.load(['ui/misc/PathSimplifier'], function(PathSimplifier) { var pathSimplifierIns = new PathSimplifier({ zIndex: 100, map: that.map, getPath: function(pathData, pathIndex) { return pathData.path; }, renderOptions: { pathLineStyle, }, }); pathSimplifierIns.setData(pointList); }); } drawMarker(marker) { this.map.add(marker); } getMarker(position, opt, extData, fn) {
let option = { position, offset: new AMap.Pixel(-17, -42), clickable: true, extData: extData, }; if (opt) { option = { ...option, ...opt }; } const marker = new AMap.Marker(option); marker.on('click', e => { this.map.setZoomAndCenter(10, e.target.getPosition()); if (this.lastSelectedMarker) { const { image } = this.lastSelectedMarker.w.icon.Ce; const icon = this.getMarkerIcon({ image }); this.lastSelectedMarker.setIcon(icon); } const { image } = e.target.w.icon.Ce; const newIcon = this.getMarkerIcon({ image, setOffset: new AMap.Pixel(-18, -50), size: new AMap.Size(52, 52), imageSize: new AMap.Size(48, 48), });
e.target.setIcon(newIcon); fn(extData); this.lastSelectedMarker = e.target; }); return marker; } getMarkerIcon(opt) { let option = { size: new AMap.Size(40, 40), imageSize: new AMap.Size(36, 36), }; return new AMap.Icon({ ...option, ...opt }); } initPositionPicker([fnSuc, fnFail], position) {
AMapUI.loadUI(['misc/PositionPicker'], PositionPicker => { this.positionPicker = new PositionPicker({ mode: 'dragMap', map: this.map, }); this.positionPicker.on('success', positionResult => { console.log(positionResult, '拖拽 success'); }); this.positionPicker.on('fail', positionResult => { console.log(positionResult, '拖拽err'); }); this.positionPicker.start( position ? position : this.currentLocation.position ); }); }
initAutoComplete() { AMap.plugin('AMap.Autocomplete', ()=> { const {citycode} = this.currentLocation.addressComponent this.autoComplete = new AMap.Autocomplete({ city:citycode?citycode:defaultCityCode }); }); }
|