Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Aug 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    JavaScript + Google Maps API

    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

    Code:
    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;
    	
    
    }

  • #2
    New to the CF scene
    Join Date
    Aug 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ok, I got it

    This helped:

    http://stackoverflow.com/questions/2...-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...
    Last edited by JS_Newbie; 08-27-2010 at 03:02 AM.

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,603
    Thanks
    80
    Thanked 4,634 Times in 4,596 Posts
    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!
    Code:
    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.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,603
    Thanks
    80
    Thanked 4,634 Times in 4,596 Posts
    Heh. Too late, huh?

    Okay, no how are you going to handle multiple addresses? Hint: You can't use just a for loop.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #5
    New to the CF scene
    Join Date
    Aug 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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....

    Code:
    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");
        }
    }

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,603
    Thanks
    80
    Thanked 4,634 Times in 4,596 Posts
    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/>
    Code:
    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/>
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •