2016-03-31 8 views
0

私は緯度と経度の情報があるmysqlテーブルに約15の場所を持っています。複数の場所の距離を決定してソートする

PHPとGoogleマップ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); 
} 

固定であるものを選択したいとします。

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

所与の行1私は距離

によってソート結果がそれぞれ単独一方と端を計算しようとしたテーブル内のすべての他の場所(他の行)までの距離を計算して返すようにしたいです非常に長いコードでアップし、api経由でそれをフェッチするのに時間がかかりすぎて、まだこのように並べ替えることができません!

ヒント?

+0

[ 'curl_multi_init'](http://php.net/manual/en/function.curl-multi-init.php)要求を処理する方法は、それが*「複数の処理を可能かもしれませんcURLは非同期に処理されます。 "*あなたのcURL要求をスピードアップするための1つの方法です。すべての 'cURL'コードを減らしたければ、[' file_get_contents() '](http://php.net/manual/en/function.file-get-contents.php)を実行することもできます。並べ替えに関しては、距離の数値を取り出して配列に格納しておけば、 'natsort()'のような組み込みソート関数を使うことができると思います。 – Mikey

+0

こんにちはピジョンこれ以上の詳細やコードサンプルをテストしたいのですか?私はPHPで非常に良いを持っていない – Mike

+0

私はいくつかの荒い作業で、私が考えていた大まかなアイデアを追加しました、任意の質問は私に知らせてください。 – Mikey

答えて

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); 
+0

ありがとうございましたpigeon – Mike

関連する問題