I have the following script, designed to pass a variable from a Google Maps API v3 geocoded latitude and longitude to a haversine formula. However, each time it says that my geocoded vars "aren't defined":
var address = ['London'];
jQuery.each(address, function(index, item) {
geocoder = new google.maps.Geocoder();
geocoder.geocode( { 'address': item}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
startlat = results[0].geometry.location.lat();
startlng = results[0].geometry.location.lng();
}
});
});
// start of haversine for marker distances
Number.prototype.toRad = function() {
return this * Math.PI / 180;
};
var mlat = '50.1';
var mlng = '-1.05';
var RDis = '3963'; // miles; Change to 6371 for km
var x1 = mlat - startlat;
var dLat = x1.toRad();
var x2 = mlng - startlng;
var dLon = x2.toRad();
var aDis = Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.cos(startlat.toRad()) * Math.cos(mlat.toRad()) *
Math.sin(dLon/2) * Math.sin(dLon/2);
var cDis = 2 * Math.atan2(Math.sqrt(aDis), Math.sqrt(1-aDis));
var dDis = RDis * cDis;
// dDis is the distance
Can anyone advise on how I can pass these through the script? Wrapping up the geocoding into the same function as the haversine isn't an option (I need to use each of them elsewhere, too).
Thanks for any help. :)