This function i m working on is to calculate the shortest distance between the delivery man and the destination by using GPS coordinates. After clicking a button, named as btnShortestDist, it will retrieve all delivery man details on a page. I can't create that many sql statement to call all delivery men's details out. Thus, I used DataList and databind it with the database on sql server.
The problem is, for each delivery man, I need to calculate the shortest distance. I dragged a label to the DataList's item template. This label, named as DistanceLabel, should use the distance calculation method in the c# page and give an output of the calculated distance. How do I do this for all the delivery men? Is it possible?
My C# distance calculation method:
public static double CalculateDistance(double lon1, double lat1, double lon2, double lat2) {
const double R = 3956;
const double degreesToRadians = Math.PI / 180;
//convert from fractional degrees (GPS) to radians
lon1 *= degreesToRadians;
lat1 *= degreesToRadians;
lon2 *= degreesToRadians;
lat2 *= degreesToRadians;
double dlon = lon2 - lon1;
double dlat = lat2 - lat1;
double a = Math.Pow(Math.Sin(dlat / 2), 2) + Math.Cos(lat1) * Math.Cos(lat2) * Math.Pow(Math.Sin(dlon / 2), 2);
double c = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a));
double d = R * c;
return d;
}
The DataList code that i did for now:
DataSourceID="SqlDataSource1" RepeatColumns="3"
RepeatDirection="Horizontal">
StaffID:
StaffName:
Distance:
ConnectionString="<%$ ConnectionStrings:VCdeliveryconnectionString %>"
SelectCommand="SELECT [StaffID], [StaffName] FROM [Staff] WHERE ([Role] = @Role)">
1 Reply
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
CrishPosted May 20, 2010, 2:22 AM
you can do it with DataList ItemDatabound event.
protected void ProductDataList_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.SelectedItem)
{
Label lblOff = (Label)e.Item.FindControl("lblOfferprod");
CalculateDistance(double lon1, double lat1, double lon2, double lat2) ;
}
}
thanks