0

I'm trying to do a simple geocode function for a map tool. I get the geocode fine, but I'm hoping to pass the location object back as the return value for the geocode function.

Something like this:

function codeAddress(address) {
    geocoder.geocode( { 'address': address}, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            var location = results[0].geometry.location;
            console.dir(location);
            return location;
        } else {
            return false;
        }
    });
}

the console.dir of the location item shows the expected location object, so the function is being called and is successfully returning data.

this function is called by another process, which would then build the markers.

if (coordinates = codeAddress(myaddress){
    // do stuff
}

However, the coordinates variable always evaluates as undefined, so the condition to "do stuff" is never met.

I know I'm probably missing something really obvious about the definition of the coordinates var, but I'm not sure what it is.

Thanks for help.

Basic code at: http://jsfiddle.net/2TXZ4/3/ though the map isn't getting drawn for whatever reason.

julio
  • 6,630
  • 15
  • 60
  • 82
  • ok it looks to me like the problem is that this is an asynchronous request-- if I hard-code a return value in the codeAddress function it all works as expected. So it must just be the nature of the request. Still not sure how to best deal with this issue however. – julio Jun 21 '11 at 20:18

2 Answers2

0

if (coordinates = codeAddress(myaddress) - that's an assignment, not a comparison.

Ok, here's a wild hack (that does work):

 <script type="text/javascript"> var geocoder; var loc; var l = false;  
 function  initialize() {

 geocoder = new google.maps.Geocoder();

 var mapDiv = document.getElementById('map_canvas');
     map = new google.maps.Map(mapDiv, {
      center: new google.maps.LatLng(-34.397, 150.644),
      zoom: 8,
       mapTypeId: google.maps.MapTypeId.ROADMAP
    });      }


function codeAddress(address) {
geocoder.geocode({
    'address': address
}, function(results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
        loc = results[0].geometry.location;             l = true;
        console.log(loc);           //alert (loc);
        //return loc;       doStuff(loc)
    } else {
        return false;
    }
}); }


 function doStuff(geo_marker){ if(l==false){
var thisaddress = document.getElementById("address").value;
var myResult = codeAddress(thisaddress); }else{

if (loc) {
    map.setCenter(loc);
    var marker = new google.maps.Marker({
        map: map,
        position: loc
    });
 l = false;  //important to use function more than once :))
} else {
    alert("Geocode was not successful");
}   }

};

google.maps.event.addDomListener(window, 'load', initialize); 
</script>



 <body>
 <div>
 <input id="address" type="textbox" value="Sydney, NSW">
 <input type="button" value="Geocode" onclick="window.doStuff()">
 </div>
 <div id="map_canvas" style="height:600px; width: 500px; position: absolute; 
  top: 80px">    </div> 

 </body>

I do think it makes more sense to do geocoding and add marker to the map in one function though, to avoid this calling back and forth between the functions.

AR.
  • 1,935
  • 1
  • 11
  • 14
  • @AR-- I've also tried the following, with the same results (ccord is undefined): `var coord = codeAddress(tempaddress); console.dir(coord); if (coord){ //dostuff }` – julio Jun 21 '11 at 19:03
  • I've updated my answer. The example definitely works, you just need to update it as you see fit. – AR. Jun 21 '11 at 22:25
0

The geocode method is asynchonous so you need to do any processing in the callback you are providing to the method or you can provide a callback to your codeAddress method like this similar question/answer describes (How do I return a variable from Google Maps JavaScript geocoder callback?). In the code below, I moved what you were doing in the doStuff function into the callback of the geocode function - it should work, but I can't test it with the jfiddle link you provided (probably due to Google Maps API keys). I hope this helps!

function codeAddress(address) {
    geocoder.geocode({
        'address': address
    }, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            var location = results[0].geometry.location;
            console.log(location);
            map.setCenter(location);
            var marker = new google.maps.Marker({
                map: map,
                position: location
            });
        } else {
            alert("Geocode was not successful for " + address);
        }
    });
}


window.doStuff = function() {
    var address = document.getElementById("address").value;
    codeAddress(address);
}
Community
  • 1
  • 1
bamana
  • 1,625
  • 12
  • 15
  • this is correct, and what I ended up doing. I still created the markers in the "doStuff" function, but then used the marker.setPosition(location) method in the codeAddress callback to set the position. – julio Jun 21 '11 at 22:21