插入N city并获取值距离起点到目的地点

时间:2015-12-10 12:42:18

标签: php google-maps-api-3

你好,任何人都可以给我意见 我有一些麻烦的逻辑来获得城市间的距离

示例:我给1个参数(参数从txt命名为城市)

如果我插入像

这样的城市

'巴厘岛 - 雅加达 - Jogja - 三宝垄 - Solo'

我可以计算距巴厘岛+雅加达+ Jogja +三宝垄+独奏的距离

实施例

  1. 巴厘岛 - >雅加达= 480公里

  2. 雅加达 - > Jogja = 1202 KM

  3. Jogja - >三宝垄= 432公里

  4. 三宝垄 - > Solo = 202 KM

  5. 所以我希望返回值是4个城市的总和,并且显示起点到目的地点是距离:2316 KM

    我从json google矩阵json获得距离

    到目前为止,我的代码是:

        public function addJarak1($kota) 
        {
           $kota1=$kota;
           $kota2=$kota;
           foreach ($kota1 as $key1 => $value1)
           {
               foreach ($kota2 as $key2 => $value2)
               {
    
                $berangkat = urlencode($value1);
                $tujuankota = urlencode($value2);
    
                $data = file_get_contents("http://maps.googleapis.com/maps/api/distancematrix/json?origins=$berangkat&destinations=$tujuankota&language=id&units=metric&sensor=true");
                $data = json_decode($data);
    
             //       echo "<pre>". print_r($data ,true) ."</pre>";
                $jarak = 0;
    
                foreach ($data->rows[0]->elements as $road) 
                {
                    $jarak += $road->distance->text;
                }
                $this->simpanjarak1=$jarak;
                //echo "Berangkat: " . $data->origin_addresses[0]+"<br \>Tujuan : " . $data->destination_addresses[0]+"<br \>Jarak: " . $jarak . " KM<br \><br \>";
                return $this->simpanjarak1;
    
            }
           }
        }
    
    
    
    
        public function hitungKM ($kota)
        {
            $this->addJarak1($kota);
            $jarak = $this->simpanjarak1;
                if ($jarak != 0 )
                {
                    {
                        $jarak ;
                    } 
                    if($jarak == 1)
                    {
                        $jarak = 0;
                    } 
                }
                else
                {
                    $jarak=0;
                }
                $x= $jarak;
                $x += $jarak;
    
                echo $jarak;
                return $jarak;
    
        }
    
    
        $kota= Bali + Jakarta + Jogja + Semarang + Solo
        $algo->hitungKM($kota);
    

    所以如果返回值是2316 KM

    你能不能给我一些想法? 谢谢你 抱歉英语不好:)

1 个答案:

答案 0 :(得分:0)

也许这会有所帮助:

print calculate_distance_between_two_uk_cities('birmingham', 'sheffield');

function calculate_distance_between_two_uk_cities($origin, $destination)
{
    $google_matrix_url = 'http://maps.googleapis.com/maps/api/distancematrix/json?origins='
        . urlencode($origin) . ',UK'
        . '&destinations='
        . urlencode($destination) . ',UK'
        . '&language=en&units=imperial';

    $json = file_get_contents($google_matrix_url);

    var_dump($json);

    if($json === false) {
        throw new Exception('Failed to retrieve response.');
    }
    $result = json_decode($json, TRUE);
    if($result === null) {
        throw new Exception('Could not decode JSON response.');
    }
    if(! isset($result['rows'][0]['elements'][0]['distance']['text'])) {
        throw new Exception('Could not retrieve distance from remote service.');
    }
    $distance = $result['rows'][0]['elements'][0]['distance']['text'];
    $distance = intval($distance);

    return $distance;
}

我在原点和目的地添加了英国后缀,否则就像&#39;伯明翰&#39;失败。