Reputation: 3
So I am trying to build a script that calculates the distances between a given zipcode and and a list of other zipcodes, check which is the smallest. I have been using document.write("") as a debugging tool to check the values of things. The problem is that no variables seem to retain their values after they leave the scope of the callback functions. For example if I call document.write for the miledistance variable withing the geocoders callback function it will give me a correct answer. This will even be true if call document.write in the fillarray function that stores the distance values in the distances array. However if I check the value of an array some outside the call back function, like at the end of the showLocations function then the array will be displayed as undefined.
This is my first stackoverflow question so I apologize in advance for any faux pas in answering this question.
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
<title>Google Maps JavaScript API Example: Extraction of Geocoding Data</title>
<script src="http://maps.google.com/maps?file=api&v=2&key=AIzaSyC8BdiVC-BCDF6zFhzR47dRc7gAryotnM0" type="text/javascript"></script>
<!-- According to the Google Maps API Terms of Service you are required display a Google map when using the Google Maps API. see: http://code.google.com/apis/maps/terms.html -->
<script type="text/javascript">
<cfoutput query = "getAddress">
var #toScript(zip,"zip")#;
</cfoutput>// Some coldfusion that creates the single zipcode from somewhere else
var geocoder,
function initialize() {
geocoder = new GClientGeocoder();
}
var miledistance;
var distances = new Array();
function showLocation() {
var chapters = new Array();
chapters[0] = "62650";
chapters[1] = "44323";
chapters[2] = "55555";
chapters[3] = "23624";
chapters[4] = "86753";
var closest = "10,000.0";
geocoder.getLatLng(zip,function(point){
for(var i= 0;i<5;i++){
if(point !==null){
var res= point;
geocoder.getLatLng(chapters[i],function(point){
miledistance = point.distanceFrom(res,3959);
fillarray(i, miledistance);
});
}else{
document.write("Address not Processed");
}
}
});
document.write(distance[0]);
}
function fillarray(i, distance){
distances[i] = distance;
}
function calculateDistance()
{
try
{
var glatlng1 = new GLatLng(location1.lat, location1.lon);
var glatlng2 = new GLatLng(location2.lat, location2.lon);
var miledistance = glatlng1.distanceFrom(glatlng2, 3959).toFixed(1);
var kmdistance = (miledistance * 1.609344).toFixed(1);
document.getElementById('results').innerHTML = '<strong>Address 1: </strong>' + location1.address + '<br /><strong>Address 2: </strong>' + location2.address + '<br /><strong>Distance: </strong>' + miledistance + ' miles (or ' + kmdistance + ' kilometers)';
return miledistance;
}
catch (error)
{
alert(error);
}
}
</script>
</head>
Upvotes: 0
Views: 509
Reputation: 1789
What about that?
http://maps.googleapis.com/maps/api/distancematrix/xml?origins=NEW-YORK-10001&destinations=WASHINGTON+20001&sensor=false
Result:
<DistanceMatrixResponse>
<status>OK</status>
<origin_address>New York, NY 10001, USA</origin_address>
<destination_address>Washington, DC 20001, USA</destination_address>
<row>
<element>
<status>OK</status>
<duration>
<value>13082</value>
<text>3 hours 38 mins</text>
</duration>
<distance>
<value>364365</value>
<text>364 km</text>
</distance>
</element>
</row>
</DistanceMatrixResponse>
Source: How to get distance by city + zipCode on google maps API
Upvotes: 1