{"id":976,"date":"2022-08-30T15:10:19","date_gmt":"2022-08-30T15:10:19","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/returning-json-array-from-a-function-to-a-variable-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:10:19","modified_gmt":"2022-08-30T15:10:19","slug":"returning-json-array-from-a-function-to-a-variable-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/returning-json-array-from-a-function-to-a-variable-collection-of-common-programming-errors\/","title":{"rendered":"Returning json array from a function to a variable-Collection of common programming errors"},"content":{"rendered":"<ul>\n<li>\n<p>Try like this:<\/p>\n<pre><code>function map_address(addr, callback) {\n\n    var input_address = addr;\n    var geocoder = new google.maps.Geocoder();\n    geocoder.geocode( { address: input_address }, function(results, status) {\n        if (status == google.maps.GeocoderStatus.OK) {\n            var lat = results[0].geometry.location.lat();\n            var lng = results[0].geometry.location.lng();\n\n\n                               jsonarr={'lat':lat,'lng':lng}\n                               alert(jsonarr.lat);\n                               callback(jsonarr) ;\n\n\n                            }\n        else {\n            alert(\"Nessuna coordinata trovata da questo indirizzo!\");\n            }\n        });\n}\nmap_address(\"hogehoge\", function(result){\n  alert(result)\n});\n<\/code><\/pre>\n<\/li>\n<li>\n<p>the <code>geocoder.geocode(..);<\/code> function is asynchronous as it wraps another function inside that will be called later when the geocode operation is done.<\/p>\n<p>because of this <code>map_address(...)<\/code> will always return <code>undefined<\/code><\/p>\n<\/li>\n<li>\n<p>Thanks all for the answers, i put all my code inside the callback and all work fine this is the final code.<\/p>\n<pre><code>   function map_address(addr,callback)\n        {\n\n            var input_address = addr;\n            var geocoder = new google.maps.Geocoder();\n            geocoder.geocode( { address: input_address }, function(results, status) {\n                if (status == google.maps.GeocoderStatus.OK) \n                {\n                    var lat = results[0].geometry.location.lat();\n                    var lng = results[0].geometry.location.lng();\n\n                    jsonarr={'lat':lat,'lng':lng}                       \n                    return callback(jsonarr);                        \n                }\n                else {\n                    alert(\"No coord find\");\n                }\n            });\n        }\n\n\n$(document).ready(function(){ \n   $(window).load(function(){ \n\nmap_address('address string',function(coord){\n\n\n  var center=new google.maps.LatLng(coord.lat,coord.lng);\n\n\n\n  var settings = {\n          zoom: 16,\n          center: center,\n          mapTypeControl: false,\n          mapTypeControlOptions: {style: google.maps.MapTypeControlStyle.DROPDOWN_MENU},\n          navigationControl: true,\n          navigationControlOptions: {style: google.maps.NavigationControlStyle.SMALL},\n          mapTypeId: google.maps.MapTypeId.ROADMAP\n      }; \n\n  var map=new google.maps.Map(document.getElementById(\"map\"), settings);\n\nvar marker = new google.maps.Marker({\nposition: new google.maps.LatLng(coord.lat,coord.lng),\nmap: map\n});\n\n\n});\n});\n\n});\n<\/code><\/pre>\n<\/li>\n<\/ul>\n<p id=\"rop\"><small>Originally posted 2013-11-09 23:09:52. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>Try like this: function map_address(addr, callback) { var input_address = addr; var geocoder = new google.maps.Geocoder(); geocoder.geocode( { address: input_address }, function(results, status) { if (status == google.maps.GeocoderStatus.OK) { var lat = results[0].geometry.location.lat(); var lng = results[0].geometry.location.lng(); jsonarr={&#8216;lat&#8217;:lat,&#8217;lng&#8217;:lng} alert(jsonarr.lat); callback(jsonarr) ; } else { alert(&#8220;Nessuna coordinata trovata da questo indirizzo!&#8221;); } }); } map_address(&#8220;hogehoge&#8221;, function(result){ [&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-976","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/976","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=976"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/976\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=976"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=976"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=976"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}