Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Extracting Your Own Location Information From Google
// Today's time is needed to get the bounding for the request
$currentTime = time();
// Beginning and end of the day
$startTime = strtotime("midnight", $currentTime);
$endTime = strtotime("tomorrow", $startTime);
$smon = date("m", $startTime)-1;
$sday = date("d", $startTime);
$syear = date("Y", $startTime);
$emon = date("m", $endTime)-1;
$eday = date("d", $endTime);
$eyear = date("Y", $endTime);
//The Google Location history URL - gets the KML.
$locationURL = "!1m8!1m3!1i".$syear."!2i".$smon."!3i".$sday."!2m3!1i".$eyear."!2i".$emon."!3i".$eday;
//To request the file, using PHP, we need to tell curl where the cookies are.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $locationURL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookies.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookies.txt');
$kml = curl_exec($ch);
//get last gx:coord
$startIndex = strripos($kml,"<gx:coord>") + strlen("<gx:coord>");
$endIndex = strripos($kml,"</gx:coord>");
$substr = substr($kml, $startIndex, $endIndex - $startIndex);
$latlngArr = explode(" ",$substr);
$lng = $latlngArr[0];
$lat = $latlngArr[1];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment