Спасибо за ваш ответ, но я получаю это предупреждение:
Vue warn: Error in mounted hook: "TypeError: Cannot read property 'latitude' of undefined"
Здесь мой код:
<script>
import CarMap from "./CarMap";
import axios from 'axios';
export default {
components: {CarMap},
name: 'CarMap',
props: ['CarMap'],
data: function () {
return {
mapName: this.name + "-map",
markerCoordinates: [],
map: null,
bounds: null,
markers: [],
locations: null,
}
},
created() {
this.fetchData();
},
methods: {
fetchData() {
this.error = this.locations = null;
this.loading = true;
axios
.get('/api/locations')
.then(response => (this.locations = response))
}
},
mounted: function () {
this.fetchData();
this.bounds = new google.maps.LatLngBounds();
const element = document.getElementById(this.mapName)
const mapCentre = this.markerCoordinates[0]
const options = {
center: new google.maps.LatLng(mapCentre.latitude, mapCentre.longitude)
}
this.map = new google.maps.Map(element, options, document.getElementById('mapName'));
this.markerCoordinates.push({
latitude: 'locations.latitude',
longitude: 'location.longitude'
});
this.markerCoordinates.forEach((coord) => {
const position = new google.maps.LatLng(coord.latitude, coord.longitude);
const marker = new google.maps.Marker({
position,
map: this.map
});
this.markers.push(marker)
this.map.fitBounds(this.bounds.extend(position))
});
}
};
</script>
Я хочу установить маркеры на карте через Координаты в моей таблице местоположений / api.