Basic error - but can't put my finger on it!

35 views
Skip to first unread message

Andy Newby

unread,
Oct 4, 2011, 2:43:09 PM10/4/11
to google-map...@googlegroups.com
Hi,

I'm trying to get a basic geocoder working, but can't seem to get the values passed back. The main codes are:

    function load() {
            geocoder = new google.maps.Geocoder();
           
            var latlng = new google.maps.LatLng(-34.397, 150.644);
            var myOptions = {
              zoom: 8,
              center: latlng,
              mapTypeId: google.maps.MapTypeId.ROADMAP
            }
            map = new google.maps.Map(document.getElementById("map"), myOptions);           
           
            var lat_lng_vals = codeAddress("Rudgwick, UK");
           
            alert("LAt: " + lat_lng_vals);
       
    }
 
  function codeAddress(address) {
    geocoder.geocode( { 'address': address}, function(results, status) {
      if (status == google.maps.GeocoderStatus.OK) {
        var test =  results[0].geometry.location.lat() + "," + results[0].geometry.location.lng();
        alert("TEST: " + test);
      } else {
        alert("Geocode was not successful for the following reason: " + status);
      }
    });
  }


The part:

        alert("TEST: " + test);

prints out the value I'm expecting fine:

TEST: 51.089816,-0.44929000000001906

..yet the values here are undef:

            var lat_lng_vals = codeAddress("Rudgwick, UK");
            alert("LAt: " + lat_lng_vals);

What am I doing wrong? FF debugger doesn't show any errors, but it never seems to get the values? Does it maybe need running using async? (cos the messages come up as "Lat:..." and THEN the "TEST:" alert)

TIA

geoco...@gmail.com

unread,
Oct 4, 2011, 2:50:50 PM10/4/11
to Google Maps JavaScript API v3
On Oct 4, 11:43 am, Andy Newby <andy.ne...@gmail.com> wrote:
> Hi,
>
> I'm trying to get a basic geocoder working, but can't seem to get the values
> passed back. The main codes are:
>
>
>   function codeAddress(address) {
>     geocoder.geocode( { 'address': address}, function(results, status) {
>       if (status == google.maps.GeocoderStatus.OK) {
>         var test =  results[0].geometry.location.lat() + "," +
> results[0].geometry.location.lng();
>         alert("TEST: " + test);
>       } else {
>         alert("Geocode was not successful for the following reason: " +
> status);
>       }
>     });
>   }
>
> The part:
>
>         alert("TEST: " + test);
>
> prints out the value I'm expecting fine:
>
> TEST: 51.089816,-0.44929000000001906
>
> ..yet the values here are undef:
>
>             var lat_lng_vals = codeAddress("Rudgwick, UK");
>             alert("LAt: " + lat_lng_vals);
>
> What am I doing wrong? FF debugger doesn't show any errors, but it never
> seems to get the values? Does it maybe need running using async?

Geocoding is asynchronous. You can't return the results that way, you
need to use them in the callback function.

-- Larry

Andy Newby

unread,
Oct 5, 2011, 6:05:33 AM10/5/11
to google-map...@googlegroups.com
Thanks, that did the trick :)

--
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 google-map...@googlegroups.com.
To unsubscribe from this group, send email to google-maps-js-a...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-maps-js-api-v3?hl=en.




--
Andy Newby
an...@ultranerds.co.uk
Reply all
Reply to author
Forward
0 new messages