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

标签 php mysql google-maps

我的 mysql 表中有大约 15 个位置,其中包含纬度和经度信息。

使用 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 行给定纬度和经度

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

关于php - 确定并排序多个位置的距离,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36338327/

相关文章:

php - 数组 PHP 中 undefined offset (在 for 迭代中)

javascript - 在嵌入式 map /谷歌地图 API 上使用标准谷歌地图接口(interface)

带有匿名连接的 PHP mysqli 准备语句

php - 在 mySQL 中使用 LIKE 匹配任何内容

php - 什么是处理 PHP 应用程序到 SQL 数据库的良好包装器/框架/库?

Zend 中的 PHP 数据对象

mysql - 如何在mysql中对GROUP_CONCAT应用LIKE查询?

PHP/FTP客户端

google-maps - 谷歌地图javascript api中的圆半径单位?

javascript - 谷歌地图多标记不工作