| | |
| | | amapApi = await loadAMapApi({ |
| | | key, |
| | | version: '2.0', |
| | | plugins: ['AMap.PlaceSearch', 'AMap.Scale', 'AMap.ToolBar'], |
| | | plugins: ['AMap.PlaceSearch', 'AMap.Geocoder', 'AMap.Scale', 'AMap.ToolBar'], |
| | | }) |
| | | return amapApi |
| | | } |
| | |
| | | }) |
| | | locationMap.addControl(new AMap.Scale()) |
| | | locationMap.addControl(new AMap.ToolBar({ position: { right: '10px', top: '10px' } })) |
| | | locationMap.on('click', (event: any) => setMapMarker(event.lnglat.getLat(), event.lnglat.getLng(), false)) |
| | | locationMap.on('click', (event: any) => { |
| | | const latitude = event.lnglat.getLat() |
| | | const longitude = event.lnglat.getLng() |
| | | setMapMarker(latitude, longitude, false) |
| | | reverseGeocodeLocation(latitude, longitude) |
| | | }) |
| | | if (selected) setMapMarker(initial.latitude, initial.longitude, false) |
| | | window.setTimeout(() => locationMap?.resize(), 120) |
| | | } catch (error) { |
| | |
| | | |
| | | function selectSearchLocation(item: { name: string; longitude: number; latitude: number }) { |
| | | setMapMarker(item.latitude, item.longitude) |
| | | if (!form.address?.trim()) form.address = item.name |
| | | setLocationAddress(item.name) |
| | | locationSearchResults.value = [] |
| | | } |
| | | |
| | | function setLocationAddress(address: string) { |
| | | const normalizedAddress = address.trim() |
| | | if (!normalizedAddress) return |
| | | if (activeParkingAddress.value) { |
| | | activeParkingAddress.value.address = normalizedAddress |
| | | } else if (activeSchoolAddress.value) { |
| | | activeSchoolAddress.value.address = normalizedAddress |
| | | } else if (activeFuelStore.value) { |
| | | activeFuelStore.value.address = normalizedAddress |
| | | } else { |
| | | form.address = normalizedAddress |
| | | } |
| | | locationSearch.value = normalizedAddress |
| | | } |
| | | |
| | | async function reverseGeocodeLocation(latitude: number, longitude: number) { |
| | | try { |
| | | const AMap = await loadAMap() |
| | | const geocoder = new AMap.Geocoder({ radius: 1000, extensions: 'all' }) |
| | | geocoder.getAddress([longitude, latitude], (status: string, result: any) => { |
| | | if (status === 'complete' && result?.regeocode?.formattedAddress) { |
| | | setLocationAddress(result.regeocode.formattedAddress) |
| | | } |
| | | }) |
| | | } catch (error) { |
| | | console.warn('Failed to reverse geocode AMap location', error) |
| | | } |
| | | } |
| | | |
| | | function closeLocationMap() { |
| | | locationMap?.destroy() |
| | | locationMap = undefined |