确定并排序多个地点的距离

时间:2016-03-31 16:00:50

标签: php mysql google-maps

我在mysql表中有大约15个位置,包含lat和long信息。

使用PHP和谷歌地图API能够计算2个地点之间的距离。

function GetDrivingDistance($lat1, $lat2, $long1, $long2) 
{ 
$url = "https://maps.googleapis.com/maps/api/distancematrix/json?origins=".$lat1.",".$long1."&destinations=".$lat2.",".$long2."&mode=driving&language=en-US"; 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_PROXYPORT, 3128); 
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); 
$response = curl_exec($ch); 
curl_close($ch); 
$response_a = json_decode($response, true); 
$dist = $response_a['rows'][0]['elements'][0]['distance']['text']; 
$time = $response_a['rows'][0]['elements'][0]['duration']['text']; 
return array('distance' => $dist, 'time' => $time); 
} 

我想选择一个固定的,例如第1行给出lat和long

$query="SELECT lat, long from table WHERE location=1"
$locationStart = $conn->query($query); =

我想计算表格中所有其他位置的距离(其他行)并返回按距离排序的结果

尝试单独计算每一个并最终得到很长的代码并且花费太长时间来通过api获取它,也仍然无法以这种方式对它们进行排序!

任何提示?

1 个答案:

答案 0 :(得分:0)

免责声明:这不是一个有效的解决方案,我也没有对其进行测试,这只是一个简单的例子我已经完成了一些代码示例我的评论。

我的大脑仍然没有完全热身,但我相信底部至少应该作为一种指导,以帮助我在评论中提出我的想法,我将尝试回答你在我的任何问题有空。希望它有所帮助。

<?php 

define('MAXIMUM_REQUEST_STORE', 5); // Store 5 requests in each multi_curl_handle

function getCurlInstance($url) {
    $handle = curl_init();
    curl_setopt($handle, CURLOPT_URL, $url);
    curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);

    return $handle;
}

$data = []; // Build up an array of Endpoints you want to hit. I'll let you do that.

// Initialise Variables
$totalRequests         = count($data);
$parallelCurlRequests  = [];
$handlerID             = 0;

// Set up our first handler
$parallelCurlRequests[$handlerID] = curl_multi_init();

// Loop through each of our curl handles
for ($i = 0; $i < $totalRequests; ++$i) {

    // We want to create a new handler/store every 5 requests. -- Goes off the constant MAXIMUM_REQUEST_STORE
    if ($i % MAXIMUM_REQUEST_STORE == 1 && $i > MAXIMUM_REQUEST_STORE) {
        ++$handlerID;
    }

    // Create a Curl Handle for the current endpoint
    // ... and store the it in an array for later use.
    $curl[$i] = getCurlInstance($data[$i]);

    // Add the Curl Handle to the Multi-Curl-Handle
    curl_multi_add_handle($parallelCurlRequests[$handlerID], $curl[$i]);
}

// Run each Curl-Multi-Handler in turn
foreach ($parallelCurlRequests as $request) {
    $running = null;
    do {
        curl_multi_exec($request, $running);
    } while ($running);
}

$distanceArray = [];

// You can now pull out the data from the request.
foreach ($curl as $response) {
    $content = curl_multi_getcontent($response);
    if (!empty($content)) {
        // Build up some form of array.
        $response = json_decode($content);
        $location = $content->someObject[0]->someRow->location;
        $distance = $content->someObject[0]->someRow->distance;

        $distanceArray[$location] = $distance;
    }
}

natsort($distanceArray);