Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 294 lines (264 sloc) 10.374 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
<?php
/*
PlaceEarth by Christian Heilmann
Version: 1.0
Homepage: http://isithackday.com/hacks/placeearth/
Copyright (c) 2009, Christian Heilmann
Code licensed under the BSD License:
http://wait-till-i.com/license.txt
*/
error_reporting(0);

if(isset($_GET['url'])){
  
  //TODO: Fallback for non-JS version, be creative!
  
}

if(isset($_GET['url'])){

  $key = 'RnqsLdPV34FbBSqnpSS8YMGcPcO9HRtWgAQ_Y7dN6pjCjF8ldmk_I.IrxtOOPZc-';
// ^ placemaker key, please replace with your own. Get it at
// https://developer.yahoo.com/wsregapp/

  $googlekey = 'ABQIAAAAijZqBZcz-rowoXZC1tt9iRQoXZxM6mF5JTMb2Ef3IreE9'.
               'giLxBTRgzgpNTtafX7QhFDMbJ8g20HbSg';
// ^ google key, please replace with your own! at
// http://code.google.com/apis/maps/signup.html

  $url = $_GET['url'];
  $o = $_GET['output'];

// load the HTML page using YQL to filter the HTML and fix any UTF-8
// nasties
  
  $realurl = 'http://query.yahooapis.com/v1/public/yql?q=select%20*%20'.
             'from%20html%20where%20url%20%3D%20%22'.
             urlencode($url).'%22&format=xml';
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, $realurl);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  $c = curl_exec($ch);
  curl_close($ch);
  if(strstr($c,'<')){
    $c = preg_replace("/.*<results>|<\/results>.*/",'',$c);
    $c = preg_replace("/<\?xml version=\"1\.0\"".
                      " encoding=\"UTF-8\"\?>/",'',$c);
    $c = strip_tags($c);
    $c = preg_replace("/[\r?\n]+/"," ",$c);
    if(isset($_GET['raw'])){
      echo "<h1>Content</h1>\n\n".htmlentities($c);
    }

// Post the result to Placemaker

    $ch = curl_init();
    if(isset($_GET['lang'])){
      $lang = '&inputLanguage='.$_GET['lang'];
    }
    define('POSTURL', 'http://wherein.yahooapis.com/v1/document');
    define('POSTVARS', 'appid='.$key.'&documentContent='.urlencode($c).
                    '&documentType=text/plain&outputType=xml'.$lang);
  
    $ch = curl_init(POSTURL);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, POSTVARS);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $placemaker = curl_exec($ch);
    curl_close($ch);

    if(isset($_GET['raw'])){
      echo "<h1>Placemaker</h1>\n\n<pre>".htmlentities($placemaker)."</pre>";
    };
  
