0

It's possible to call JavaScript from link in a infowindow? Know it's possible but in my case I can't figure how to declare i ?

i is a polygon number, and it mean to call function kmlShowPlacemark(i)

Here's my example:

function clickablePolygon(placemark, info, i) {
     var name = geoXmlDoc.placemarks[i].name;
     var numMkrs = geoXmlDoc.placemarks[i].numMarkers;
     google.maps.event.addListener(placemark.polygon, "click", function(e) {
        if (e && e.latLng) {
             infowindow.setPosition(e.latLng);
        } else {
             infowindow.setPosition(placemark.polygon.bounds.getCenter());
        }
    infowindow.setContent('<div class="geoxml3_infowindow"><h3>' + placemark.name +
           '</h3><div>' + info + '<br><a href="javascript:kmlShowPlacemark('+ i + ');">Show place</a></div>');
        infowindow.open(map); 
     });
}
scooterfreek
  • 53
  • 1
  • 10
  • It is possible. We need more context. – geocodezip Mar 20 '13 at 00:08
  • I looked at your [other question](http://stackoverflow.com/questions/15415357/marker-cluster-number-in-a-polygon-or-and-infowindow) where I wrote this code for you. How are you calling it? – geocodezip Mar 20 '13 at 05:35

2 Answers2

1

This works for me, looks like your HTML in the InfoWindow is invalid (that is the only real difference I see):

function clickablePolygon(placemark, info, i) {
  google.maps.event.addListener(placemark.polygon, "click", function(e) {
    if (e && e.latLng) {
      infowindow.setPosition(e.latLng);
    } else {
      infowindow.setPosition(placemark.polygon.bounds.getCenter());
    }
    infowindow.setContent('<div class="geoxml3_infowindow"><h3>' + placemark.name +
               '</h3><div>' + info + '</div>'+'<br><a href="javascript:kmlShowPlacemark('+ i + ');">Show place</a></div>');
    infowindow.open(map);
  });
}

modified proof of concept

geocodezip
  • 158,664
  • 13
  • 220
  • 245
  • It work good only think is that infowindow is not auto closed when javascript is called [example](http://mini.hr/stanonline/test2.php) – scooterfreek Mar 20 '13 at 09:38
  • So write code to close it. I would think infowindow.close() in kmlShowPlacemark should work. – geocodezip Mar 20 '13 at 11:29
0

Please try the following:

Replace:

<a href="javascript:kmlShowPlacemark('+ i + ');">Show place</a>

With

<a href="#" onclick="kmlShowPlacemark('+ i + '); return false;">Show place</a>
ron tornambe
  • 10,452
  • 7
  • 33
  • 60
  • still problem with opening infowindow, think is a problem in declaration of i with var name = geoXmlDoc.placemarks[i].name; ? – scooterfreek Mar 19 '13 at 22:50
  • I noticed you are missing a closing but am not sure that will solve the problem. Are you saying the infowindow does not open at all? – ron tornambe Mar 19 '13 at 22:50
  • Yes, it does not open at all, when comment is made for `//var name = geoXmlDoc.placemarks[i].name; //var numMkrs = geoXmlDoc.placemarks[i].numMarkers;` it open but without function for _Show place_ link – scooterfreek Mar 19 '13 at 22:56
  • Does the JavaScript console show any errors? Are you able to debug and step through the code.If so, what is the value of "i" and "name"? – ron tornambe Mar 19 '13 at 23:35
  • Error _Uncaught_ _TypeError:_ _Cannot_ _read property_ _'name'_ _of_ _undefined_ – scooterfreek Mar 20 '13 at 00:00
  • Where is geoXmlDoc defined? – ron tornambe Mar 20 '13 at 00:17