Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Marker content (infoWindow) Google Maps

I'm trying to add infoWindow's to multiple markers on a Google Map. The closest I have come is to get an infoWindow to display the last address you can see in the array, on all markers. The bit of code I have pasted below does not work, I get an "Uncaught TypeError: Cannot read property '4' of undefined". I'm sure this is a scope issue, but I'm going round in circles here and could do with some help:

var hotels = [             ['ibis Birmingham Airport', 52.452656, -1.730548, 4, 'Ambassador Road<br />Bickenhill<br />Solihull<br />Birmingham<br />B26 3AW','(+44)1217805800','(+44)1217805810','[email protected]','http://www.booknowaddress.com'],             ['ETAP Birmingham Airport', 52.452527, -1.731644, 3, 'Ambassador Road<br />Bickenhill<br />Solihull<br />Birmingham<br />B26 3QL','(+44)1217805858','(+44)1217805860','[email protected]','http://www.booknowaddress.com'],             ['ibis Birmingham City Centre', 52.475162, -1.897208, 2, 'Ladywell Walk<br />Birmingham<br />B5 4ST','(+44)1216226010','(+44)1216226020','[email protected]','http://www.booknowaddress.com']         ];          for (var i = 0; i < hotels.length; i++) {             var marker = new google.maps.Marker({                 position: new google.maps.LatLng(hotels[i][1], hotels[i][2]),                 map: map,                 icon: image,                 title: hotels[i][0],                 zIndex: hotels[i][2]             });              var infoWindow = new google.maps.InfoWindow();              google.maps.event.addListener(marker, 'click', function () {                 var markerContent = hotels[i][4];                 infoWindow.setContent(markerContent);                 infoWindow.open(map, this);             });         } 

Thanks in anticipation.

like image 765
DarrylGodden Avatar asked May 03 '11 11:05

DarrylGodden


People also ask

How do I put InfoWindow on marker?

Call setPosition() on the info window, or. Attach the info window to a new marker using the InfoWindow. open() method. Note: If you call open() without passing a marker, the InfoWindow will use the position specified upon construction through the InfoWindowOptions object literal.

How do I get rid of InfoWindow in Google Maps?

After constructing an InfoWindow, you must call open to display it on the map. The user can click the close button on the InfoWindow to remove it from the map, or the developer can call close() for the same effect.


1 Answers

We've solved this, although we didn't think having the addListener outside of the for would make any difference, it seems to. Here's the answer:

Create a new function with your information for the infoWindow in it:

function addInfoWindow(marker, message) {              var infoWindow = new google.maps.InfoWindow({                 content: message             });              google.maps.event.addListener(marker, 'click', function () {                 infoWindow.open(map, marker);             });         } 

Then call the function with the array ID and the marker you want to create:

addInfoWindow(marker, hotels[i][3]); 
like image 108
DarrylGodden Avatar answered Sep 27 '22 21:09

DarrylGodden