{"id":552,"date":"2022-08-30T15:03:15","date_gmt":"2022-08-30T15:03:15","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/unable-to-access-array-inside-geocoder-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:03:15","modified_gmt":"2022-08-30T15:03:15","slug":"unable-to-access-array-inside-geocoder-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/unable-to-access-array-inside-geocoder-collection-of-common-programming-errors\/","title":{"rendered":"Unable to access array inside geocoder-Collection of common programming errors"},"content":{"rendered":"<p>The geocoder is asynchronous. The loop spins through all the possible values of i, leaving i set to postCode.length+1 which is undefined. This can be addressed with function closure (however, depending on the number of locations you have, you may have issues with the quota or rate limit):<\/p>\n<pre><code>function geocodeAddress(index) {\n    geocoder.geocode({'address': postCode[index]}, function(results, status) {\n      if (status == google.maps.GeocoderStatus.OK) {\n        var lat2 = results[0].geometry.location.lat();\n        var lng2 = results[0].geometry.location.lng();\n        var Latlng = new google.maps.LatLng(lat2, lng2);\n        var marker = new google.maps.Marker({\n                           position: Latlng,\n                           map: map,\n                           title: titles[index], \n                           icon: icon\n                         });\n     } else { alert(\"geocode failed:\"+status);\n   });\n}\n\nfor(var i = 0; i &lt; length; i++)\n{\n   geocodeAddress(i);\n}\n<\/code><\/pre>\n<p id=\"rop\"><small>Originally posted 2013-11-09 20:50:45. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>The geocoder is asynchronous. The loop spins through all the possible values of i, leaving i set to postCode.length+1 which is undefined. This can be addressed with function closure (however, depending on the number of locations you have, you may have issues with the quota or rate limit): function geocodeAddress(index) { geocoder.geocode({&#8216;address&#8217;: postCode[index]}, function(results, status) [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-552","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/552","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=552"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/552\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=552"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=552"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=552"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}