Javascript closures with google geocoder -


i still have problems javascript closures, , input/output variables.

im playing google maps api no profit project: users place marker gmap, , have save locality (with coordinates) in db.

the problem comes when need second geocode in order unique pairs of lat , lng location: lets 2 users place marker in same town in different places, dont want have same locality twice in database differents coords.

i know can second geocode after user select locality, want understand mistaking here:

// first geocoding, take marker coords locality. geocoder.geocode(   {     'latlng': new google.maps.latlng($("#lat").val(), $("#lng").val()),     'language': 'it'   },   function(results_1, status_1){     // initialize html var inside closure     var html = '';     if(status_1 == google.maps.geocoderstatus.ok)     {       // stuff here       for(i = 0, geolen = results_1[0].address_components.length; != geolen)       {         // second type of geocoding: each location first geocoding,         // want have unique [lat,lan]         geocoder.geocode(           {             'address': results_1[0].address_components[i].long_name           },           function(results_2, status_2){             // here come problem. need have long_name here, ,             // 'html' var should increment.             coords = results_2[0].geometry.location.tourlvalue();             html += 'some html let user choose locality';           }         );       }       // finally, insert 'html' variable value dom...        //but never gets updated!     }     else     {       alert("error google geocoder:" + status_1)     }   } ); 

i tryed with:

  // second type of geocoding: each location first geocoding, want   // have unique [lat,lan]   geocoder.geocode(     {       'address': results_1[0].address_components[i].long_name     },     (function(results_2, status_2, long_name){       // in way i'll never results_2 or status_2, well, results_2        // long_name value, status_2 , long_name undefined.       // however, html var correctly updated.       coords = results_2[0].geometry.location.tourlvalue();       html += 'some html let user choose locality';     })(results_1[0].address_components[i].long_name)   ); 

and with:

  // second type of geocoding: each location first geocoding, want have   // unique [lat,lan]   geocoder.geocode(     {       'address': results_1[0].address_components[i].long_name     },     (function(results_2, status_2, long_name){       // obvious "results_2 not defined" error (same status_2).       coords = results_2[0].geometry.location.tourlvalue();       html += 'some html let user choose locality, can more one';     })(results_2, status_2, results_1[0].address_components[i].long_name)   ); 

any suggestion?

edit:

my problem how pass additional arguments geocoder inner function:

function(results_2, status_2, long_name){     //[...] } 

becose if clousure, mess original parameters (results_2 , status_2)

if i'm understanding correctly:

your problem in first example second (innermost) geocode's callback function (which appends string html) not have executed time reach line comment finally, insert 'html' variable value dom....

you launching second geocode request, , inserting html before operation has completed.


regarding passing argument callback, make function creates , returns function:

eg.

function(my_argument)
{
return(function(cb1,cb2) { ... });
}(my_argument_value);

then can pass in whatever want my_argument_value, , innermost code (...) see 2 callback args.

the return value of function pass callback geocode call.


Comments

Popular posts from this blog

linux - Mailx and Gmail nss config dir -

c# - Is it possible to remove an existing registration from Autofac container builder? -

php - Mysql PK and FK char(36) vs int(10) -