While I was developing my
gallery I had few problems which I want here to describe.
My solution for errors "initMap is not a function" and "google is not defined":
(function (ns) {
/*globals google, map*/
"use strict";
var map;
function initMap() {
try {
map = new google.maps.Map(document.getElementById('map-canvas'), {
center: { lat: 34.397, lng: 150.644 },
scrollwheel: true,
zoom: 2
});
}
catch (e) {
console.log(e);
setTimeout(function () {
if (typeof google !== 'object') {
location.reload();
}
}, 1000);
}
ns.map = map;
}
ns.initMap = initMap;
})(window.milosev);
POI fix for "google is not defined" error:
setTimeout(function () {
if (typeof google !== 'object') {
location.reload();
}
}, 1000);
POI fix for "initMap is not a function" error:
function initMap() {
map = new google.maps.Map(document.getElementById('map-canvas'), {
center: { lat: 34.397, lng: 150.644 },
scrollwheel: false,
zoom: 2
});
ns.map = map;
}
Where HTML part should look like:
<script defer type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=myKey;callback=window.milosev.initMap"></script>
Notice: window.milosev.initMap
My solution for zoom after fitBounds:
(function (ns) {
/*globals google, $*/
"use strict";
$.getJSON("djala.json", function (data) {
data.forEach(function (file) {
var picsLatLng = new google.maps.LatLng(file.Latitude, file.Longitude);
var bounds = new google.maps.LatLngBounds();
var marker = new google.maps.Marker({
position: picsLatLng,
map: ns.map,
title: file.FileName,
url: file.FileName
});
marker.addListener('click', function () {
window.open(marker.url, "_target");
});
bounds.extend(picsLatLng);
ns.map.fitBounds(bounds);
var zoomChangeBoundsListener =
google.maps.event.addListenerOnce(ns.map, 'bounds_changed', function(event) {
if ( ns.map.getZoom() ){
ns.map.setZoom(14); // set zoom here
}
});
setTimeout(function(){
google.maps.event.removeListener(zoomChangeBoundsListener);
}, 2000);
});
});
})(window.milosev);
POI:
var zoomChangeBoundsListener =
google.maps.event.addListenerOnce(ns.map, 'bounds_changed', function(event) {
if ( ns.map.getZoom() ){
ns.map.setZoom(14); // set zoom here
}
});
setTimeout(function(){
google.maps.event.removeListener(zoomChangeBoundsListener);
}, 2000);