SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Zealot kobra's Avatar
    Join Date
    Sep 2003
    Location
    Chicago
    Posts
    190
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    javascript callback return problem

    I'm having problem with a javascript callback function. I'm using google map api to get the coordinates of an address and calculate the distance between those two points. I have the function that calculates the distance between to points, however I cannot get the lat and long passed to that function.

    As far as I know you can only return bool when using callback function.

    Code:
    function getStoreDistance(sUserAddress, sStoreAddress) {
    
      oGeocoder.getLocations(sUserAddress, getAddressLatLong);
      oGeocoder.getLocations(sStoreAddress, getAddressLatLong);
    
      // here I need to get the values from the callback function to pass them to the calcDistastance ????
    
      return calcDistance(lat1, lon1, lat2, lon2, unit);
    }
    
    function getAddressLatLong(response) {
    
      if (!response || response.Status.code != 200) {
        alert("Sorry, we were unable to geocode that address");
      }
      else {
    
        var latitude   = response.Placemark[0].Point.coordinates[1];
        var longtitude = response.Placemark[0].Point.coordinates[0];
      }
    }
    Any ideas or workarounds you'd suggest?

    Thanks,
    Krasen
    Last edited by kobra; Nov 27, 2006 at 19:07.


  2. #2
    SitePoint Enthusiast
    Join Date
    Nov 2005
    Location
    London, UK
    Posts
    81
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How about a global variable like this:
    Code:
    var latitude = null;
    var longtitude = null;
    
    function getStoreDistance(sUserAddress, sStoreAddress) {
    
      oGeocoder.getLocations(sUserAddress, getAddressLatLong);
      lat1 = latitude;
      lon1 = longtitude;
      if (lat1==null) return;
      oGeocoder.getLocations(sStoreAddress, getAddressLatLong);
      lat2 = latitude;
      lon2 = longtitude;
      if (lat2==null) return;
    
      return calcDistance(lat1, lon1, lat2, lon2, unit);
    }
    
    function getAddressLatLong(response) {
    
      if (!response || response.Status.code != 200) {
        alert("Sorry, we were unable to geocode that address");
        latitude = longtitude = null;
      }
      else {
        latitude   = response.Placemark[0].Point.coordinates[1];
        longtitude = response.Placemark[0].Point.coordinates[0];
      }
    }


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
  •