-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
100 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import type { Ref } from 'vue'; | ||
import { markRaw, onMounted, reactive, ref, toValue } from 'vue'; | ||
import { useMap } from './useMap'; | ||
|
||
export function useSearch(apiKey: string) { | ||
const isLoading = ref(false); | ||
const cGoogle = ref<typeof google>(); | ||
const cApi = ref<typeof google.maps>(); | ||
const cGeocoder = ref<google.maps.Geocoder>(); | ||
const coordinates = reactive<google.maps.LatLngLiteral>({ | ||
lat: 0, | ||
lng: 0, | ||
}); | ||
|
||
function resetCoordinates() { | ||
Object.assign(coordinates, { | ||
lat: 0, | ||
lng: 0, | ||
}); | ||
} | ||
|
||
const getCoordinates = async (address: string | Ref<string>) => { | ||
isLoading.value = true; | ||
|
||
if (!cGeocoder.value) | ||
return; | ||
|
||
if (!address && !toValue(address)) | ||
return; | ||
|
||
const request: google.maps.GeocoderRequest = { | ||
address: toValue(address), | ||
}; | ||
|
||
// NOTE: 使用地理編碼服務將地址轉換為地理座標 | ||
await cGeocoder.value.geocode(request, (results, status) => { | ||
if (status === 'OK' && results && results.length > 0) { | ||
const { location } = results[0].geometry; | ||
|
||
if (!location) | ||
return; | ||
|
||
const { lat, lng } = location; | ||
|
||
if (!lat || !lng) | ||
return; | ||
|
||
// 設置座標 | ||
Object.assign(coordinates, { lat: lat(), lng: lng() }); | ||
|
||
isLoading.value = false; | ||
} | ||
else { | ||
resetCoordinates(); | ||
|
||
isLoading.value = false; | ||
} | ||
}); | ||
}; | ||
|
||
onMounted(async () => { | ||
const { loader } = useMap(apiKey); | ||
cGoogle.value = markRaw(await loader.load()); | ||
cApi.value = markRaw(cGoogle.value.maps); | ||
cGeocoder.value = markRaw(new cApi.value.Geocoder()); | ||
}); | ||
|
||
return { | ||
isLoading, | ||
coordinates, | ||
getCoordinates, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from './composables/useMap'; | ||
export * from './composables/useSearch'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.