Permalink
Browse files

Why is this not in version control already??

  • Loading branch information...
tsmith512 committed Apr 5, 2016
0 parents commit 26fbff80d704be926d8fe999809f439ccb163708
Showing with 444 additions and 0 deletions.
  1. +9 −0 config.php
  2. +51 −0 geocode.php
  3. +77 −0 geojson.php
  4. +127 −0 index.php
  5. +46 −0 map-heat.php
  6. +39 −0 map-history.php
  7. +36 −0 map-line.php
  8. +59 −0 record.php
@@ -0,0 +1,9 @@
<?php
global $conf;
$conf = array();
$conf['username'] = "maps_dev";
$conf['password'] = "galjp98whno3iEeU356y2yhilu";
$conf['database'] = "maps_dev";
$conf['key'] = "77QedyTiktSP1egjhGVit7vm";
$conf['gmaps_api_key'] = 'AIzaSyDWV8oVLeBHIcScGNtnK8ae2SL2mho62us';
@@ -0,0 +1,51 @@
<?php
include 'config.php';
function getCity($latlng) {
global $conf;
$ch = curl_init();
$url = ('https://maps.googleapis.com/maps/api/geocode/json?latlng=' . $latlng[0] . ',' . $latlng[1] . '&result_type=locality&key=' . $conf['gmaps_api_key']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, false);
curl_setopt($ch, CURLOPT_URL, $url);
$result = curl_exec($ch);
$output = json_decode($result, true);
curl_close($ch);
$match = isset($output['results']) ? reset($output['results']) : false;
$city = isset($match['address_components'][0]['long_name']) ? $match['address_components'][0]['long_name'] : NULL;
$full = isset($match['formatted_address']) ? $match['formatted_address'] : NULL;
return array($city, $full);
}
function geocodeLatest() {
global $conf;
$mysqli = new mysqli("localhost", $conf['username'], $conf['password'], $conf['database']);
if ($mysqli->connect_errno) { echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error; }
$latest = $mysqli->query("SELECT * FROM `location_history` WHERE `city` IS NULL ORDER BY `timestamp` DESC LIMIT 20");
$history = array();
while ($data = $latest->fetch_assoc()) {
// Round these to ~100 meters
$location = getCity(array($data['lat'], $data['lon']));
if (!empty($location[0])) {
$query = "UPDATE `location_history` SET `city` = '{$location[0]}', `full_city` = '{$location[1]}' WHERE `id` = {$data['id']} LIMIT 1;";
$update = $mysqli->query($query);
}
}
}
if ($conf['key'] == $_GET['key']) {
geocodeLatest();
} else {
header('HTTP/1.0 403 Forbidden');
}
@@ -0,0 +1,77 @@
<?php
$username="maps_dev";
$password="galjp98whno3iEeU356y2yhilu";
$database="maps_dev";
$mysqli = new mysqli("localhost", $username, $password, $database);
if ($mysqli->connect_errno) { echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error; }
switch ($_GET['q']) {
case 'points':
$latest = $mysqli->query("SELECT * FROM `location_history` ORDER BY `timestamp` DESC");
$history = array();
while ($data = $latest->fetch_assoc()) {
// Round these to ~100 meters
$lon = $data['lon']; // round($data['lon'], 3);
$lat = $data['lat']; // round($data['lat'], 3);
$history[] = array(
'type' => 'Feature',
'geometry' => array(
'type' => 'Point',
'coordinates' => array($lon, $lat),
),
'properties' => array(
'marker-color' => '#0066CC',
'marker-size' => 'small',
'marker-symbol' => '1',
),
);
}
break;
case 'cartodb':
$latest = $mysqli->query("SELECT * FROM `location_history` ORDER BY `timestamp` DESC");
$history = array();
while ($data = $latest->fetch_assoc()) {
$history[] = array(
'lon' => $data['lon'],
'lat' => $data['lat'],
'time' => $data['timestamp'],
);
}
break;
case 'line':
$latest = $mysqli->query("SELECT * FROM `location_history` ORDER BY `timestamp` DESC");
$history = array(
'type' => 'LineString',
'properties' => array(
'stroke' => '#006699',
'stroke-width' => 2
),
);
while ($data = $latest->fetch_assoc()) {
// Round these to ~100 meters
// $lon = round($data['lon'], 3);
$lon = $data['lon'];
// $lat = round($data['lat'], 3);
$lat = $data['lat'];
$history['coordinates'][] = array($lon, $lat);
}
break;
default:
# code...
break;
}
header("content-type: application/json");
print json_encode($history);
127 index.php
@@ -0,0 +1,127 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Map Image Builder</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap-theme.min.css">
<script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
</head>
<body>

