...

View Full Version : JavaScript + Google Maps API



JS_Newbie
08-27-2010, 12:47 AM
Hey all, I was wondering if someone would be able to help me with a problem I've been having. I'm attempting to find coordinates for several addresses at a time. I'm using a for-loop to loop through an array of addresses. I figured it would be best to have a separate function to evaluate the coordinate given an address, and just call it in every loop.

Note that my code below is simplified (I removed the for loop because that's not where I'm having trouble). I know that the issue is probably because I'm trying to return a value from an anonymous function... I tried making the variable global, I tried having a return line from within the anonymous function, nothing has worked... any tips?

Thanks -- Code follows



function initialize(){

var geocoder = new google.maps.Geocoder(); // creating a new geocode object
var locCood = findcood('44106', geocoder);
}

function findcood(addr, geocoder)
{
var cood;
if (geocoder) {

geocoder.geocode({'address': addr}, function(results, status, cood){
if (status == google.maps.GeocoderStatus.OK)
cood = results[0].geometry.location;

else {
cood = null;
alert("Geocode was not successful for the following reason: " + status);

}

});

}
else
{
alert("No Geocode");
cood = null;
}
alert(cood);
return cood;


}

JS_Newbie
08-27-2010, 02:00 AM
Ok, I got it

This helped:

http://stackoverflow.com/questions/2993563/how-do-i-return-a-variable-in-javascript

I realized that there was something wrong with the timing when I printed an alert inside the anonymous function and after the findcood call in the initialize function... That helped narrow things down when searching for an answer...

Old Pedant
08-27-2010, 02:04 AM
Making the variable global should have worked.

There are other ways, but I can't see why that didn't do it.

Oh, wait! I see it!


function(results, status, cood){

Even if you had a global cood variable, the local variable that is implied by that function parameter would override it within the function!!!

ZAP that parameter. It's not doing anything that I can see, other than making your life miserable.

Old Pedant
08-27-2010, 02:05 AM
Heh. Too late, huh?

Okay, no how are you going to handle multiple addresses? Hint: You can't use just a for loop.

JS_Newbie
08-27-2010, 02:53 AM
Thanks for the input. Apologies for the mistake with including a the cood variable as a parameter, I had edited the code so much that I missed changing that bit (I didn't have it in there at first).

This is so frustrating and confusing though. Adding the for loop brings me back to square one. The code below gives me "undefined" the first time I run it, but gives me results the second time, it's weird....



var addrAr = ['44106', '90210', '19420'];
var coordAr = new Array(addrAr.length);



function initialize(){

var geocoder = new google.maps.Geocoder(); // creating a new geocode object



for(var i=0; i<addrAr.length; i++)
{

(function (i) {
findcood(addrAr[i], geocoder, function(coord) {coordAr[i] = coord;});
})(i);
}


for(var k=0; k<coordAr.length; k++)
{
alert(coordAr[k]);
}




}

function findcood(addr, geocoder, callback){
if (geocoder) {

geocoder.geocode({'address': addr}, function(results, status){
if (status == google.maps.GeocoderStatus.OK) {
callback(results[0].geometry.location);
}
else {
alert("Geocode was not successful for the following reason: " + status);
}
});


}
else {
alert("No Geocoder");
}
}

Old Pedant
08-27-2010, 08:38 PM
Well, this is my own stab at it.

I tend to believe in keeping things simple.

Plus I don't believe the geocoder can handle multiple address requests at same time (could well be wrong on that!).

Untested. Will let you do that. <grin/>



var addrAr = [
... your array of addresses ...
];
var coordAr = [ ]; // coordinate array to be initialized

var currentAddress = 0; // working on this address

var geocoder = null;

function initialize()
{
geocoder = new google.maps.Geocoder(); // a new geocoder object
findCoord( );
}

function findCoord( )
{
if (geocoder == null )
{
alert("Disaster! No geocoder!");
return;
}

geocoder.geocode( {'address': addrAr[currentAddress] }, findResults );
}

function findResults(results, status)
{
if (status != google.maps.GeocoderStatus.OK)
{
alert("Geocode was not successful for the following reason: " + status);
return;
}
// success
coordAr[currentAddress] ] = results[0].geometry.location);
++currentAddress;
if ( currentAddress < addrAr.length )
{
findCoord( );
} else {
alert("Done!");
// and whatever else you want to do
}
}

p.s.: I just couldn't handle shortening "coordinates" to "cood". Sorry. <grin/>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum