javascript - How to run success function with JQuery getJSON? -


i want push data array, unsure how go due ajax's asychronisity.

i can't seem find complete examples online of way done getjson.

i've been following documentation here no luck/

i have tried:

var jsonltdlng="https://maps.googleapis.com/maps/api/geocode/json?address=" + addressval; var latlng;  $.getjson(jsonltdlng, function (data) {   var lat = data.results[0].geometry.location.lat;   var lng = data.results[0].geometry.location.lng;    latlng = {lat: lat, lng: lng};  }).done(function() {   markerlocations.push(latlng); })//end json 

and:

var jsonltdlng="https://maps.googleapis.com/maps/api/geocode/json?address=" + addressval; var latlng;

$.getjson(jsonltdlng, function (data) {   var lat = data.results[0].geometry.location.lat;   var lng = data.results[0].geometry.location.lng;    latlng = {lat: lat, lng: lng};   markerlocations.push(latlng); }); 

and variations of no results.

would know correct way of doing , can point me in right direction?

you need push() array in success handler has access data returned request. better add objects array instead of manually hacking around json string. there can pass array whatever function need execute. try this:

var markerlocations = []; $.getjson(jsonltdlng, function (data) {     var lat = data.results[0].geometry.location.lat; // note 'lat' here, not 'lng'     var lng = data.results[0].geometry.location.lng;     markerlocations.push({ lat: lat, lng: lng });     dosomethingwiththearray(markerlocations); });  function dosomethingwiththearray(arr) {     (var = 0; < arr.length; i++) {         console.log(arr[i].lat, arr[i].lng);     } } 

Comments

Popular posts from this blog

Hatching array of circles in AutoCAD using c# -

ios - UITEXTFIELD InputView Uipicker not working in swift -

Python Pig Latin Translator -