<form class="form-horizontal">
<fieldset>

<!-- Form Name -->
<legend>Fetch Mapbox Image</legend>

<!-- Text input-->
<div class="control-group">
<label class="control-label" for="lat">Latitude</label>
<div class="controls">
<input id="lat" name="lat" type="text" value="" class="input-xlarge">

</div>
</div>

<!-- Text input-->
<div class="control-group">
<label class="control-label" for="lon">Longitude</label>
<div class="controls">
<input id="lon" name="lon" type="text" value="" class="input-xlarge">

</div>
</div>

<!-- Text input-->
<div class="control-group">
<label class="control-label" for="z">Zoom</label>
<div class="controls">
<input id="z" name="z" type="text" value="" class="input-xlarge">

</div>
</div>

<!-- Text input-->
<div class="control-group">
<label class="control-label" for="width">Width</label>
<div class="controls">
<input id="width" name="width" type="text" value="800" class="input-xlarge">

</div>
</div>

<!-- Text input-->
<div class="control-group">
<label class="control-label" for="height">Height</label>
<div class="controls">
<input id="height" name="height" type="text" value="800" class="input-xlarge">

</div>
</div>

<!-- Multiple Radios (inline) -->
<div class="control-group">
<label class="control-label" for="format">Format</label>
<div class="controls">
<label class="radio inline" for="format-0">
<input type="radio" name="format" id="format-0" value="png" checked="checked">
png
</label>
<label class="radio inline" for="format-1">
<input type="radio" name="format" id="format-1" value="jpg">
jpg
</label>
</div>
</div>

<!-- Text input-->
<div class="control-group">
<label class="control-label" for="mapid">Map ID</label>
<div class="controls">
<input id="mapid" name="mapid" type="text" value="tsmith512.b3c22e74" class="input-xlarge">

</div>
</div>

<!-- Text input-->
<div class="control-group">
<label class="control-label" for="access">Access Token</label>
<div class="controls">
<input id="access" name="access" type="text" <?php /* value="pk.eyJ1IjoidHNtaXRoNTEyIiwiYSI6IlBERzc0Mk0ifQ.IBWVp4rs5wKQ_8pkLOBXUw" */ ?> class="input-xlarge">

</div>
</div>

<!-- Button (Double) -->
<div class="control-group">
<label class="control-label" for="get"></label>
<div class="controls">
<button id="get" name="get" value="get" class="btn btn-success">Fetch</button>
</div>
</div>

</fieldset>
</form>

<div>
<?php
if ($_GET['get'] == 'get') {
$url = "https://api.mapbox.com/v4/{$_GET['mapid']}/pin-m-star+0099FF({$_GET['lon']},{$_GET['lat']})/{$_GET['lon']},{$_GET['lat']},{$_GET['z']}/{$_GET['width']}x{$_GET['height']}.{$_GET['format']}?access_token={$_GET['access']}";
echo "<img src='{$url}'>";
}
?>
</div>

<script>
if ("geolocation" in navigator) {
navigator.geolocation.getCurrentPosition(function(position) {
document.getElementById('lat').value = position.coords.latitude;
document.getElementById('lon').value = position.coords.longitude;
document.getElementById('z').value = 8;
});
}
</script>
</body>
</html>
@@ -0,0 +1,46 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Map Test</title>
<link href='https://api.mapbox.com/mapbox.js/v2.2.1/mapbox.css' rel='stylesheet' />
<link href='https://api.tiles.mapbox.com/mapbox.js/plugins/leaflet-markercluster/v0.4.0/MarkerCluster.css' rel='stylesheet' />
<link href='https://api.tiles.mapbox.com/mapbox.js/plugins/leaflet-markercluster/v0.4.0/MarkerCluster.Default.css' rel='stylesheet' />
</head>
<body style="margin:0;padding:0;">
<div id="map" style="width:100%;height:100vh;"></div>

