Skip to content

Instantly share code, notes, and snippets.

Embed
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);
//pb=!1m8!1m3!1iYYYY!2iMM!3iDD!2m3!1iYYYY!2iMM!3iDD
//The Google Location history URL - gets the KML.
$locationURL = "https://www.google.com/maps/timeline/kml?authuser=0&pb=!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);
curl_close($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
You can’t perform that action at this time.