Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is a common problem (usually encountered with infowindows), it can be solved with function closure (a "createMarker" function is one option).</p> <pre><code>function createMarker(map, latlng, address, image) { var marker = new google.maps.Marker({ map: map, icon: 'http://www.google.com/mapfiles/marker'+image+'.png', position: latlng }); var info = new google.maps.InfoWindow({ content: address+', India' }); google.maps.event.addListener(marker, 'click', function() { info.open(map, marker); }); return marker; } </code></pre> <p>call it inside your loop like this:</p> <pre><code>geocoder.geocode({ 'address': address[i]+", India" },function(results, status){ if(status == google.maps.GeocoderStatus.OK) { var marker = createMarker(results[0].geometry.location, address[i], images[i]); } }); </code></pre> <p><a href="http://www.geocodezip.com/v3_SO_simpleMap_geocodeCustomIconA.html" rel="nofollow">working example</a> (the code you posted didn't behave as you described, includes function closure for the geocoding operation as well)</p> <p><strong>UPDATE:</strong> You also need function closure on the address (a "geocodeAddress" function):</p> <pre><code>function geocodeAddress(map, address, i) { geocoder.geocode({ 'address': address+", India" },function(results, status){ if(status == google.maps.GeocoderStatus.OK) { var marker = createMarker(map, results[0].geometry.location, address, images[i]); } }); } </code></pre> <p>So your processing loop becomes:</p> <pre><code>var i=1; for(i=0; i&lt;address.length; i++){ geocodeAddress(map, address[i], i); } </code></pre> <p><strong>code snippet:</strong></p> <p><div class="snippet" data-lang="js" data-hide="false"> <div class="snippet-code"> <pre class="snippet-code-js lang-js prettyprint-override"><code>var geocoder; var address = new Array(); var marker = new Array(); var images = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L"]; function initialize() { var locations = 'Pune\;Mumbai\;'; // locations=locations.substring(0,(locations.length)-1); var map = new google.maps.Map(document.getElementById('googleMap'), { mapTypeId: google.maps.MapTypeId.ROADMAP, zoom: 8 }); if (locations == 'null' || locations == '') locations = 'Pune, INDIA\;'; address = locations.split('\;'); marker[0] = new google.maps.Marker({ map: map }); geocoder = new google.maps.Geocoder(); var i = 1; for (i = 0; i &lt; address.length; i++) { geocodeAddress(map, address[i], i); } geocoder.geocode({ 'address': address[0] + ", India" }, function(results, status) { if (status == google.maps.GeocoderStatus.OK) { // marker[0].setPosition(results[0].geometry.location); map.setCenter(results[0].geometry.location); new google.maps.Circle({ center: results[0].geometry.location, radius: 1000, // radius * 1000, // Convert to meters fillColor: '#00ff00', fillOpacity: 0.2, map: map, strokeColor: '#0000ff', strokeOpacity: 0.3, strokeWeight: 1 }); } }); } //End of initialize function. function geocodeAddress(map, address, i) { geocoder.geocode({ 'address': address + ", India" }, function(results, status) { if (status == google.maps.GeocoderStatus.OK) { var marker = createMarker(map, results[0].geometry.location, address, images[i]); } }); } function createMarker(map, latlng, address, image) { var marker = new google.maps.Marker({ map: map, icon: 'http://www.google.com/mapfiles/marker' + image + '.png', position: latlng }); var info = new google.maps.InfoWindow({ content: address + ', India' }); google.maps.event.addListener(marker, 'click', function() { info.open(map, marker); }); return marker; } google.maps.event.addDomListener(window, 'load', initialize);</code></pre> <pre class="snippet-code-css lang-css prettyprint-override"><code>html, body, #googleMap { height: 100%; width: 100%; }</code></pre> <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;script src="https://maps.googleapis.com/maps/api/js"&gt;&lt;/script&gt; &lt;div id="googleMap"&gt;&lt;/div&gt;</code></pre> </div> </div> </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload