-
Notifications
You must be signed in to change notification settings - Fork 92
fix(google-maps): overhaul <ScriptGoogleMaps> and <ScriptGoogleMapsOverlayView>
#689
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
48892da
0ea65d8
6fcebc9
48420b7
fdb78b1
b2af26f
9efe02c
b6d989d
3e42e03
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,26 +1,42 @@ | ||
| <script setup lang="ts"> | ||
| import type { ScriptGoogleMapsProps } from '#nuxt-scripts/components/GoogleMaps/ScriptGoogleMaps.vue' | ||
| import { ref } from 'vue' | ||
|
|
||
| const center = ref<string>('Brooklyn+Bride,New+York+NY') | ||
| const googleMapsRef = useTemplateRef('googleMapsRef') | ||
|
|
||
| function changeQuery() { | ||
| if (center.value.startsWith('Statue')) { | ||
| center.value = 'Brooklyn+Bride,New+York+NY' | ||
| } | ||
| else { | ||
| center.value = 'Statue+of+Liberty+National+Monument+New+York+NY' | ||
| } | ||
| const query = ref('Brooklyn+Bridge,New+York+NY') | ||
|
|
||
| const center = ref<google.maps.LatLng | google.maps.LatLngLiteral>({ lat: -34.397, lng: 150.644 }) | ||
|
|
||
| async function changeQuery() { | ||
| if (!googleMapsRef.value) { | ||
| return | ||
| } | ||
|
|
||
| query.value = query.value.startsWith('Statue') | ||
| ? 'Brooklyn+Bridge,New+York+NY' | ||
| : 'Statue+of+Liberty+National+Monument+New+York+NY' | ||
|
|
||
| const queryLatLng = await googleMapsRef.value.resolveQueryToLatLng( | ||
| query.value | ||
| ) | ||
|
Comment on lines
+20
to
+22
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Handle geocode failures to avoid unhandled rejections.
Proposed fix async function changeQuery() {
if (!googleMapsRef.value) {
return
}
query.value = query.value.startsWith('Statue')
? 'Brooklyn+Bridge,New+York+NY'
: 'Statue+of+Liberty+National+Monument+New+York+NY'
- const queryLatLng = await googleMapsRef.value.resolveQueryToLatLng(
- query.value
- )
-
- if (queryLatLng) {
- center.value = queryLatLng
+ try {
+ const queryLatLng = await googleMapsRef.value.resolveQueryToLatLng(query.value)
+ if (queryLatLng) {
+ center.value = queryLatLng
+ }
+ }
+ catch (error) {
+ console.error('Failed to resolve map query:', error)
}
}🤖 Prompt for AI Agents |
||
|
|
||
| if (queryLatLng) { | ||
| center.value = queryLatLng | ||
| } | ||
| } | ||
| </script> | ||
|
|
||
| <template> | ||
| <div> | ||
| <div> | ||
| <ScriptGoogleMaps | ||
| api-key="AIzaSyAOEIQ_xOdLx2dNwnFMzyJoswwvPCTcGzU" | ||
| ref="googleMapsRef" | ||
| :width="1200" | ||
| :height="600" | ||
| :center="center" | ||
| :map-options="{ | ||
| center | ||
| }" | ||
| /> | ||
| </div> | ||
| <div class="button-container"> | ||
|
|
||
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add null check before accessing
googleMapsRef.value.If
changeQueryis invoked before the component mounts,googleMapsRef.valuewill benull, causing a runtime error when callingresolveQueryToLatLng.Also note the query string contains "Bride" (should likely be "Bridge").
Proposed fix
async function changeQuery() { + if (!googleMapsRef.value) { + return + } - const res = await googleMapsRef.value.resolveQueryToLatLng('Brooklyn+Bride,New+York+NY') + const res = await googleMapsRef.value.resolveQueryToLatLng('Brooklyn+Bridge,New+York+NY') - mapOptions.value.center = res + if (res) { + mapOptions.value.center = res + } }🤖 Prompt for AI Agents