I got a list if addresses showed in a list and one search filter containing textbox to insert Zip code.
Now I need to calculate the distance between the filtering Zip code and each address showing in the list. To do this I used google api.
---->
jQuery(document).ready(function () {
if(jQuery('.atm-zip').children().val().length>0)
{
//get the filtering zip value and calculate the distance
getLatLngFromAddress(jQuery('.atm-zip').children().val());
}
});
var geocoder;
//filtering zip's lat
var p1lt;
//filtering zip's long
var p1ln;
function getLatLngFromAddress(postal_code) {
if (geocoder == null) {
geocoder = new google.maps.Geocoder();
}
var searchLoc;
geocoder.geocode({ 'address': postal_code }, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
searchLoc = results[0].geometry.location;
p1lt=searchLoc.lat();
p1ln=searchLoc.lng();
}
jQuery('.atm_list li').each(function(){
//listed address's lat
var p2lt=jQuery(this).find('.GeoLat').text();
//listed address's long
var p2ln=jQuery(this).find('.GeoLong').text();
jQuery(this).find('.GeoDistance').text(getDistance(p2lt,p2ln) +' mi');
});
});
}
var rad = function(x) {
return x * Math.PI / 180;
};
//this is a function to calculate distance between 2 geographic points
function getDistance(p2lt,p2ln) {
var R = 6378137; // Earth’s mean radius in meter
var dLat = rad(p2lt - p1lt);
var dLong = rad(p2ln - p1ln);
var a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
Math.cos(rad(p1lt)) * Math.cos(rad(p2lt)) *
Math.sin(dLong / 2) * Math.sin(dLong / 2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
var d = R * c;
var resultInMile=d*0.000621371;
var finalResult=parseFloat(Math.round(resultInMile * 100) / 100).toFixed(2);
return finalResult; // returns the distance in meter then converted to miles
}
Now I need to calculate the distance between the filtering Zip code and each address showing in the list. To do this I used google api.
---->
jQuery(document).ready(function () {
if(jQuery('.atm-zip').children().val().length>0)
{
//get the filtering zip value and calculate the distance
getLatLngFromAddress(jQuery('.atm-zip').children().val());
}
});
var geocoder;
//filtering zip's lat
var p1lt;
//filtering zip's long
var p1ln;
function getLatLngFromAddress(postal_code) {
if (geocoder == null) {
geocoder = new google.maps.Geocoder();
}
var searchLoc;
geocoder.geocode({ 'address': postal_code }, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
searchLoc = results[0].geometry.location;
p1lt=searchLoc.lat();
p1ln=searchLoc.lng();
}
jQuery('.atm_list li').each(function(){
//listed address's lat
var p2lt=jQuery(this).find('.GeoLat').text();
//listed address's long
var p2ln=jQuery(this).find('.GeoLong').text();
jQuery(this).find('.GeoDistance').text(getDistance(p2lt,p2ln) +' mi');
});
});
}
var rad = function(x) {
return x * Math.PI / 180;
};
//this is a function to calculate distance between 2 geographic points
function getDistance(p2lt,p2ln) {
var R = 6378137; // Earth’s mean radius in meter
var dLat = rad(p2lt - p1lt);
var dLong = rad(p2ln - p1ln);
var a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
Math.cos(rad(p1lt)) * Math.cos(rad(p2lt)) *
Math.sin(dLong / 2) * Math.sin(dLong / 2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
var d = R * c;
var resultInMile=d*0.000621371;
var finalResult=parseFloat(Math.round(resultInMile * 100) / 100).toFixed(2);
return finalResult; // returns the distance in meter then converted to miles
}
No comments:
Post a Comment