<script src='https://api.mapbox.com/mapbox.js/v2.2.1/mapbox.js'></script>
<script src='https://api.mapbox.com/mapbox.js/plugins/leaflet-heat/v0.1.3/leaflet-heat.js'></script>
<script src="https://code.jquery.com/jquery-2.2.2.min.js"></script>
<script>
// Provide your access token
L.mapbox.accessToken = 'pk.eyJ1IjoidHNtaXRoNTEyIiwiYSI6ImIxMzdiYWYyOTRiZGM0NzQzMmU0ZWU4ZjJmMDU0MjYzIn0.SJchcHmW4PzUmwFcADM-sQ';
// Create a map in the div #map
var map = L.mapbox.map('map', 'tsmith512.b3c22e74');
// https://github.com/Leaflet/Leaflet.heat/issues/12
geoJson2heat = function(geojson, intensity) {
return geojson.features.map(function(feature) {
return [parseFloat(feature.geometry.coordinates[1]), parseFloat(feature.geometry.coordinates[0]), intensity];
});
}
// http://stackoverflow.com/questions/26629000/mapbox-issue-with-parsing-json-data-for-heatmap
var heat = L.heatLayer([], { maxZoom: 12 }).addTo(map);
$.getJSON('/geojson.php?q=points', function(data) {
var geojson = L.geoJson(data, {
onEachFeature: function(feature, layer) {
heat.addLatLng(L.latLng(feature.geometry.coordinates[1], feature.geometry.coordinates[0]), { radius: 5, blur: 5});
}
});
});
map.on('zoomend', function() {
var z = map.getZoom(); // Between 1 (out) and 22 (in)
heat.setOptions({radius: (Math.abs(z-10)+20), blur: ((Math.abs(z-12)*2)+20) });
});
</script>
</body>
</html>
@@ -0,0 +1,39 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Map Test</title>
<link href='https://api.mapbox.com/mapbox.js/v2.2.1/mapbox.css' rel='stylesheet' />
<link href='https://api.tiles.mapbox.com/mapbox.js/plugins/leaflet-markercluster/v0.4.0/MarkerCluster.css' rel='stylesheet' />
<link href='https://api.tiles.mapbox.com/mapbox.js/plugins/leaflet-markercluster/v0.4.0/MarkerCluster.Default.css' rel='stylesheet' />
</head>
<body style="margin:0;padding:0;">
<div id="map" style="width:100%;height:100vh;"></div>

<script src='https://api.mapbox.com/mapbox.js/v2.2.1/mapbox.js'></script>
<script src='https://api.tiles.mapbox.com/mapbox.js/plugins/leaflet-markercluster/v0.4.0/leaflet.markercluster.js'></script>
<script>
// Provide your access token
L.mapbox.accessToken = 'pk.eyJ1IjoidHNtaXRoNTEyIiwiYSI6ImIxMzdiYWYyOTRiZGM0NzQzMmU0ZWU4ZjJmMDU0MjYzIn0.SJchcHmW4PzUmwFcADM-sQ';
// Create a map in the div #map
var map = L.mapbox.map('map', 'tsmith512.b3c22e74');
// var locationHistory =
// var myLayer = L.mapbox.featureLayer().setGeoJSON(locationHistory).addTo(map);
L.mapbox.featureLayer()
.loadURL('/geojson.php?q=points')
.on('ready', function(e) {
var clusterGroup = new L.MarkerClusterGroup();
e.target.eachLayer(function(layer) {
clusterGroup.addLayer(layer);
});
map.addLayer(clusterGroup);
map.fitBounds(clusterGroup.getBounds());
});
</script>
</body>
</html>
@@ -0,0 +1,36 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Map Test</title>
<link href='https://api.mapbox.com/mapbox.js/v2.2.1/mapbox.css' rel='stylesheet' />
<link href='https://api.tiles.mapbox.com/mapbox.js/plugins/leaflet-markercluster/v0.4.0/MarkerCluster.css' rel='stylesheet' />
<link href='https://api.tiles.mapbox.com/mapbox.js/plugins/leaflet-markercluster/v0.4.0/MarkerCluster.Default.css' rel='stylesheet' />
</head>
<body style="margin:0;padding:0;">
<div id="map" style="width:100%;height:100vh;"></div>

<script src='https://api.mapbox.com/mapbox.js/v2.2.1/mapbox.js'></script>
<script src='https://api.tiles.mapbox.com/mapbox.js/plugins/turf/v1.4.0/turf.min.js'></script>
<script>
// Provide your access token
L.mapbox.accessToken = 'pk.eyJ1IjoidHNtaXRoNTEyIiwiYSI6ImIxMzdiYWYyOTRiZGM0NzQzMmU0ZWU4ZjJmMDU0MjYzIn0.SJchcHmW4PzUmwFcADM-sQ';
// Create a map in the div #map
var map = L.mapbox.map('map', 'tsmith512.b3c22e74');
// var locationHistory =
// var myLayer = L.mapbox.featureLayer().setGeoJSON(locationHistory).addTo(map);
var tripLine = L.mapbox.featureLayer()
.loadURL('/geojson.php?q=line')
.addTo(map);
tripLine.on('ready', function () {
map.fitBounds(tripLine.getBounds());
});
</script>
</body>
</html>
Oops, something went wrong.

0 comments on commit 26fbff8

Please sign in to comment.