SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Hybrid View

  1. #1
    runat="server" Golgotha's Avatar
    Join Date
    Nov 2001
    Location
    Colorado
    Posts
    2,085
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    JavaScript return issue

    Code:
    var geocoder = new google.maps.Geocoder();
    
    function GetLatLngFromZip(zip) {
        return geocoder.geocode({ 'address': zip }, function (results, status) {
    
            if (status == google.maps.GeocoderStatus.OK) {
                console.log(results[0].geometry.location);
                return results[0].geometry.location
            }
        });
    };
    I must be missing something, but I can't get the above function to return the result?

  2. #2
    padawan silver trophybronze trophy markbrown4's Avatar
    Join Date
    Jul 2006
    Location
    Victoria, Australia
    Posts
    4,095
    Mentioned
    28 Post(s)
    Tagged
    1 Thread(s)
    Does status != google.maps.GeocoderStatus.OK ?

  3. #3
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,046
    Mentioned
    16 Post(s)
    Tagged
    3 Thread(s)
    That function will never return the result because geocoding is an asynchronous process within the context of gmap3. Which is why a callback is being passed upon calling the method. So when the geocoder sends back the data the callback is called allowing the outside world to do something with that value. The short of it is that the GetLatLngFromZip() function requires an additional argument that should be a function which pases the zipcode value once it has been resolved.
    The only code I hate more than my own is everyone else's.

  4. #4
    runat="server" Golgotha's Avatar
    Join Date
    Nov 2001
    Location
    Colorado
    Posts
    2,085
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    want to show me what that code would look like?

  5. #5
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,046
    Mentioned
    16 Post(s)
    Tagged
    3 Thread(s)
    Code JAVASCRIPT:
    var geocoder = new google.maps.Geocoder();
     
    function GetLatLngFromZip(zip,crapToDoAfterwards) {
        geocoder.geocode({ 'address': zip }, function (results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                crapToDoAfterwards(results[0].geometry.location);
            }
        });
    };
    The only code I hate more than my own is everyone else's.


Bookmarks

Posting Permissions

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