google maps - How to pass a variable into a function defined in the argument of another function in JavaScript? -


i working on google maps api v3 javascript. using geocoder feature passing city name , plotting on map. along marker, want add separate info window every marker not able that. infowindows displaying content set last marker instead of setting different info window each. please find javascript code below:

   function initialize() {               geocoder = new google.maps.geocoder();               var address = document.getelementbyid('address').value;               var jstring = json.parse(address);               var infowindow;               (var key in jstring) {                   contentstr = json.stringify(jstring[key]); //this step setting content independent every maker                   if (jstring.hasownproperty(key)) {                       geocoder.geocode( { 'address': key}, function(results, status) {                             if (status == google.maps.geocoderstatus.ok) {                               map.setcenter(results[0].geometry.location);                               var marker = new google.maps.marker({                                   map: map,                                   position: results[0].geometry.location                               });                               infowindow = new google.maps.infowindow({                                   content: contentstr//not able access 'contentstr' set above.                                   });                               infowindow.open(map,marker);                                 } else {                               alert('geocode not successful following reason: ' + status);                             }                           });                    }               }                var latlng = new google.maps.latlng(-34.397, 150.644);               var mapoptions = {                 zoom: 8,                 center: latlng               }               map = new google.maps.map(document.getelementbyid('map-canvas'), mapoptions);             }               google.maps.event.adddomlistener(window, 'load', initialize); 

after debugging understood reason, value of variable 'contentstr' being set during last iteration of 'for' loop being used below function calls made:

   geocoder.geocode( { 'address': key}, function(results, status) { 

so basically, not able pass variable 'contentstr' function defined in argument of function. think missing basic here. please guide me this?

thank you!

i have had same problem many times. stolen link below. need attach infowindow marker.

marker content (infowindow) google maps

function createmarker(lat, lon, html) {     var newmarker = new google.maps.marker({         position: new google.maps.latlng(lat, lon),         map: map,         title: html     });      newmarker['infowindow'] = new google.maps.infowindow({             content: html         });      google.maps.event.addlistener(newmarker, 'mouseover', function() {         this['infowindow'].open(map, this);     }); } 

a second way of doing this. :

google maps api v3 infowindow - infowindows displaying same content


Comments

Popular posts from this blog

java - Intellij Synchronizing output directories .. -

git - Initial Commit: "fatal: could not create leading directories of ..." -