Created
August 21, 2017 18:54
-
-
Save fredyfx/9005d68d5e8d89455f751de1b6b05bcf to your computer and use it in GitHub Desktop.
This open a marker from an anchor which technically could be any kind of event listener.
This file contains hidden or 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
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script> | |
<script type="text/javascript"> | |
var markers = []; | |
function initialize() { | |
var mapOptions = { | |
zoom: 10, | |
center: new google.maps.LatLng(40.714364, -74.005972), | |
mapTypeId: google.maps.MapTypeId.ROADMAP | |
} | |
var map = new google.maps.Map(document.getElementById("googlemap"), mapOptions); | |
var locations = [ | |
['New York', 40.714364, -74.005972, 'http://www.google.com/intl/en_us/mapfiles/ms/micons/green-dot.png'] | |
]; | |
var marker, i; | |
var infowindow = new google.maps.InfoWindow(); | |
google.maps.event.addListener(map, 'click', function() { | |
infowindow.close(); | |
}); | |
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, | |
icon: locations[i][3] | |
}); | |
google.maps.event.addListener(marker, 'click', (function(marker, i) { | |
return function() { | |
infowindow.setContent(locations[i][0]); | |
infowindow.open(map, marker); | |
} | |
})(marker, i)); | |
markers.push(marker); | |
} | |
} | |
google.maps.event.addDomListener(window, 'load', initialize); | |
function myClick(id){ | |
google.maps.event.trigger(markers[id], 'click'); | |
} | |
</script> | |
<div id="googlemap" style="width: 100%; height: 500px;"></div> | |
<a href="#" onclick="myClick(0);">Open Info Window</a> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
By the way, the original source is located at: http://jsfiddle.net/fJ4jG/9/