如何根据存储在数据库中的地理点找到总行进距离
how to find the total distance traveled, based on geo points stored in database
我正在使用 android 应用程序将纬度和经度值发送到数据库。
这是我的数据库
现在在网页中我需要显示 K.Prathibha.N
的总行驶距离
我已经使用 Google Maps Direction API 来计算距离,但它接受 'id=2' 作为起点和 'id=5' 作为终点。
但我的座右铭是找到总行进距离,即 id_2 到 id_3 到 id_4 到 id_5。
我在这里使用 while 循环。
代码片段:
<?php
$params_string="";
$pname = $_POST['name']; //name is passed from a dropdown
$query = "SELECT * FROM information WHERE mobile=? ORDER BY id DESC ";
$result = $dbConnection->prepare($query);
$result->execute(array($pname));
if($result->rowCount() >0)
{
$params = array();
while($row = $result->fetch())
{
$mobile = $row['mobile'];
$latitude = $row['latitude'];
$cname = $row['name'];
$longitude = $row['longitude'];
$lat = "17.4136846";
$long = "78.49228289999996";
$params = array("origin" => $latitude.",".$longitude,"destination" => $lat.",".$long,"sensor" => 'true',"units" => 'imperial');
//Join parameters into URL string
foreach($params as $var => $val)
{
$params_string .= '&' . $var . '=' . urlencode($val);
}
}
}
// Request URL
$url = "http://maps.googleapis.com/maps/api/directions/json?".ltrim($params_string, '&');
// Make our API request
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$return = curl_exec($curl);
curl_close($curl);
// Parse the JSON response
$directions = json_decode($return);
// Show the total distance
print('<p><strong>Total distance:</strong> ' . ceil(($directions->routes[0]->legs[0]->distance->text)* 1.609344) .' KM'. '</p>');
?>
现在,如何在此处传递目标值。
我的任务是将数据库值传递给变量 lat 和 long....例如,我已经传递了手动值..如何同时获取数据库值,以便 origin 获取 id 2 和 destination 获取 id 3 值
示例-如果接受
起点为 id-2 那么终点应该是 id-3 下一个
起点为 id-3 终点为 id-4 依此类推
在最后根据可用的地理位置给出该特定日期的总行驶距离。
3 天多以来一直在为此苦苦挣扎。
任何类型的建议都会有很大帮助。
您可以在 while 循环中连续调用此函数
$latitude = $row['latitude'];
$longitude = $row['longitude'];
$lat = "17.4136846";
$long = "78.49228289999996";
$distance=distance($latitude,$longitude,$lat,$long,"k");
and the function is
function distance($lat1, $lon1, $lat2, $lon2, $unit)
{
$theta = $lon1 - $lon2;
$dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));
$dist = acos($dist);
$dist = rad2deg($dist);
$miles = $dist * 60 * 1.1515;
$unit = strtoupper($unit);
if ($unit == "K") {
return ($miles * 1.60934);
} else if ($unit == "N") {
return ($miles * 0.8684);
} else {
return $miles;
}
}
//remember you pass last parameter in function k. so it you you the //distance in kilometer
Well, You can do like this.
例如
$qry = mysql_query("select * from store");
$num = mysql_num_rows($qry);
while($row = mysql_fetch_array($qry)){
$lat[] = $row['lat'];
$long[] =$long['long'];
}
for($i=0;$i<$num;$i++){
$distance=distance($lat[$i],$long[$i],$lat[$i+1],$long[$i+1],"k");
}
function:
function distance($lat1, $lon1, $lat2, $lon2, $unit)
{
$theta = $lon1 - $lon2;
$dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));
$dist = acos($dist);
$dist = rad2deg($dist);
$miles = $dist * 60 * 1.1515;
$unit = strtoupper($unit);
if ($unit == "K") {
return ($miles * 1.60934);
} else if ($unit == "N") {
return ($miles * 0.8684);
} else {
return $miles;
我正在使用 android 应用程序将纬度和经度值发送到数据库。
这是我的数据库
现在在网页中我需要显示 K.Prathibha.N
的总行驶距离我已经使用 Google Maps Direction API 来计算距离,但它接受 'id=2' 作为起点和 'id=5' 作为终点。 但我的座右铭是找到总行进距离,即 id_2 到 id_3 到 id_4 到 id_5。
我在这里使用 while 循环。
代码片段:
<?php
$params_string="";
$pname = $_POST['name']; //name is passed from a dropdown
$query = "SELECT * FROM information WHERE mobile=? ORDER BY id DESC ";
$result = $dbConnection->prepare($query);
$result->execute(array($pname));
if($result->rowCount() >0)
{
$params = array();
while($row = $result->fetch())
{
$mobile = $row['mobile'];
$latitude = $row['latitude'];
$cname = $row['name'];
$longitude = $row['longitude'];
$lat = "17.4136846";
$long = "78.49228289999996";
$params = array("origin" => $latitude.",".$longitude,"destination" => $lat.",".$long,"sensor" => 'true',"units" => 'imperial');
//Join parameters into URL string
foreach($params as $var => $val)
{
$params_string .= '&' . $var . '=' . urlencode($val);
}
}
}
// Request URL
$url = "http://maps.googleapis.com/maps/api/directions/json?".ltrim($params_string, '&');
// Make our API request
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$return = curl_exec($curl);
curl_close($curl);
// Parse the JSON response
$directions = json_decode($return);
// Show the total distance
print('<p><strong>Total distance:</strong> ' . ceil(($directions->routes[0]->legs[0]->distance->text)* 1.609344) .' KM'. '</p>');
?>
现在,如何在此处传递目标值。
我的任务是将数据库值传递给变量 lat 和 long....例如,我已经传递了手动值..如何同时获取数据库值,以便 origin 获取 id 2 和 destination 获取 id 3 值
示例-如果接受
起点为 id-2 那么终点应该是 id-3 下一个
起点为 id-3 终点为 id-4 依此类推
在最后根据可用的地理位置给出该特定日期的总行驶距离。
3 天多以来一直在为此苦苦挣扎。
任何类型的建议都会有很大帮助。
您可以在 while 循环中连续调用此函数
$latitude = $row['latitude'];
$longitude = $row['longitude'];
$lat = "17.4136846";
$long = "78.49228289999996";
$distance=distance($latitude,$longitude,$lat,$long,"k");
and the function is
function distance($lat1, $lon1, $lat2, $lon2, $unit)
{
$theta = $lon1 - $lon2;
$dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));
$dist = acos($dist);
$dist = rad2deg($dist);
$miles = $dist * 60 * 1.1515;
$unit = strtoupper($unit);
if ($unit == "K") {
return ($miles * 1.60934);
} else if ($unit == "N") {
return ($miles * 0.8684);
} else {
return $miles;
}
}
//remember you pass last parameter in function k. so it you you the //distance in kilometer
Well, You can do like this.
例如
$qry = mysql_query("select * from store");
$num = mysql_num_rows($qry);
while($row = mysql_fetch_array($qry)){
$lat[] = $row['lat'];
$long[] =$long['long'];
}
for($i=0;$i<$num;$i++){
$distance=distance($lat[$i],$long[$i],$lat[$i+1],$long[$i+1],"k");
}
function:
function distance($lat1, $lon1, $lat2, $lon2, $unit)
{
$theta = $lon1 - $lon2;
$dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));
$dist = acos($dist);
$dist = rad2deg($dist);
$miles = $dist * 60 * 1.1515;
$unit = strtoupper($unit);
if ($unit == "K") {
return ($miles * 1.60934);
} else if ($unit == "N") {
return ($miles * 0.8684);
} else {
return $miles;