views:

1305

answers:

1

We have been developing an ASP.NET application that works with the Google Maps API to assist in logistics and planning for a small shipping company in our area. One of the features is for the company to input a list of customers and the system will package up all the customer addresses as a series of waypoints, send it off to Google, and get back the direction information for the route.

One problem that we have been having is that many of the addresses in our database are not great and will often times are not able to be processed by Google Maps. When we do this we receive back an error code, but I would like to be able to determine which waypoint was the one to fail (that way the client can then go and fix the address in the database).

EDIT Here is a chunk of the code that handles the initialization and current error handling:

function initialize() {
          if (GBrowserIsCompatible()) {      
            map = new GMap2(document.getElementById("map"));
            map.addControl(new GSmallMapControl());
            gdir = new GDirections(map);
            GEvent.addListener(gdir, "load", onGDirectionsLoad);
            GEvent.addListener(gdir, "error", handleErrors);

            if (document.getElementById("<%=hiddenWayPoints.ClientID %>").getAttribute("value") != '')
            {
                setDirections();
            }

          }
        }


        function setDirections() {
            var waypoints = new Array();
            var str = document.getElementById("<%=hiddenWayPoints.ClientID%>").getAttribute("value");

            waypoints = document.getElementById("<%=hiddenWayPoints.ClientID %>").getAttribute("value").split(":");

            gdir.loadFromWaypoints(waypoints, {getSteps:true});

        }

        function handleErrors(){
           if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
             alert("No corresponding geographic location could be found for one of the specified addresses. This may be due to the fact that the address is relatively new, or it may be incorrect.\nError code: " + gdir.getStatus().code);
           else if (gdir.getStatus().code == G_GEO_SERVER_ERROR)
             alert("A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Error code: " + gdir.getStatus().code);

           else if (gdir.getStatus().code == G_GEO_MISSING_QUERY)
             alert("The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.\n Error code: " + gdir.getStatus().code);

           else if (gdir.getStatus().code == G_GEO_BAD_KEY)
             alert("The given key is either invalid or does not match the domain for which it was given. \n Error code: " + gdir.getStatus().code);

           else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)
             alert("A directions request could not be successfully parsed.\n Error code: " + gdir.getStatus().code);

           else alert("An unknown error occurred.");

        }

The problem is that the LoadFromWayPoints() method in the GDirections object doesn't seem to return status based on each individual waypoint, but from just the entire list (if one fails it all fails).

The only real solution I can think of (without performing checks in other areas of the system) is to send each waypoint off to Google Maps in a separate request and check it's validity that way prior to sending off the entire waypoints list for the GDirections object, but that seems incredibly inefficient (especially when dealing with a larger set of customer locations).

+1  A: 

If you are using the GClientGeocoder object to do your requests, then you will get back an appropriate response code for each getLocations call:'

function logAddress (response)
{
    if (!response || response.Status.code != 200) 
    {
        // log the response.name and the response.Status.code
        return;
    } 

    // otherwise everything was fine
}

for (var i = 0; i < addresses.length; i++)
{
    var geocoder = new GClientGeocoder ();
    geocoder.getLocations (addresses[i], logAddress);
}

Their are various response codes, but I am guessing you want to inform the user when you get a 602 - Unknown Address.

EDIT:

Yep, you will only get a single error callback for loadFromWaypoints for the entire directions request. What you are doing is more than just a simple geocoding request, you are actually generating directions and rendering overlays to a map for a sequence of addresses values. I suggest a couple of solutions:

  • You could do a getLocation request before your loadFromWaypoints request (as you suggested) and then use the latitude,longitude data returned for each address as the parameter for your loadFromWayPoints. This splits the geocoding processing out of the loadFromWayPoints request, but adds the extra round trip for each geocoding lookup. This is really something you only want to do once, when the user first enters the address (see next).
  • When the user enters the address information for the first time you can do a GClientGeocoder getLocations lookup at the time and get the latitude,longitude to store in your database along with the address. That way, if the user enters an address that can't be geocoded, then you can ask them to re-enter the address, or perhaps let them select the location on a google map (and get the lat,lng from that). This doesn't solve the problems with address data you have now, but perhaps you can write some code to run through you existing data (offline) and flag the addresses in the db that are not able to be geocoded.
Cannonade
Your edit (which I am just now seeing, sorry) provided much better info pertaining to my particular problem. The two points you brought up were exactly what have been running through my mind (concerning the extra round trips if I split it up, and checking individual addresses when they are first enterred or selected). What I decided on is adding a verify button which will only check the individual address they want to (and only when they want to). This will save a lot of unnecessary checks I believe.
TheTXI