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
| import { Loader } from '@googlemaps/js-api-loader'; import { getDistanceByAddr } from '@/api/mapaddrpositions';
interface MapOptions { center: { lat: number; lng: number }; zoom: number; }
interface Location { lat: number; lng: number; }
const loader = new Loader({ apiKey: import.meta.env.VITE_APP_MAP_KEY, version: 'weekly', libraries: ['places', 'geometry'], language: 'zh-TW', });
export function useGoogleMap() { let _google: any = null; const map = ref<google.maps.Map | null>(null); const autocompeleteService = ref<google.maps.places.AutocompleteService | null>(null); const geocoder = ref<google.maps.Geocoder | null>(null); const polyLine = ref<google.maps.Polyline | null>(null); const path = ref<string>('null'); let center = { lat: 0, lng: 0 };
const markers: google.maps.Marker[] = []; let markerStart: google.maps.Marker | null = null; let markerEnd: google.maps.Marker | null = null;
const addressFrom = ref<Location>({ lat: 25.0374865, lng: 121.5647688, }); const addressTo = ref<Location>({ lat: 25.0374865, lng: 121.5647688, });
async function initMap(mapOptions: MapOptions) { await loader .load() .then((google) => { nextTick(async () => { _google = google; map.value = new google.maps.Map(document.getElementById('map') as HTMLElement, mapOptions); geocoder.value = new google.maps.Geocoder(); }); }) .catch((e) => { console.log(e); }); }
async function setPolyLine(startPoint: string, endPoint: string) { const { result } = (await getDistanceByAddr({ FromAddr: startPoint, ToAddr: endPoint })) as any; path.value = result.polyLine; center = { lat: (result.fromLat + result.toLat) / 2, lng: (result.fromLng + result.toLng) / 2 }; drawPolyLine(); }
async function drawPolyLine() { if (polyLine.value) { toRaw(polyLine.value).setMap(null); } polyLine.value = new _google.maps.Polyline({ path: _google.maps.geometry.encoding.decodePath(path.value), strokeColor: '#FF0000', strokeOpacity: 0.8, strokeWeight: 5, map: map.value, }); map.value!.setCenter({ lat: center.lat, lng: center.lng }); map.value!.setZoom(11); }
async function initAutoComplete() { await loader .load() .then((google) => { nextTick(() => { autocompeleteService.value = new window.google.maps.places.AutocompleteService(); }); }) .catch((e) => { console.log(e); }); }
function setMarker(lat: number, lng: number, type: string) { const marker = new _google.maps.Marker({ map: map.value, position: { lat, lng }, animation: _google.maps.Animation.DROP, }); if (type === 'from') { if (markerStart) { markerStart.setMap(null); } markerStart = marker; } else if ('to') { if (markerEnd) { markerEnd.setMap(null); } markerEnd = marker; } else { markers.push(marker); } }
function toTargetPlace(address: string, type: string) { if (!geocoder.value) return; geocoder.value.geocode({ address }, (results, status) => { if (status === 'OK') { if (type === 'from') { addressFrom.value = { lat: results![0].geometry.location.lat(), lng: results![0].geometry.location.lng(), }; } else { addressTo.value = { lat: results![0].geometry.location.lat(), lng: results![0].geometry.location.lng(), }; } setMarker(results![0].geometry.location.lat(), results![0].geometry.location.lng(), type); if (!map.value) return;
map.value.setCenter({ lat: results![0].geometry.location.lat(), lng: results![0].geometry.location.lng(), }); map.value.setZoom(16); } else { throw new Error(`Geocode was not successful for the following reason: ${status}`); } }); }
return { map, geocoder, autocompeleteService, initMap, initAutoComplete, setMarker, toTargetPlace, setPolyLine }; }
|