Hi,
This is amit from india. I have used google map api to find out the
driving distance among muliple points. We have used the setTimout()
java script funtion to induce deley to iterate the google listener for
driving distance calculation without any error. We have used 10ms
delay in java script function setTimeOut(). Its working fine at my
machine in india. But our client in US, is getting error 604 for same.
When we induce more delay say 100ms sec then code works fine at client
end also. Here, i am sending my code to you. please have look at in it
and give your suggestion.
/-------------------------------------Code
Start-----------------------/
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://
www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<%@ page import="java.util.*" %>
<%@ page import="fpcecombtob.ServiceLocationBean" %>
<!--
- Author(s):Amit Kumar Singh
- Date:27/04/2009
- Description: Find the closest Service Location using Google
MAP API.
-->
<%-- Cache clear snippets --%>
<%
response.setHeader("Cache-Control","no-cache");
response.setHeader("Pragma","no-cache");
response.setDateHeader ("Expires", 0);
%>
<%--Create JSP variables --%>
<%! ArrayList serviceLocCoordinateList = null;
String address = null;
String coordinate = null;
String contextPath = null;
int sizeServiceLoc = 0;
%>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
<title>Find Closest Service Location</title>
<!-- Reference to Google Map API key. -->
<script src="http://maps.google.com/maps?file=api&v=2&key
=ABQIAAAAq8SmCREBqYuu0as5qXXPoRRiTKb8v1Jjl_n5BMF8t9GlQDheWRSci54wZZmkAv0qYs6Uyto8qoqh_g
=true_or_false" type="text/javascript">
</script>
<script type="text/javascript">
/* distanceCriteria' variable holds 'Criteria One' - if there exists a
service
location within 90 minutes driving distance and 'Criteria Second' -
if there exists
a service location within 90-180 minutes driving distance. If no
service location exist
in both criteria then 'distanceCriteria' variable holds 'No Service
Location Exist'*/
var distanceCriteria;
<%--Get the context path--%>
<% contextPath = request.getContextPath(); %>
<%--Get the Service Location Coordinate list from request--%>
<%
if(request.getAttribute("serviceLocCoordinateList")!=null){
serviceLocCoordinateList
=
(ArrayList)request.getAttribute("serviceLocCoordinateList");
}
if(serviceLocCoordinateList!=null){
sizeServiceLoc = serviceLocCoordinateList.size();
}
%>
/* Create variable to hold the java arrayList size */
var arrayLengthServLoc = "<%=sizeServiceLoc%>";
/* Create variables for driving distance calculation between service
locations and user entered zip-code. */
var distance, distanceArray, geocoder, nextDistance,
servLocAddressArray, serLocAndPointArr,
address;
/* Create variable to hold driving distance of 90 minutes, i.e. 5400
seconds. */
var drivingCriteria = 5400;
/* Create variable to hold driving distance of 180 minutes, i.e. 10800
seconds. */
var drivingCriteriaSecond = 10800;
/* 10 miliseconds, delay to be inducted in between execution of
listener code in a loop */
var delay = 10;
/* Create two dimensional array to hold the coordinates for Service
Locations provided. */
var servLocCoordinateArray = new Array(2);
var mySampleForm = null;
/**
* This function gets invoked onload event and store the service
locations details from
* java arraylist to java script two diamension array.
*/
function globalFunction(){
/* Check browser compatibility */
if (GBrowserIsCompatible()) {
geocoder = new GClientGeocoder();
}
/* Create Object of Form */
mySampleForm = document.getElementById("sampleForm");
for(var i=0; i<servLocCoordinateArray.length; i++){
servLocCoordinateArray[i] = new Array(arrayLengthServLoc);
}
/* Iterating List and populating the Service Location Details into
java
script two diamension array */
<%
if(serviceLocCoordinateList != null){
ServiceLocationBean serviceLocation ;
for(int i=0; i<serviceLocCoordinateList.size(); i++){
if(serviceLocCoordinateList.get(i)!=null){
serviceLocation =
(ServiceLocationBean)serviceLocCoordinateList.get
(i);
if(serviceLocation!=null){
address = serviceLocation.getAddress();
coordinate
=serviceLocation.getCoordinates();
}
}
%>
/* Create a seperate array for having all coordinates
and address
of each Service Location */
for(var j=0;j<servLocCoordinateArray.length; j++){
if(j==0){
servLocCoordinateArray[j]["<%=i%>"] =
"<%=coordinate%>";
}else if(j == 1){
servLocCoordinateArray[j]["<%=i%>"] =
"<%=address%>";
}
}
<%
}
}
%>
}
/**
* This function initiate the loop counter, check mandatory validation
on field zip-code
* reset closestLocation and calculatedDrivingDistance fields, and hide
the
* information labels.
*/
function setCounter(){
nextDistance = 0;
distanceArray = new Array();
servLocAddressArray = new Array();
/* Create two dimensional java script array */
serLocAndPointArr = new Array(2);
displayProgressStatus();
getClosestServiceLoc();
}
/**
* This function gets called from function setCounter internally and
calculate the closest
* service location for user entered zip-code and check whether same
service location
* exists inside or outside of 90 minutes driving distance.
*/
function getClosestServiceLoc(){
if (geocoder) {
/* Using google api, Calculating the point
(latitude, lognitude) for user entered zip-code */
geocoder.getLatLng(address,function(point) {
/* Calculating Co-ordinate. */
var targetLoclognitude = point.lng().toFixed(5);
var targetLocLatitude = point.lat().toFixed(5);
/* Calculating point(latitude, lognitude). */
var targetLocPoint = new GLatLng
(targetLocLatitude,targetLoclognitude);
/* Null Check */
if(servLocCoordinateArray != null){
if(nextDistance <= arrayLengthServLoc){
if(servLocCoordinateArray[0][nextDistance] != null){
/* Adding time delay for
distance between service location
zip-Codes and user
entered location zip-code */
setTimeout('calculateDistance("'+targetLocPoint
+'","'+servLocCoordinateArray[0][nextDistance]
+'","'+servLocCoordinateArray[1][nextDistance]
+'",getClosestServiceLoc)', delay);
}
if(nextDistance == arrayLengthServLoc){
var closestDistance;
var closestServiceLoc;
/* Creating two diamension
array */
for (var k=0; k
<serLocAndPointArr.length; k++){
serLocAndPointArr[k] =
new Array(servLocAddressArray.length);
}
/* Null Check */
if(distanceArray != null &&
servLocAddressArray != null){
for(var i=0;
i<servLocAddressArray.length; i++){
for(var j=0;
j<serLocAndPointArr.length; j++){
if(j==0){
serLocAndPointArr[j][i] = distanceArray[i];
}else{
serLocAndPointArr[j][i] = servLocAddressArray
[i];
}
}
}
/* Iterating the array
to calculate closest distance and
closest service
loation*/
closestDistance =
serLocAndPointArr[0][0];
closestServiceLoc =
serLocAndPointArr[1][0];
for(var i=0;
i<servLocAddressArray.length; i++){
if(closestDistance > serLocAndPointArr[0][i]){
closestDistance = serLocAndPointArr[0][i];
closestServiceLoc = serLocAndPointArr[1][i];
}
}
}
hideProgressStatus();
/* Check whether service
location exists within 90 minutes
of driving distance or not*/
if(closestDistance>drivingCriteria){
/* Check whether
service location exists within 180 minutes
of driving
distance or not*/
if(closestDistance>drivingCriteriaSecond){
distanceCriteria = "No Service Location exist."
}else
if(closestDistance<=drivingCriteriaSecond){
distanceCriteria = "Criteria Second";
}
}else
if(closestDistance<=drivingCriteria){
distanceCriteria =
"Criteria One";
}
alert("distanceCriteria:
"+distanceCriteria);
}
nextDistance++;
}
}
});
}
}
/**
* Hide progress status and disable the button 'Closest Service
Location'.
*/
function hideProgressStatus(){
document.getElementById("showScreenSplashNew").style.display =
"none";
document.getElementById("calculateDistance").disabled = false;
document.getElementById("calculateDistance").style.display = "none";
document.getElementById("address").disabled = false;
document.getElementById("validateZipCodeNew").disabled = false;
}
/**
* Display progress status and disabled the button 'Closest Service
Location'
*/
function displayProgressStatus(){
document.getElementById("showScreenSplashNew").style.display = "";
document.getElementById("calculateDistance").disabled = true;
document.getElementById("address").disabled = true;
document.getElementById("validateZipCodeNew").disabled = true;
}
/**
* Calculating driving distance between user entered location and all
service Locations.
*
* @param point1 - Target location points(latitude, Lognitude).
* @param point2 - Service location points(latitude, Lognitude).
* @param servLocAddress - Service Location address.
* @param next - This parameter is used to call next function.
*/
function calculateDistance(point1,point2,servLocAddress,next){
var direction = new GDirections();
/* Null Check */
if(point1 != null && point2 != null && servLocAddress != null &&
next != null){
/* Getting the direction between point1 and point2 */
direction.loadFromWaypoints([point1,point2]);
/* Calculating driving distance between point1 and point2 */
GEvent.addListener(direction,"load", function() {
/* Calcualtion driving distance in secconds from
direction */
distance = direction.getDuration().seconds;
distanceArray.push(distance);
servLocAddressArray.push(servLocAddress);
next();
});
}
/* Using listener, display Error message if direction is undefined
between
point1 and point2 */
GEvent.addListener(direction,"error",function() {
alert("Error Code- "+direction.getStatus().code+" : No route
available between "
+"the entered location and service locations
points.");
hideProgressStatus();
});
}
/**
* This function gets invoked at onclick event of button 'Validate Zip
Code' and validate
* the user entered zip code.
*/
function validateZipCode(){
address = mySampleForm.address.value;
var strValidChars = "0123456789";
var strChar;
var flag = true;
/* Validate the lenght of entered Zip Code */
if (address.length != 5){
hideButtonDisplay();
alert("Please enter correct zip code.");
document.getElementById("address").focus();
flag = false;
}else if (address.length == 5){
/* Numeric check */
for (i = 0; i < address.length && flag == true; i++){
strChar = address.charAt(i);
if (strValidChars.indexOf(strChar) == -1){
flag = false;
alert("Zip Code format is not numeric.");
document.getElementById("address").focus();
hideButtonDisplay();
}
}
if(flag == true){
geocoder.getLatLng(address,function(point) {
if (!point) {
flag = false;
alert("Entered zipcode '"+address
+"' can not be found. Please
check the zipcode!");
hideProgressStatus();
hideButtonDisplay();
document.getElementById("address").focus();
}else{
alert("Entered Zip Code is correct.");
document.getElementById("calculateDistance").style.display = "";
}
});
}
}
}
/**
* This function get invoked onchange event of input field 'Zip Code'
and hide the
* button 'Check Closest Service Location'.
*/
function hideButtonDisplay(){
document.getElementById("calculateDistance").style.display = "none";
}
</script>
</head>
<body onload="globalFunction()" onunload="GUnload()" >
<form id="sampleForm">
<br></br>
<table border="0" width="85%">
<tr>
<td align="center" colspan="2">
<h1>Automate Zip Code Validation<h1>
</td>
</tr>
<tr>
<td align="center" colspan="2">
<span id="showScreenSplashNew"
style="color:gray;font-
size:large;display:none;">
<P>Please wait while your request is
being processed...</P>
</span>
</td>
</tr>
<tr>
<td colspan="0" align="center">
Zip Code <input type="text" size="10"
name="address"
id="address" value=""
onclick="hideButtonDisplay()"/>
</td>
</tr>
<tr><td> </td></tr>
<tr>
<td align="center" colspan="2">
<input type="button" value="Validate Zip Code"
id="validateZipCodeNew"
onclick="validateZipCode();"/>
</td>
</tr>
<tr><td> </td></tr>
<tr>
<td align="center" colspan="2">
<input type="button" value="Check Closest Service
Location"
id="calculateDistance"
onclick="setCounter();" style="display:
none"/>
</td>
</tr>
</table>
<div id="map" style="width: 600px; height: 400px; display: none;">
</div>
</form>
</body>
</html>
/-------------------------------------Code End-----------------------/
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Google Maps API" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/Google-Maps-API?hl=en
-~----------~----~----~----~------~----~------~--~---