Created
May 5, 2013 15:07
-
-
Save jiminald/5521060 to your computer and use it in GitHub Desktop.
Track an individual using Google Latitude, depending on times and days
This file contains hidden or 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 | |
| /** | |
| * This lets you track an individual using Google Latitude depending on the hour of the day, days in a week and months. | |
| * | |
| * How to get your Google ID | |
| * ========================= | |
| * Go to : https://latitude.google.com/latitude/b/0/apps | |
| * Click "Enable and show best available location" and Save | |
| * In the text area, copy the ID from the URL, for example: | |
| * http://latitude.google.com/latitude/apps/badge/api?user=<GOOGLE ID>&type=iframe&maptype=roadmap&hl=en-GB | |
| * and paste it in $google_id. | |
| * | |
| */ | |
| $refresh_interval = 10; | |
| $allowed_months = array('01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12'); | |
| $allowed_days = array('1', '2', '3', '4', '5'); | |
| $allowed_hours = array('8', '9', '17', '18'); | |
| $google_id = ''; | |
| if ($_GET['retrieve'] == 'true') { | |
| $month = date('m'); | |
| $day = date('N'); | |
| $hour = date('H'); | |
| if ((in_array($month, $allowed_months)) && (in_array($day, $allowed_days)) && (in_array($hour, $allowed_hours))) { | |
| $badge_url = 'http://www.google.com/latitude/apps/badge/api?user='.$google_id.'&type=json'; | |
| // We get the content | |
| $content = file_get_contents($badge_url); | |
| // We convert the JSON to an object | |
| $json = json_decode($content, TRUE); | |
| //Read in coordinates | |
| $coord = $json['features'][0]['geometry']['coordinates']; | |
| if ($coord == NULL) { | |
| $return = array('result' => 'false'); | |
| } else { | |
| $return = array('result' => 'true', 'lat' => $coord[1], 'lon' => $coord[0]); | |
| foreach ($json['features'][0]['properties'] as $key => $value) { | |
| switch ($key) { | |
| case 'timeStamp': | |
| $value = date('d/M/Y H:i:s', $value); | |
| break; | |
| } //End of switch "Formatting data ready for display" | |
| //Save it for return | |
| $return[$key] = $value; | |
| } //End of foreach "Preparing data for delivery" | |
| } //End of if "Checking for data received" | |
| } else { | |
| $return = array( | |
| 'result' => 'false', | |
| 'reverseGeocode' => 'No updates allowed at this time.', | |
| 'timeStamp' => 'Unknown' | |
| ); | |
| } //End of if "Checking for allowed month, day and hours" | |
| echo json_encode(array_merge($return, array('datenow' => date('d/M/Y H:i:s')))); | |
| exit; | |
| } //End of if "$_GET['retrieve'] AJAX call" | |
| ?> | |
| <!DOCTYPE html> | |
| <html> | |
| <head> | |
| <meta name="viewport" content="initial-scale=1.0, user-scalable=no" /> | |
| <link href="http://code.google.com/apis/maps/documentation/javascript/examples/default.css" rel="stylesheet" type="text/css" /> | |
| <style type="text/css"> | |
| html { height: 100%; font-family: verdana; } | |
| body { height: 100%; margin: 0; padding: 0 } | |
| #map_canvas { width:100%; height: 95%; } | |
| .text-data { text-align: center; } | |
| .bold { font-weight: bold; padding-left: 30px; } | |
| </style> | |
| <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=true"></script> | |
| <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script> | |
| <script type="text/javascript"> | |
| var map = null; | |
| var marker = null; | |
| var circle = null; | |
| var last_marker = null; | |
| var last_circle = null; | |
| var refresh_interval = <?php echo $refresh_interval; ?>000; | |
| $(document).ready(function() { | |
| var myOptions = { | |
| center: new google.maps.LatLng(0, 0), | |
| zoom: 17, | |
| mapTypeId: google.maps.MapTypeId.ROADMAP | |
| }; | |
| map = new google.maps.Map(document.getElementById("map_canvas"), myOptions); | |
| place_marker(); | |
| }); //End of document ready | |
| function place_marker() { | |
| $.get("?retrieve=true", | |
| function(data){ | |
| if (data.result == 'true') { | |
| var latAndLon = new google.maps.LatLng(data.lat, data.lon); | |
| } else { | |
| var latAndLon = new google.maps.LatLng(0, 0); | |
| } //End of if "Checking we have data to put on the map" | |
| //Save the old markers for deletion | |
| last_marker = marker; | |
| last_circle = circle; | |
| //Set the marker | |
| marker = new google.maps.Marker({ | |
| position: latAndLon, | |
| map: map, | |
| title: data.reverseGeocode, | |
| icon: data.placardUrl | |
| }); | |
| // Add circle overlay and bind to marker | |
| circle = new google.maps.Circle({ | |
| map: map, | |
| radius: data.accuracyInMeters, | |
| fillColor: '#AA0000' | |
| }); | |
| circle.bindTo('center', marker, 'position'); | |
| //Clear down from last refresh | |
| if (last_marker != null) { last_marker.setMap(null); } | |
| if (last_circle != null) { last_circle.setMap(null); } | |
| map.panTo(latAndLon); | |
| $('#current-location').html(data.reverseGeocode); | |
| $('#latitude-update').html(data.timeStamp); | |
| $('#last-refresh').html(data.datenow); | |
| }, "json" | |
| ); //End of jQuery AJAX GET | |
| } //End of function "place_marker" | |
| setInterval( | |
| function() { | |
| place_marker(); | |
| }, refresh_interval | |
| ); // refresh every 10000 milliseconds | |
| </script> | |
| </head> | |
| <body> | |
| <div class="text-data"> | |
| <span class="bold">Current location:</span> <span id="current-location"></span> | |
| <span class="bold">Last Movement at:</span> <span id="latitude-update"></span> | |
| <span class="bold">Last Refresh at:</span> <span id="last-refresh"></span> | |
| </div> | |
| <div id="map_canvas"></div> | |
| </body> | |
| </html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment