确定并排序多个位置的距离


determine and sort distance for several locations

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

使用PHP和谷歌地图API我可以计算两个位置之间的距离。

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行

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

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

试图单独计算每一个,结果得到了很长的代码,通过api获取代码需要很长时间,但仍然无法以这种方式排序!

有什么提示吗?

免责声明:这不是一个有效的解决方案,我也没有测试过它,它只是我脑海中的一个快速示例,为我的评论提供了一种代码示例。

我的大脑还没有完全预热,但我相信底部至少应该起到一种引导作用,帮助我理解我在评论中提出的想法,我会在空闲时回答你的任何问题。希望能有所帮助。

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