gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 21:11:22 26 4
gpt4 key购买 nike

我的 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com