// turn into an PHP object and loop over the results

    $placelist = '<ul id="locations">';
    $places = simplexml_load_string($placemaker, 'SimpleXMLElement',
                                    LIBXML_NOCDATA);
    $out = '';
    if($places->document->placeDetails){
      $foundplaces = array();

// create a hashmap of the places found to mix with the references found

      foreach($places->document->placeDetails as $p){
        $wkey = 'woeid'.$p->place->woeId;
        $foundplaces[$wkey]=array(
          'name'=>str_replace(', ZZ','',$p->place->name).'',
          'type'=>$p->place->type.'',
          'woeId'=>$p->place->woeId.'',
          'lat'=>$p->place->centroid->latitude.'',
          'lon'=>$p->place->centroid->longitude.''
        );
      }
    
// loop over references and filter out duplicates

      $refs = $places->document->referenceList->reference;
      $usedwoeids = array();
      foreach($refs as $r){
        foreach($r->woeIds as $wi){
          if(in_array($wi,$usedwoeids)){
            continue;
          } else {
            $usedwoeids[] = $wi.'';
          }
          $currentloc = $foundplaces["woeid".$wi];
          if($r->text!='' && $currentloc['name']!='' &&
            $currentloc['lat']!='' && $currentloc['lon']!=''){
            $text = preg_replace('/\s+/',' ',$r->text);
            $name = addslashes(str_replace(', ZZ','',$currentloc['name']));
            $desc = addslashes($text);
            $lat = $currentloc['lat'];
            $lon = $currentloc['lon'];
            $kml[] = '<Placemark>'.
                     '<name>'.$name.'</name>'.
                     '<description>'.$desc.'</description>'.
                     '<Point>'.
                     '<coordinates>'.$lon.','.$lat.',0</coordinates>'.
                     '</Point>'.
                     '</Placemark>';
                     $class = stripslashes($desc)."|$name|$lat|$lon";
            $placelist.= "<li>".
                         "<button class=\"$class\">".
                         "<span class=\"vcard\">\n<span class=\"adr\">\n".
                         "<span class=\"locality\">".stripslashes($desc).
                         " ($name)</span>\n".
                         "</span>\n<span class=\"geo\">()\n".
                         "<span class=\"latitude\">$lat</span>,\n".
                         "<span class=\"longitude\">$lon</span>".
                         "\n)</span>\n</span></button></li>";
          }
        }
      }
     if($o=='kml'){
       $kml = implode("\n",$kml);
       header('content-type:application/vnd.google-earth.kml+xml');
       header('Content-disposition: attachment; filename=locations.kml');
       echo '<'.'?xml version="1.0" encoding="UTF-8"?'.'>';
       echo '<kml xmlns="http://www.opengis.net/kml/2.2"><Document>'.$kml;
       echo '</Document></kml>';
     }
   }
   $placelist .= '</ul>';
  }
}
?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
<link rel="stylesheet" href="http://yui.yahooapis.com/2.7.0/build/reset-fonts-grids/reset-fonts-grids.css" type="text/css">
<script type="text/javascript" src="http://yui.yahooapis.com/combo?2.8.0r4/build/yahoo/yahoo-min.js&2.8.0r4/build/event/event-min.js"></script>
<style type="text/css">
html,body{background:#cc9;}
#doc2{
background:#fff;
border:1em solid #fff;
}
h1{color:#369;font-size:200%;font-family:calibri,sans-serif;font-weight:bold;padding-bottom:.5em}
h2{color:#036;font-size:120%;font-family:calibri,sans-serif;font-weight:bold;padding:.5em 0}
p{padding-bottom:.5em;}
form{
background:#69c;
padding:1em;
font-weight:bold;
-moz-border-radius:5px;
}
#map3d{width:100%;height:400px;}
.geo{position:absolute;top:0;left:-9999px;}

button{
border:none;
background:transparent;
display:block;
width:100%;
text-align:left;
}
ul{
margin:0;
padding:0;
list-style:square;
}
#locations{overflow:auto;height:400px;}
li{
color:#000;
border-top:1px solid #fff;
list-style-type:none;
padding:5px;
margin:0;
background:#ccc;
}
li:hover{
background:#69c;
color:#fff;
}
#ft{font-size:80%;margin-top:3em;color:#999;}
li:hover button{color:#fff;}
</style>
</head>
<body>
<div id="doc2" class="yui-t7">
<div id="hd" role="banner"><h1>PlaceEarth</h1></div>
<div id="bd" role="main">
<p>PlaceEarth is a demo of how you can use Yahoo Placemaker to find geographical locations in a certain web site and show them in Google Earth.</p>
<p>Simply enter the URL of the site you want to analyse in the following form.</p>
<form action="index.php" method="get">
<div><label for="url">URL:</label><input type="text" name="url" id="url" value="<?php echo $_GET['url'];?>"><input type="submit" value="get locations"></div>
</form>
<?php if(isset($_GET['url'])){?>
<?php if($placelist!=''){?>
<div class="yui-gc">
<div class="yui-u first"><h2>Explore on Google Earth:</h2>
<div id="map3d"></div></div>
<div class="yui-u"><h2>Locations found:</h2>
<?php echo $placelist;?></div>
</div>
<?php }else{?>
<h2 class="warn">Couldn't find any locations for this URL :-(</h2>
<?php }}?>
<h2>API (of sorts)</h2>
<p>If you want to see the outcome directly in Google Earth as KML, simply add an <code>output=kml</code> to the URL. For example: <br><a href="index.php?url=http%3A%2F%2Fwait-till-i.com&output=kml">index.php?url=http%3A%2F%2Fwait-till-i.com&output=kml</a></p>
<p>To debug and see why nothing was found add <code>raw=true</code> to the URL. For example: <br><a href="index.php?url=http%3A%2F%2Fwait-till-i.com&raw">index.php?url=http%3A%2F%2Fwait-till-i.com&raw=true</a></p>
</div>
<div id="ft" role="contentinfo"><p>Written by Christian Heilmann, <a href="http://github.com/codepo8/PlaceEarth">source available on GitHub</a></p></div>
</div>

<?php if(isset($_GET['url'])){?>

<script src="http://www.google.com/jsapi?key=<?php echo $googlekey;?>"></script>
<script type="text/javascript">
var ge;
google.load("earth", "1");

earth = function(){
function flyTo(e){
YAHOO.util.Event.preventDefault(e);
var t = YAHOO.util.Event.getTarget(e);
while(t.nodeName.toLowerCase()!=='button'){
t = t.parentNode;
}
if(t.nodeName.toLowerCase()==='button'){
var chunks = t.className.split('|');
var desc = chunks[0];
var name = chunks[1];
var lat = +chunks[2]
var lon = +chunks[3];
var lookAt = ge.createLookAt('');
var placemark = ge.createPlacemark('');
placemark.setDescription(desc + ' (' + name + ')');
var point = ge.createPoint('');
point.setLatitude(lat);
point.setLongitude(lon);
point.setAltitudeMode(ge.ALTITUDE_CLAMP_TO_GROUND);
placemark.setGeometry(point);
ge.getFeatures().appendChild(placemark);
var balloon = ge.createFeatureBalloon('');
balloon.setFeature(placemark);
balloon.setMinWidth(400);
ge.setBalloon(balloon);
lookAt.set(lat, lon, 10, ge.ALTITUDE_RELATIVE_TO_GROUND,
0, 60, 20000);
ge.getView().setAbstractView(lookAt);
}

}
function initInstance(instance) {
ge = instance;
ge.getWindow().setVisibility(true);
ge.getNavigationControl().setVisibility(ge.VISIBILITY_AUTO);
ge.getLayerRoot().enableLayerById(ge.LAYER_BORDERS, true);
ge.getLayerRoot().enableLayerById(ge.LAYER_ROADS, true);
YAHOO.util.Event.addListener('locations','click', earth.flyTo);
}
function failureInstance(errorCode) {
alert('Ooops: '+errorCode);
}
return{ic:initInstance,fc:failureInstance,flyTo:flyTo}
}();
window.onload = function(){
google.earth.createInstance('map3d', earth.ic, earth.fc);
}
</script>
<?php } ?>
</body>
</html>
Something went wrong with that request. Please try again.