great code! I have a small question if you don't mind, how would you use this if you wanted to calculate distance of multiple points. Example: if i want to calculate distance travelled for the past hour, I will have a lot of co-ordinate points. How can I run this function over a bunch of points?
This is how I have achieved it, please change the mysql query as per your need, but I think the code below should give you a fair idea of how you can calculate distance between multiple points.
I have not added comment's due to lack of time.
$j=0;
$sql=mysql_query("SELECT * FROM position WHERE deviceId='xxx' AND fixTime>='xxx' AND fixTime <='xxxx' AND speed >'1' ORDER BY id ASC ");
while($row=mysql_fetch_array($sql))
{
if($j==0)
{
$lat1=$row1['latitude'];
$lon1=$row1['longitude'];
}
if($j>=1)
{
$lat2=$row1['latitude'];
$lon2=$row1['longitude'];
$obj=new test();
$distance=$obj->GetDistance($lat1,$lon1,$lat2,$lon2);
if($distance>180) $distance=0;
$TotalDis+=$distance;
$lat1=$lat2;
$lon1=$lon2;
}
$j++;
}
if($TotalDis>1000)
$countDis = number_format(round($TotalDis/1000,1),1)." km"; else
$countDis = number_format(round($TotalDis,3),3)." m";
Excuse me.
Rewritten in C #, the value differs from the value in the report :-(
Help please, that all is not so.
Those. in report 17.91 km, and I get in C# 18.2km, but it is provided if you remove the condition:
/*if( distance > 180 ) {
distance = 0;
}else{*/
maxDistance += distance;
//}
If you leave it turns out: 8km
Please help me!!!
There must be some difference in calculation algorithm.
My SQL query:
String query = "SELECT * FROM `positions` WHERE `device id` =" + device_id + "AND `time` > = ' "+ date_starts +"' AND `time` <= '" + date_stop + "' AND speed > 0 ORDER BY `time` ASC";
I'm in stupor... :-(
It's a really bad idea to use string concatenation for SQL queries.
Yes, I know.
In this case, I'm just testing a work request.
Thank you! :-)
C# code getDistance:
...........
String getDistance(String device_id, String date_starts, String date_stop){ // Возвращает пройденное расстояние определенного автомобиля в км или м за промежуток времени / Returns the distance traveled a specific vehicle in kilometers or meters in a time interval
if(date_starts != "" && date_stop != ""){
_db = DBInstance.Instance;
const String query = "SELECT * FROM <code>positions</code> WHERE <code>device_id</code> = @Device_id AND <code>time</code> >= @Date_starts AND <code>time</code> <= @Date_stop AND speed > 0 ORDER BY <code>time</code> ASC";
_db.Open();
MySqlCommand command = new MySqlCommand();
command.Connection = _db;
command.CommandText = query;
command.Prepare();
command.Parameters.AddWithValue("@Device_id", device_id);
command.Parameters.AddWithValue("@Date_starts", date_starts);
command.Parameters.AddWithValue("@Date_stop", date_stop);
MySqlDataReader reader = command.ExecuteReader();
int i = 0;
double maxDistance = 0;
double distance = 0;
float lat1 = 0, lon1 = 0, lat2, lon2;
while(reader.Read()){
if(i == 0){
lat1 = float.Parse(reader.GetString(4));
lon1 = float.Parse(reader.GetString(5));
}
if(i >= 1){
lat2 = float.Parse(reader.GetString(4));
lon2 = float.Parse(reader.GetString(5));
distance = CalcDistance.getDistance(lat1, lon1, lat2, lon2);
/*if( distance > 180 ) {
distance = 0;
}else{*/
maxDistance += distance;
//}
lat1 = lat2;
lon1 = lon2;
}
i++;
}
if(maxDistance > 1000){
MessageBox.Show((Math.Round(maxDistance / 1000, 1) + " Km"));
}else{
MessageBox.Show((Math.Round(maxDistance, 3)+" m"));
}
_db.Close();
return maxDistance.ToString();
}else{
return "ERROR. NOT ";
}
}
......
public class CalcDistance
{
.......
const double EARTH_RADIUS = 6378.137;
public static double getDistance(float lat1, float lng1, float lat2, float lng2) {
double radLat1 = lat1 * Math.PI / 180.0;
double radLat2 = lat2 * Math.PI / 180.0;
double a = radLat1 - radLat2;
double b = (lng1 * Math.PI / 180.0) - (lng2 * Math.PI / 180.0);
double s = 2 * Math.Asin(Math.Sqrt(Math.Pow(Math.Sin(a / 2), 2) + Math.Cos(radLat1) * Math.Cos(radLat2) * Math.Pow(Math.Sin(b / 2), 2)));
s = s * EARTH_RADIUS; // EARTH_RADIUS;
s = Math.Round(s * 1000,3);
return s;
}
}
...........
Just try to calculate distance manually and compare with your result.
Dear All,
Sharing a function I found handy in calculating distance between two co-ordiantes in PHP.
usage example
returned value will be in meters.