On Aug 24, 10:38 am, jontes <[email protected]> wrote: > Hello, > I'm new to Google Maps and today i tried to make a simple map. I > followed tutorial and all went well, so i decided to implement maps > into my webpage. Long story short, the example function codeAddress() > can'r return the "results[0].geometry.location" data or (and that's > more probable) I do something wrote. Here's the code: > > function codeAddress(adresa) { > var geocoder = new google.maps.Geocoder(); > geocoder.geocode( { 'address': adresa}, function(results, status) > { > if (status == google.maps.GeocoderStatus.OK) { > return results[0].geometry.location; > } else { > alert("Geocode was not successful for the following reason: " > + status); > } > }); > } > > var loc = codeAddress('Sydney'); > document.write(loc); > > It outputs just text "undefined". But when I output it to a html > element (in that function) it works. > > I don't know Javascript very well, so I'd like to ask you, what's > wrong.
Geocoding is asynchronous, it sends the request off to the server and runs the callback function when the data returns. The codeAddress function can't return the result. See Mike Williams' explanation in his v2 tutorial for more information: Javascript Concepts - Part 2 Asynchronous I/O http://econym.org.uk/gmap/async.htm -- Larry > > Thanks a lot -- You received this message because you are subscribed to the Google Groups "Google Maps JavaScript API v3" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/google-maps-js-api-v3?hl=en.
