Created
June 9, 2017 05:43
-
-
Save ki11ua/74d6e7af468bda44f28485f59d2130f5 to your computer and use it in GitHub Desktop.
multiple markers map async and only when #map exists
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
jQuery(window).on('load', function () { | |
if(jQuery("#map").length){ | |
loadMap(); | |
} | |
}); | |
function loadMap() { | |
var script = document.createElement('script'); | |
script.defer = true; | |
script.async = true; | |
script.src = 'https://maps.googleapis.com/maps/api/js?key=' + '' + '&callback=initMap'; | |
document.body.appendChild(script); | |
} | |
function initMap() { | |
var markers = [ | |
{ | |
"title": 'Laganas Rank', | |
"lat": '37.731103', | |
"lng": '20.863153', | |
"description": 'Laganas Rank' | |
}, | |
{ | |
"title": 'Alykes Rank', | |
"lat": '37.841162', | |
"lng": '20.768494', | |
"description": 'Alykes Rank' | |
}, | |
{ | |
"title": 'Vassilikos Rank', | |
"lat": '37.715704', | |
"lng": '20.980716', | |
"description": 'Vassilikos Rank' | |
}, | |
{ | |
"title": 'Agios Nikolaos Port Rank', | |
"lat": '37.905918', | |
"lng": '20.706018', | |
"description": 'Agios Nikolaos Port Rank' | |
}, | |
{ | |
"title": 'Tsilivi Rank', | |
"lat": '37.816781', | |
"lng": '20.864023', | |
"description": 'Tsilivi' | |
}, | |
{ | |
"title": 'Solomos Square Rank', | |
"lat": '37.786384', | |
"lng": '20.899399', | |
"description": 'Solomos Square Rank' | |
}, | |
{ | |
"title": 'Zakynthos International Airport "Dionysios Solomos"', | |
"lat": '37.754452', | |
"lng": '20.887103', | |
"description": 'Zakynthos International Airport "Dionysios Solomos"' | |
}, | |
{ | |
"title": 'Zakynthos Main Port', | |
"lat": '37.779211', | |
"lng": '20.901796', | |
"description": 'Zakynthos Main Port' | |
}]; | |
var image ="images/map-marker.png"; | |
var mapOptions = { | |
zoom: 9, | |
minZoom: 10, | |
maxZoom: 19, | |
mapTypeId: google.maps.MapTypeId.MAP, | |
mapTypeControl: true, | |
mapTypeControlOptions: { | |
style: google.maps.MapTypeControlStyle.DROPDOWN_MENU | |
}, | |
zoomControl: true, | |
zoomControlOptions: { | |
style: google.maps.ZoomControlStyle.LARGE, | |
position: google.maps.ControlPosition.LEFT_CENTER | |
}, | |
scrollwheel: false, | |
scaleControl: true, | |
panControl: true, | |
streetViewControl: false, | |
'disableDefaultUI': false, | |
'styles': [{ | |
stylers: [{ | |
lightness: 5 | |
}, { | |
saturation: -30 | |
}] | |
}] | |
}; | |
var map = new google.maps.Map(document.getElementById('map'), mapOptions); | |
var bounds = new google.maps.LatLngBounds(); | |
var infoWindow = new google.maps.InfoWindow(); | |
for (var i = 0; i < markers.length; i++) { | |
var data = markers[i]; | |
var myLatlng = new google.maps.LatLng(data.lat, data.lng); | |
var marker = new google.maps.Marker({ | |
position: myLatlng, | |
map: map, | |
icon: image, | |
title: data.title | |
}); | |
bounds.extend(marker.position); | |
(function (marker, data) { | |
google.maps.event.addListener(marker, "click", function (e) { | |
infoWindow.setContent("<div class='map-text'>" + data.description + "</div>"); | |
infoWindow.open(map, marker); | |
}); | |
})(marker, data); | |
} | |
map.fitBounds(bounds); | |
google.maps.visualRefresh = true; | |
google.maps.event.addDomListener(window, "resize", function() { | |
var center = map.getCenter(); | |
google.maps.event.trigger(map, "resize"); | |
map.setCenter(center); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment