I have searched all over google for a solution however this seems a new one? I am trying to implement google maps API on a site however I keep getting the following error:
Uncaught TypeError: Cannot call method 'apply' of undefined
My JS is as follows:
var map;
function initialize(location) {
var mapDiv = document.getElementById('map-canvas');
var latLng;
if (location == 0) {
latLng = new google.maps.LatLng(52.066356, 1.102388);
}
else if (location == 1) {
latLng = new google.maps.LatLng(52.672492, 1.232196);
}
else if (location == 2) {
latLng = new google.maps.LatLng(52.207607, 0.123017);
}
map = new google.maps.Map(mapDiv, {
center: latLng,
zoom: 14,
mapTypeId: google.maps.MapTypeId.ROADMAP
});
google.maps.event.addDomListener(map, 'tilesloaded', addMarker(location));
}
function addMarker(location) {
var latLng;
if (location == 0) {
latLng = new google.maps.LatLng(52.066703, 1.113573);
}
else if (location == 1) {
latLng = new google.maps.LatLng(52.672492, 1.232196);
}
else if (location == 2) {
latLng = new google.maps.LatLng(52.207607, 0.123017);
}
var marker = new google.maps.Marker({
position: latLng,
map: map
});
}
$(document).ready(function () {
initialize(0);
});
There are a several reasons why your google maps may not be working, the most common issue being no Google Map API key set or set incorrectly. To use the Google Maps JavaScript API, you must register your app project on the Google Cloud Platform Console and get a Google API key which you can add to your app.
Steps: Go to https://google-developers.appspot.com/maps/documentation/utils/geocoder/ and search for “Tower at Cityplace (N Haskell Ave STE 250, Dallas, TX 75204)”. Click the Geocode button, and see the visual representation of the Google Geocoding API response.
In this line
google.maps.event.addDomListener(map, 'tilesloaded', addMarker(location));
You're calling the function addMarker instead of passing it as a function reference. Therefore, addMarker is getting executed before map is defined. Try changing that line to:
google.maps.event.addDomListener(map, 'tilesloaded', function() {addMarker(location)});
The problem is you're invoking the function addMarker
which you should be supplying a callback. Change your call to the following
google.maps.event.addDomListener(map, 'tilesloaded', function() { addMarker(location) });
Right now you're instead directly invoking addMarker
which returns no value. This means you're effectively passing undefined
to the API and the google library is hitting the error attempting to invoke your callback
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With