The application had problems with providing driving directions in consistent manner. This mainly presented itself when the user entered an incomplete address. Providing a zip code or a just a city name will plot a point on that feature's centroid. This type of address was sometimes incompatible with the the GDirections object. Many times this would return a status code of 602. Other times it would work correctly. So what to do?
Digging deeper into the GDirections object it appears that it will take addresses or lat long pairs or a combination of both. We experimented with supplying lat long pairs for the start and end points in Google Maps. We were viewing a rural area and right clicked in the middle of a field to select directions from here. When the directions plotted it used the closest road as the start point. Eureka!
So then the application was rewritten to utilize only geographic points for plotting driving directions. But this did not work as well as we had hoped. Directions generated via this method do not have a fully formed address as the origin or destination in the directions listing. Instead of '123 Main St.' The origin would be listed as just 'Main St.'. The marker is plotted exactly where we want it, however, the user is just not informed of the destination address.
So it appears that a hybrid approach will solve the problem of the 602 errors. We will structure our code to attempt to use the incomplete addresses and if that does not work we will drop back to just using the the geographic points. To see how this works let's take a look at the code snippet below. ( ${variable_name} indicates an injection point from the jsp )
function initDirections() {
directionsMap = new GMap2(document.getElementById("drivingMap"));
// try to use addresses.
var startAddress = '${spAddress}';
var endAddress = '${epAddress}';
addressDirections = new GDirections(directionsMap, document.getElementById('drivingDetails'));
GEvent.addListener(addressDirections, "error", initLatLongDirections);
addressDirections.load("from: ${spAddress} to: ${epAddress}", {travelMode:G_TRAVEL_MODE_DRIVING});
}
function initLatLongDirections() {
//alert("dropping to latlong becuase of error" + status);
var status = addressDirections.getStatus().code;
if ( addressDirections.getStatus().code != 200 )
{
var startLatLng = new GLatLng( '${spLatitude}', '${spLongitude}' );
var endLatLng = new GLatLng( '${epLatitude}', '${epLongitude}' );
latlongDirections = new GDirections(directionsMap, document.getElementById('drivingDetails'));
GEvent.addListener(latlongDirections, "error", onDirectionsError);
latlongDirections.load("from: ${spLatitude}, ${spLongitude} to: ${epLatitude}, ${epLongitude}", {travelMode:G_TRAVEL_MODE_DRIVING});
}
}
function onDirectionsError(mode) {
var status = latlongDirections.getStatus().code;
if (status != 200)
{
document.getElementById("directionsGenerated").value = 'false';
document.getElementById("mapDisplayStatus").value = 'error';
document.getElementById("termSearchDisplay").style.display = '';
document.getElementById("termSearchAddress").style.display = '';
document.getElementById("errorMapDisplayText").style.display = '';
document.getElementById("mapHeader").style.display ='none';
document.getElementById("mapDetail").style.display='none';
}
}
Notice that we define an event handler for the first attempt at getting directions, however, we really don't use it to display an error. We pass a function pointer to initLatLongDirections() to attempt to resolve the directions using the this function. Within this function we create a new event handler that points the actual error function.
No comments:
Post a Comment