I have a backend API using a MySQL database, I am trying to retrieve data from my database and use the data to plot the latitude and longitude on a map using the google maps API. I am using the Gmaps API with Nuxt JS.
I have tried to do this with the code below but I have encountered an error which says
"Cannot read properties of undefined (reading 'coordinates')"
I am trying to retrieve my data with the below code
async function getData(){
const {data} = await this.$axios.$get('http://localhost:8000/api/parkingPlace');
console.log(data)
return {data}
}
and then pass this data into my Gmaps.
export default {
data() {
return {
currentLocation: {},
circleOptions: {},
parkingPlaces: getData(),
Which I can then use to retrieve my values of latitude and longitude and plot these as pins onto my map.
<template>
<div class="min-h-screen relative max-6/6" >
<GMap class="absolute inset-0 h-100% bg-blue-400"
ref="gMap"
language="en"
:cluster="{options: {styles: clusterStyle}}"
:center="{lat:parkingPlaces[0].coordinates.lat, lng: parkingPlaces[0].coordinates.lng}"
:options="{fullscreenControl: false, styles: mapStyle}"
:zoom="5">
<GMapMarker
v-for="location in parkingPlaces"
:key="location.id"
:position="{lat: location.coordinates.lat, lng: location.coordinates.lng}"
:options="{icon: location.free_spots > 0 ? pins.spacefree : pins.spacenotfree}"
@click="currentLocation = location"
>
<GMapInfoWindow :options="{maxWidth: 200}">
<code>
lat: {{ location.coordinates.lat }},
lng: {{ location.coordinates.lng }}
</code>
</GMapInfoWindow>
</GMapMarker>
<GMapCircle :options="circleOptions"/>
</GMap>
</div>
</template>
My whole index.vue
class is below where I am trying to do this.
<template>
<div class="min-h-screen relative max-6/6" >
<GMap class="absolute inset-0 h-100% bg-blue-400"
ref="gMap"
language="en"
:cluster="{options: {styles: clusterStyle}}"
:center="{lat:parkingPlaces[0].coordinates.lat, lng: parkingPlaces[0].coordinates.lng}"
:options="{fullscreenControl: false, styles: mapStyle}"
:zoom="5">
<GMapMarker
v-for="location in parkingPlaces"
:key="location.id"
:position="{lat: location.coordinates.lat, lng: location.coordinates.lng}"
:options="{icon: location.free_spots > 0 ? pins.spacefree : pins.spacenotfree}"
@click="currentLocation = location"
>
<GMapInfoWindow :options="{maxWidth: 200}">
<code>
lat: {{ location.coordinates.lat }},
lng: {{ location.coordinates.lng }}
</code>
</GMapInfoWindow>
</GMapMarker>
<GMapCircle :options="circleOptions"/>
</GMap>
</div>
</template>
<script>
async function getData(){
const {data} = await this.$axios.$get('http://localhost:8000/api/parkingPlace');
console.log(data)
return {data}
export default {
data() {
return {
currentLocation: {},
circleOptions: {},
parkingPlaces: getData(),
// parkingPlaces: [
// {
// "id": 1,
// "name": "Chandler Larson",
// "post": "37757",
// "coordinates": {
// "lng": -51.84,
// "lat": -60.02
// },
// "total_spots": 0,
// "free_spots": 0
// }
// ],
pins: {
spacefree: "/parkingicongreen3.png",
spacenotfree: "/parkingiconred3.png",
},
mapStyle: [],
clusterStyle: [
{
url: "https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/m1.png",
width: 56,
height: 56,
textColor: "#fff"
}
]
}
}
}
</script>
I have tried to use Axios to retrieve data from my database and pass this into my google maps to display points on the map.
I am expecting points to appear on my map where data from my database gives the lat
and lng
.
EDIT 1:
I have now edited some of my code to reflect the changes as suggested by @kissu.
<template v-if="parkingPlaces.length > 0">
<div class="min-h-screen relative max-6/6" >
<GMap class="absolute inset-0 h-100% bg-blue-400"
ref="gMap"
language="en"
:cluster="{options: {styles: clusterStyle}}"
:center="{lat:this.parkingPlaces[0].coordinates.lat, lng: this.parkingPlaces[0].coordinates.lng}"
:options="{fullscreenControl: false, styles: mapStyle}"
:zoom="5">
<GMapMarker
v-for="location in parkingPlaces"
:key="location.id"
:position="{lat: location.coordinates.lat, lng: location.coordinates.lng}"
:options="{icon: location.free_spots > 0 ? pins.spacefree : pins.spacenotfree}"
@click="currentLocation = location"
>
<GMapInfoWindow :options="{maxWidth: 200}">
<code>
lat: {{ location.coordinates.lat }},
lng: {{ location.coordinates.lng }}
</code>
</GMapInfoWindow>
</GMapMarker>
<GMapCircle :options="circleOptions"/>
</GMap>
</div>
</template>
<script>
// async function getData(){
// const {data} = await this.$axios.$get('http://localhost:8000/api/parkingPlace');
// console.log(data)
// return {data}
// }
export default {
data() {
return {
currentLocation: {},
circleOptions: {},
parkingPlaces: [],
// parkingPlaces: [
// {
// "id": 1,
// "name": "Chandler Larson",
// "post": "37757",
// "coordinates": {
// "lng": -51.84,
// "lat": -60.02
// },
// "total_spots": 0,
// "free_spots": 0
// }
// ],
pins: {
spacefree: "/parkingicongreen3.png",
spacenotfree: "/parkingiconred3.png",
},
mapStyle: [],
clusterStyle: [
{
url: "https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/m1.png",
width: 56,
height: 56,
textColor: "#fff"
}
]
}
},
async fetch(){
this.parkingPlaces = await fetch('http://localhost:8000/api/parkingPlace').then(res => res.json())
console.log(this.parkingPlaces)
}
}
</script>
Result of my console.log(this.parkingPlaces)
EDIT 2