Looking for some help with this issue. I've loaded the Google map API on the created hook of the parent component, but I keep running into the following error message: Error in the created hook:
ReferenceError: google is not defined
Here is the code snippet where the Google map script is being called:
mounted() {
let mapScript = document.createElement('script');
mapScript.setAttribute('src', 'https://maps.googleapis.com/maps/api/js?key=TheKey');
document.head.appendChild(mapScript);
}
Below is the child component where I am trying to display the Google map:
<template>
<div>
<div ref="mapID" :id="mapID"></div>
</div>
</template>
<script>
export default {
props: [ 'place', 'coordinates' ],
data() {
return {
mapID: 'mapID',
markerCord: '',
markers: []
}
},
methods: {
initialize() {
console.log('Hello World');
let locations = [
['Bondi Beach', -33.890542, 151.274856, 4],
['Coogee Beach', -33.923036, 151.259052, 5],
['Cronulla Beach', -34.028249, 151.157507, 3],
['Manly Beach', -33.80010128657071, 151.28747820854187, 2],
['Maroubra Beach', -33.950198, 151.259302, 1]
];
let map = new
google.maps.Map(document.getElementById(this.mapID), {
zoom: 10,
center: new google.maps.LatLng(-33.92, 151.25),
mapTypeId: google.maps.MapTypeId.ROADMAP
});
let infowindow = new google.maps.InfoWindow();
let marker, i;
for (i = 0; i < locations.length; i++) {
marker = new google.maps.Marker({
position: new google.maps.LatLng(locations[i][1],
locations[i][2]),
map: map
});
google.maps.event.addListener(marker, 'click',
(function(marker, i) {
return function() {
infowindow.setContent(locations[i][0]);
infowindow.open(map, marker);
}
})(marker, i));
}
}
},
mounted() {
this.initialize()
}
}
</script>