Skip to content

Instantly share code, notes, and snippets.

@diplix
Last active October 31, 2015 17:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save diplix/f735373e8caca147a2d7 to your computer and use it in GitHub Desktop.
Save diplix/f735373e8caca147a2d7 to your computer and use it in GitHub Desktop.
get mf2/h-entry geo data and display in a map
<?php
$reload = $_GET['reload'];
$id = $_GET['id']; // category id for wirres.net
$url = $_GET['url']; // or any other url
if (!isset($id) AND !is_numeric($id)) { $id = 44; }
if ($reload != "1") { $reload = false; }
$geoCacheFile = "geo-" . $id . ".json";
if (isset($url)) {
$geoCacheFile = "geo-" . md5($url) . ".json";
}
$loadData = true;
if ( file_exists( $geoCacheFile ) ) { // there is a cache
$timeout = 1440; // 24 hours
$SiteCacheTime = filemtime( $geoCacheFile );
if ( ( time() - $SiteCacheTime ) < ( $timeout * 60 ) AND !$reload ) {
// load from file
$json_data = file_get_contents($geoCacheFile);
$loadData = false;
} else {
// reload from site
$loadData = true;
}
}
if ($loadData) {
// get data
if (isset($url)) {
$url_array = array($url);
}
// or get from archives
else {
$url_array = array(
"http://wirres.net/article/archive/".$id."/0/",
"http://wirres.net/article/archive/".$id."/10/",
"http://wirres.net/article/archive/".$id."/20/",
"http://wirres.net/article/archive/".$id."/30/",
"http://wirres.net/article/archive/".$id."/40/",
"http://wirres.net/article/archive/".$id."/50/",
"http://wirres.net/article/archive/".$id."/60/",
"http://wirres.net/article/archive/".$id."/70/",
"http://wirres.net/article/archive/".$id."/80/",
"http://wirres.net/article/archive/".$id."/90/",
"http://wirres.net/article/archive/".$id."/100/",
"http://wirres.net/article/archive/".$id."/110/",
"http://wirres.net/article/archive/".$id."/120/",
"http://wirres.net/article/archive/".$id."/130/",
"http://wirres.net/article/archive/".$id."/140/",
"http://wirres.net/article/archive/".$id."/150/",
"http://wirres.net/article/archive/".$id."/160/",
"http://wirres.net/article/archive/".$id."/170/",
"http://wirres.net/article/archive/".$id."/180/",
"http://wirres.net/article/archive/".$id."/190/",
"http://wirres.net/article/archive/".$id."/200/",
"http://wirres.net/article/archive/".$id."/210/",
"http://wirres.net/article/archive/".$id."/220/",
"http://wirres.net/article/archive/".$id."/230/",
"http://wirres.net/article/archive/".$id."/240/",
"http://wirres.net/article/archive/".$id."/250/",
"http://wirres.net/article/archive/".$id."/260/",
"http://wirres.net/article/archive/".$id."/270/",
"http://wirres.net/article/archive/".$id."/280/",
"http://wirres.net/article/archive/".$id."/290/",
"http://wirres.net/article/archive/".$id."/300/",
"http://wirres.net/article/archive/".$id."/310/",
"http://wirres.net/article/archive/".$id."/320/",
"http://wirres.net/article/archive/".$id."/330/",
"http://wirres.net/article/archive/".$id."/340/",
"http://wirres.net/article/archive/".$id."/350/",
);
}
$url_count = count($url_array);
$j = 0;
for ($i = 0; $i <= $url_count; $i++) {
$response = json_decode(file_get_contents('http://pin13.net/mf2/?url='.urlencode($url_array[$i])), true);
foreach ($response['items'] as $item) {
if ( $item['properties']['geo'][0]['type'][0] == "h-geo") {
$res[$j]['title'] = $item['properties']['name'][0];
$res[$j]['lat'] = $item['properties']['geo'][0]['properties']['latitude'][0];
$res[$j]['lng'] = $item['properties']['geo'][0]['properties']['longitude'][0];
$res[$j]['cnt'] = $item['properties']['content'][0]['html'];
$res[$j]['cntv'] = $item['properties']['content'][0]['value'];
$res[$j]['url'] = $item['properties']['url'][0];
$j++;
}
if ( $item['properties']['location'][0]['type'][0] == "h-card" AND isset($item['properties']['location'][0]['properties']['latitude'][0]) ) {
$res[$j]['title'] = $item['properties']['name'];
$res[$j]['lat'] = $item['properties']['location'][0]['properties']['latitude'][0];
$res[$j]['lng'] = $item['properties']['location'][0]['properties']['longitude'][0];
$res[$j]['cnt'] = $item['properties']['content'][0]['html'];
$res[$j]['cntv'] = $item['properties']['content'][0]['value'];
$res[$j]['url'] = $item['properties']['url'][0];
$j++;
}
}
}
$json_data = json_encode($res);
// save to file
file_put_contents($geoCacheFile, $json_data);
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<style type="text/css">
html { height: 100% }
body { height: 100%; margin: 0; padding: 0 }
#map_canvas { height: 100% }
.gm-style img.u-photo { max-width:100% !important; height:auto !important;}
</style>
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=true"></script>
<!-- https://github.com/jawj/OverlappingMarkerSpiderfier
Overlapping Marker Spiderfier for Google Maps API v3 -->
<script src="oms.min.js"></script>
<script>
window.onload = function() {
var gm = google.maps;
var map = new gm.Map(document.getElementById('map_canvas'), {
mapTypeId: gm.MapTypeId.ROADMAP,
center: new gm.LatLng(50, 0), zoom: 6, // whatevs: fitBounds will override
scrollwheel: true
});
var iw = new gm.InfoWindow({
maxWidth: 200,
});
var oms = new OverlappingMarkerSpiderfier(map,
{markersWontMove: true, markersWontHide: true});
oms.addListener('click', function(marker) {
iw.setContent(marker.desc);
iw.open(map, marker);
});
oms.addListener('spiderfy', function(markers) {});
oms.addListener('unspiderfy', function(markers) {});
var bounds = new gm.LatLngBounds();
for (var i = 0; i < window.mapData.length; i ++) {
var datum = window.mapData[i];
var loc = new gm.LatLng(datum.lat, datum.lon);
bounds.extend(loc);
var marker = new gm.Marker({
position: loc,
title: datum.h,
map: map,
});
marker.desc = datum.d;
oms.addMarker(marker);
}
map.fitBounds(bounds);
// for debugging/exploratory use in console
window.map = map;
window.oms = oms;
}
</script>
</head>
<body>
<div id="map_canvas" style="width:100%; height:100%"></div>
</body>
<script>
// http://stackoverflow.com/questions/1199352/smart-way-to-shorten-long-strings-with-javascript
String.prototype.trunc =
function(n,useWordBoundary){
var toLong = this.length>n,
s_ = toLong ? this.substr(0,n-1) : this;
s_ = useWordBoundary && toLong ? s_.substr(0,s_.lastIndexOf(' ')) : s_;
return toLong ? s_ + ' …' : s_;
};
var jsondata = <?php echo $json_data; ?>;
var data = [];
//for (var j = 0; j < jsondata; j ++) data.push({
jsondata.forEach(function(mapData,idx) {
var header = '';
if (mapData.title.length > 0) {
header = '<h2><a style="text-decoration: none; color: #000;" href="' + mapData.url + '">' + mapData.title + '</a></h2>';
} else {
//mapData.title = 'ohne titel';
mapData.title = mapData.cntv.trunc(30,true);
}
data.push({
lon: mapData.lng,
lat: mapData.lat,
h: mapData.title + ' ',
d: header + mapData.cnt+'<p>'+'→ <a href="' + mapData.url + '">original</a></p>'
});
});
window.mapData = data;
</script>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment