// Distance code: Used for determining the distance between two addresses. // To use this code, the google api key must be plugged into the page it will be used on. // For example: // // @author: Jess Mann - jess-mann.com // @date: January 8th, 2008 var map; var distance; var gdir; var map_loaded = false; var ALSO_CONTACT = "You may also contact us by phone for shipping rate information."; var PLEASE_CONTACT = "Please contact us by phone for shipping rate information."; var INVALID_ADDRESS = "The address supplied is invalid. Please check that it is valid. " + ALSO_CONTACT; var UNAVAILABLE_ADDRESS = "Shipping rates could not be determined for the address supplied. Please check that it is valid. " + ALSO_CONTACT; var UNKNOWN_ADDRESS = "The address supplied could not be found. Please check that it is valid. " + ALSO_CONTACT; var UNKNOWN_DIRECTIONS = "Directions could not be determined to the address provided. Please check that it is valid. " + ALSO_CONTACT; var SERVER_ERROR = "The shipping calculator is currently unavailable. " + PLEASE_CONTACT; var INVALID_DATA = "Some data supplied is invalid. Please check that your address and contact information are correct. " + ALSO_CONTACT; var ADDRESS_MISSING = "No address provided, but one is required!"; // Raise an alert by supplying the string provided, and a proper error code in parenthesis function alertError( error ) { alert(error +"("+ gdir.getStatus().code +")"); } // Load the directions to the map function setDirections(fromAddress, toAddress, locale) { map_loaded = true; gdir.load("from: " + fromAddress + " to: " + toAddress, { "locale": locale }); } // Handle any errors that arise, by spawning an alert function handleErrors() { // No error occurred. Huh? if (gdir.getStatus().code === G_GEO_SUCCESS) { return true; } // Reset the distance due to error, and show that we failed distance = 0; map_loaded = false; if (gdir.getStatus().code === G_GEO_UNKNOWN_ADDRESS) { alertError(UNKNOWN_ADDRESS); } else if (gdir.getStatus().code === G_GEO_UNAVAILABLE_ADDRESS) { alertError(UNAVAILABLE_ADDRESS); } else if (gdir.getStatus().code === G_GEO_UNKNOWN_DIRECTIONS) { alertError(UNKNOWN_DIRECTIONS); } else if (gdir.getStatus().code === G_GEO_SERVER_ERROR) { alertError(SERVER_ERROR); } else if (gdir.getStatus().code === G_GEO_MISSING_QUERY) { alertError(INVALID_DATA); } else if (gdir.getStatus().code === G_GEO_BAD_KEY) { alertError(SERVER_ERROR); } else if (gdir.getStatus().code === G_GEO_TOO_MANY_QUERIES) { alertError(SERVER_ERROR); } else if (gdir.getStatus().code === G_GEO_BAD_REQUEST) { alertError(INVALID_DATA); } else { alertError(SERVER_ERROR); } } // Do some processing after the directions load function onGDirectionsLoad() { // Only load the distance if we successfully loaded the map if (map_loaded === true) { distance = gdir.getDistance()["meters"]; } // Clear the map when we're done gdir.clear(); } // Initialize the map, and load the directions function initialize(address1,address2) { if (new GBrowserIsCompatible()) { // Create two invisible elements, for creation of the map object, and create it map = new GMap2(document.createElement("map_canvas")); gdir = new GDirections(map, document.createElement("directions")); // Add the appropriate listeners GEvent.addListener(gdir, "load", onGDirectionsLoad); GEvent.addListener(gdir, "error", handleErrors); // Pull up the directions, with a US locale setDirections(address1, address2, "en_US"); } } // Determine the format, process it, and return the value function getDistance( format ) { var value = 0; // If we weren't able to load the map, then don't process anything if (map_loaded === false || distance === 0) { return 0; } value = distance; if (format === "meters") { value = Math.round(distance); } else if (format === "kilometers") { value = Math.round(distance / 1000); } else if (format === "miles") { value = Math.round(distance / 1609.344); } else if (format === "feet") { value = Math.round(distance * 3.2808399); } else if (format === "blocks") { value = Math.round(distance / (1609.344 / 20)); } else if (format === "yards") { value = Math.round(distance * 1.0936133); } else { value = Math.round(distance); } return value; } function formatAddress( address, city, state, zip ) { var found = false; if (city.length > 0) { found = true; if (address.length > 0) { address = address.concat(", "); } address = address.concat(city); } if (state.length > 0) { found = true; if (address.length > 0) { address = address.concat(", "); } address = address.concat(state); } if (zip.length > 0) { if (address.length > 0) { if (found === true) { address = address.concat(", "); } else { address = address.concat(" "); } } address = address.concat(zip); } return address; }