Last active
November 16, 2016 21:13
-
-
Save baquiax/732d593e8fc8fa48bd25f9a4fdc3e901 to your computer and use it in GitHub Desktop.
Using Google Maps to calculate distances
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
header("Content-type: text/csv"); | |
header('Content-disposition: attachment;filename="matrix.csv"'); | |
$matrix = array( ); | |
$titles = array(); | |
$allPositions = array("14.597518,-90.514900", | |
"14.599453,-90.515419", | |
"14.597998,-90.512912", | |
"14.597389,-90.510218", | |
"14.599377,-90.513408", | |
"14.597791,-90.512088", | |
"14.579766,-90.495399", | |
"14.592630,-90.511397", | |
"14.609474,-90.505654", | |
"14.599025,-90.515651", | |
"14.587368,-90.513884", | |
"14.582139,-90.515367", | |
"14.576909,-90.523670", | |
"14.561100,-90.472302", | |
"14.580478,-90.492893", | |
"14.609463,-90.486374", | |
"14.597262,-90.496923", | |
"14.647721,-90.481787", | |
"14.622750,-90.552212", | |
"14.618189,-90.555172", | |
"14.620369,-90.561692", | |
"14.592493,-90.564815", | |
"14.611207,-90.597559", | |
"14.597130,-90.579336", | |
"14.602201,-90.580975", | |
"14.650728,-90.539538", | |
"14.592146,-90.493894", | |
"14.552698,-90.452897", | |
"14.635150,-90.581292", | |
"14.635569,-90.582361", | |
"14.562676,-90.464594", | |
"14.639643,-90.565264", | |
"14.624663,-90.470468", | |
"14.623395,-90.550826", | |
"14.582227,-90.546246", | |
"14.621855,-90.503155", | |
"14.643225,-90.497081", | |
"14.598870,-90.543208", | |
"14.583031,-90.566706", | |
"14.574058,-90.542411", | |
"14.289215,-90.785595"); | |
/*$allPositions = array("14.597518,-90.514900", | |
"14.599453,-90.515419", | |
"14.597998,-90.512912", | |
"14.597389,-90.510218");*/ | |
$googleMapsUrl = "https://maps.googleapis.com/maps/api/distancematrix/json?destinations=<ORIGIN>&origins=<DESTINATION>&language=es-GT&mode=driving&key=AIzaSyD6LnWTDEyiF0HRCmHaukDsMCJjnn8akDI"; | |
$rowsToAnalyze = count($allPositions); | |
$lastRowAnalized = 0; | |
$globalRowLenght = 2; | |
$globalColumnLenght = 25; | |
while ($rowsToAnalyze > 0) { | |
$rows = array_slice($allPositions, $lastRowAnalized, $globalRowLenght); | |
$origins = implode("|", $rows); | |
$columnsToAnalyze = count($allPositions); | |
$lastColumnAnalized = 0; | |
while ($columnsToAnalyze > 0) { | |
$columns = array_slice($allPositions, $lastColumnAnalized, $globalColumnLenght); | |
$destinations = implode("|", $columns); | |
$finalUrl = str_replace("<ORIGIN>", $origins, $googleMapsUrl); | |
$finalUrl = str_replace("<DESTINATION>", $destinations, $finalUrl); | |
//echo $finalUrl; | |
$jsonText = file_get_contents($finalUrl); | |
$jsonObject = json_decode($jsonText, true); | |
if (!isset($jsonObject)) { | |
var_dump($jsonText); | |
continue; | |
} | |
if ($jsonObject["status"] != "OK") { | |
var_dump($jsonText); | |
continue; | |
} | |
for ($r = 0; $r < count($jsonObject["rows"]) ; $r++) { | |
$title = $allPositions[$lastColumnAnalized + $r]; | |
$row = array(); | |
if ($matrix[$title] != null) { | |
$row = $matrix[$title]; | |
} | |
$elements = $jsonObject["rows"][$r]["elements"]; | |
for ($c = 0; $c < count($elements); $c++) { | |
array_push($row, $elements[$c]["distance"]["value"]); | |
} | |
$matrix[$title] = $row; | |
} | |
$lastColumnAnalized += $globalColumnLenght; | |
$columnsToAnalyze -= $globalColumnLenght; | |
} | |
$lastRowAnalized += $globalRowLenght; | |
$rowsToAnalyze -= $globalRowLenght; | |
} | |
foreach ($matrix as $key => $value) { | |
echo "\"".$key."\",". implode(",", $value)."\n"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment