i am having issues adding infoWindow to an existing gmaps markerclusterer project. I am aware there are similar questions but the answers i've seen haven't worked. It may be because i am fairly new to this and missing something.
Below is my code without the infoWindow. Each marker is generated from lat/long in the .jason data file. The 'pre-attempt' code:
<script src="https://maps.googleapis.com/maps/api/js"></script>
<script src="data.json"></script>
<script type="text/javascript" src="markerclusterer.js"></script>
<script>
function initialize() {
var center = new google.maps.LatLng(34.777491, 64.5852620);
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 3,
center: center,
mapTypeId: google.maps.MapTypeId.ROADMAP
});
var markers = [];
for (var i = 0; i < 100; i++) {
var dataPhoto = data.photos[i];
var latLng = new google.maps.LatLng(dataPhoto.latitude,
dataPhoto.longitude);
var marker = new google.maps.Marker({
position: latLng,
icon: "images/gps.svg"
});
markers.push(marker);
}
var markerCluster = new MarkerClusterer(map, markers,
{imagePath:'images/m'});
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
<script>
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-12846745-20']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type =
'text/javascript'; ga.async = true;
ga.src = ('https:' === document.location.protocol ? 'https://ssl' :
'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(ga, s);
})();
</script>
<body>
<div id="map-container"><div id="map"></div></div>
</body>
The attempted but non working code:
<script src="https://maps.googleapis.com/maps/api/js"></script>
<script src="data.json"></script>
<script type="text/javascript" src="markerclusterer.js"></script>
<script>
function initialize() {
var center = new google.maps.LatLng(34.777491, 64.5852620);
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 3,
center: center,
mapTypeId: google.maps.MapTypeId.ROADMAP
});
var markers = [];
for (var i = 0; i < 100; i++) {
var dataPhoto = data.photos[i];
var infoWin = new google.maps.InfoWindow();
var latLng = new google.maps.LatLng(dataPhoto.latitude,
dataPhoto.longitude);
var marker = new google.maps.Marker({
position: latLng,
icon: "images/gps.svg"
});
google.maps.event.addListener(marker, 'click', function(evt) {
infoWin.setContent('content here');
infoWin.open(map, marker);
})
markers.push(marker);
}
var markerCluster = new MarkerClusterer(map, markers, {imagePath:'images/m'});
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
<script>
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-12846745-20']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' === document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
<body>
<div id="map-container"><div id="map"></div></div>
</body>
I don't know if i am missing something obvious or if there is a conflict with the click to zoom function. To be clear i am looking to implement the infoWindow when only one device is clickable, not a cluster. Thanks for any help, i really appreciate it.
Update:
var data = { "count": 10785236,
"photos": [{"photo_id": 27932, "photo_title": "Atardecer en Embalse", "photo_url": "http://www.panoramio.com/photo/27932", "photo_file_url": "http://mw2.google.com/mw-panoramio/photos/medium/27932.jpg", "longitude": -64.404945, "latitude": -32.202924, "width": 500, "height": 375, "upload_date": "25 June 2006", "owner_id": 4483, "owner_name": "Miguel Coranti", "owner_url": "http://www.panoramio.com/user/4483"}
The var data shown above is an example from the .json file. This is one of many entries.
Inside your
for loop
a function closure
is missing so you cannot capture infowindow objects and assign them to their relative markers, letting alone displaying their contents.
Change this:
google.maps.event.addListener(marker, 'click', function(evt) {
infoWin.setContent('content here');
infoWin.open(map, marker);
})
For this:
(function(m, infoWindow, idx){
google.maps.event.addListener(m, 'click', function(evt) {
infoWindow.setContent('marker ' + idx);
infoWindow.open(map, m);
})
})(marker, infoWin, i);
To reproduce the issue, I have used 100 different lat/lon in a JSON object, as for the number of iterations in your for loop.