javascript - Google Maps API JS All Points open same infoWindow on click -
i've been tinkering around day , can't seem figure out. when render map, everytime click on point, opens windowinfo of last location added. i've verified each xml element returning it's proper name,location,lat/lng etc. appreciated.
<code> <script type="text/javascript" src="//maps.googleapis.com/maps/api/js"></script> <script> $(document).ready(function () { $("#map").css({ height: 300, width: 400 }); var mylatlng = new google.maps.latlng(33.985236, -117.715781); mymap.init('#map', mylatlng, 8); //alert("0: "+mymap.map.getzoom()); mymap.placemarkers('/admin/company/project/googlexml'); //alert("2: "+mymap.map.getzoom()) }); var wh = $(document).height(); //$('#frame').height(wh - 125); google.maps.visualrefresh = true; var locations = 0; var mymap = { map: null, bounds: null } mymap.init = function (selector, latlng, zoom) { var myoptions = { zoom: zoom, center: latlng, maptypeid: google.maps.maptypeid.roadmap } this.map = new google.maps.map($(selector)[0], myoptions); this.bounds = new google.maps.latlngbounds(); //alert("0: "+mymap.map.getzoom()); window.settimeout(function () { //alert("10: "+mymap.map.getzoom()); if (mymap.map.getzoom() > 13) { mymap.map.setzoom(13) } }, 500); } mymap.placemarkers = function (filename) { $.post(filename, function (xml) { var markers = xml.documentelement.getelementsbytagname("marker"); (var = 0; < markers.length; i++) { var name = markers[i].getattribute("name"); var address = markers[i].getattribute("address"); //var image = '/images/someimage.png'; var lat = markers[i].getattribute("lat"); var lng = markers[i].getattribute("lng"); var point = new google.maps.latlng( parsefloat(lat), parsefloat(lng) ); //alert("data :" + name + " address " + address + " lat/lng " + lat + ":" + lng); locations++; // extend bounds include new point mymap.bounds.extend(point); var marker = new google.maps.marker({ position: point, map: mymap.map, //icon: image, clickable: true }); marker.info = new google.maps.infowindow({ content: '<strong>' + name + '</strong><br />' + address }); google.maps.event.addlistener(marker, 'click', function () { marker.info.open(mymap.map, marker); }); mymap.map.fitbounds(mymap.bounds); //alert("1: " + mymap.map.getzoom()); } }, 'xml'); } </code>
use "this" in listener click event, "marker" still contains last value had in loop:
google.maps.event.addlistener(marker, 'click', function () { this.info.open(mymap.map, marker); });
Comments
Post a Comment