如何根据数据库中存储的地理位置查找总行程 [英] how to find the total distance traveled, based on geo points stored in database

查看:117
本文介绍了如何根据数据库中存储的地理位置查找总行程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用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是从下拉菜单中传递的
$ 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');

//将参数加入URL字符串
foreach($ params as $ var => $ val)
{
$ params_string。='&'。 $ var。 '='。进行urlencode($ VAL);
}

}
}

//请求URL
$ url =http://maps.googleapis.com/maps /api/directions/json?\".ltrim($params_string,'&');

//创建我们的API请求
$ curl = curl_init();
curl_setopt($ curl,CURLOPT_URL,$ url);
curl_setopt($ curl,CURLOPT_RETURNTRANSFER,1);
$ return = curl_exec($ curl);
curl_close($ curl);

//解析JSON响应
$ directions = json_decode($ return);

//显示总距离

print('< p>< strong>总距离:< / strong>'ceil(($ directions-> ;路线[0] - >腿[0] - >距离 - >文字)* 1.609344)。'KM'。'< / p>');

?>

现在,如何在这里传递目标值。



我的任务是将数据库值传递给变量lat和long ....例如,我已经通过手动值..如何同时获取数据库值,以便原点获得id 2并且目标获得id 3值



示例 - 如果它接受

origin作为id-2,则目标应该是id-3 next p>

起源为id-3,然后目的地应该是id-4,依此类推。



在这一天,根据可用的地理位置点。



从此挣扎3天以上。

任何类型的建议都会对你有很大的帮助。

解决方案


嗯,你可以这样做。


例如

  $ qry = mysql_query(select * from store); 
$ num = mysql_num_rows($ qry);
while($ row = mysql_fetch_array($ qry)){
$ lat [] = $ row ['lat'];
$ long [] = $ long ['long']; ($ i = 0; $ i <$ num; $ i ++){
$距离=距离($ lat [$ i],$ long [$ i],

) LAT [$ I + 1],$长[$ I + 1]中, k);
}

函数:

函数距离($ lat1,$ lon1,$ lat2,$ lon2,$单位)
{

$ 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;


Using an android app I am sending latitude and longitude values to database.
This is my database

Now in webpage I need to show the total distance traveled by K.Prathibha.N

I've used Google Maps Direction API to find the distance but it is accepting 'id=2' as origin and 'id=5' as destination. But my motto is to find the total distance travelled i.e., id_2 to id_3 to id_4 to id_5.

I am using while loop here.

Code snippet:

<?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>');

    ?>

Now,How to pass the destination values here.

My task is to pass database values to variables lat and long....Just for example i've passed manual values..how to fetch database values simultaneously so as origin gets id 2 and destination gets id 3 values

Example-If it is accepting

origin as id-2 then destination should be id-3 next

origin as id-3 then destination should be id-4 and so on

at the end give total distance travelled on that particular day based on the available geo points.

Struggling with this from more than 3 days.

Any kind of suggestions would be of great help.

解决方案

Well, You can do like this.

For eg.

  $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;

这篇关于如何根据数据库中存储的地理位置查找总行程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