1 <html xmlns="http://www.w3.org/1999/xhtml">
3 <title>OpenLayers: Sundials on a Spherical Mercator Map</title>
4 <script src='http://maps.google.com/maps?file=api&v=2&key=ABQIAAAAjpkAC9ePGem0lIq5XcMiuhR_wWLPFku8Ix9i2SXYRVK3e45q1BQUd_beF8dtzKET_EteAjPdGDwqpQ'></script>
5 <link rel="stylesheet" href="../theme/default/style.css" type="text/css" />
6 <link rel="stylesheet" href="style.css" type="text/css" />
8 <style type="text/css">
12 border: 1px solid black;
14 .olPopup p { margin:0px; font-size: .9em;}
15 .olPopup h2 { font-size:1.2em; }
17 <script src="../lib/OpenLayers.js"></script>
18 <script type="text/javascript">
26 projection: new OpenLayers.Projection("EPSG:900913"),
27 displayProjection: new OpenLayers.Projection("EPSG:4326"),
29 maxResolution: 156543.0339,
30 maxExtent: new OpenLayers.Bounds(-20037508.34, -20037508.34,
31 20037508.34, 20037508.34)
33 map = new OpenLayers.Map('map', options);
34 var mapnik = new OpenLayers.Layer.TMS(
35 "OpenStreetMap (Mapnik)",
36 "http://tile.openstreetmap.org/",
38 type: 'png', getURL: osm_getTileURL,
39 displayOutsideMaxExtent: true,
40 attribution: '<a href="http://www.openstreetmap.org/">OpenStreetMap</a>'
43 var gmap = new OpenLayers.Layer.Google("Google", {sphericalMercator:true});
44 var sundials = new OpenLayers.Layer.Vector("KML", {
45 projection: map.displayProjection,
46 strategies: [new OpenLayers.Strategy.Fixed()],
47 protocol: new OpenLayers.Protocol.HTTP({
48 url: "kml/sundials.kml",
49 format: new OpenLayers.Format.KML({
51 extractAttributes: true
56 map.addLayers([mapnik, gmap, sundials]);
58 select = new OpenLayers.Control.SelectFeature(sundials);
61 "featureselected": onFeatureSelect,
62 "featureunselected": onFeatureUnselect
65 map.addControl(select);
68 map.addControl(new OpenLayers.Control.LayerSwitcher());
71 new OpenLayers.Bounds(
72 68.774414, 11.381836, 123.662109, 34.628906
73 ).transform(map.displayProjection, map.projection)
76 function onPopupClose(evt) {
79 function onFeatureSelect(event) {
80 var feature = event.feature;
81 var selectedFeature = feature;
82 var popup = new OpenLayers.Popup.FramedCloud("chicken",
83 feature.geometry.getBounds().getCenterLonLat(),
84 new OpenLayers.Size(100,100),
85 "<h2>"+feature.attributes.name + "</h2>" + feature.attributes.description,
86 null, true, onPopupClose
88 feature.popup = popup;
91 function onFeatureUnselect(event) {
92 var feature = event.feature;
94 map.removePopup(feature.popup);
95 feature.popup.destroy();
99 function osm_getTileURL(bounds) {
100 var res = this.map.getResolution();
101 var x = Math.round((bounds.left - this.maxExtent.left) / (res * this.tileSize.w));
102 var y = Math.round((this.maxExtent.top - bounds.top) / (res * this.tileSize.h));
103 var z = this.map.getZoom();
104 var limit = Math.pow(2, z);
106 if (y < 0 || y >= limit) {
107 return OpenLayers.Util.getImagesLocation() + "404.png";
109 x = ((x % limit) + limit) % limit;
110 return this.url + z + "/" + x + "/" + y + "." + this.type;
115 <body onload="init()">
116 <h1 id="title">OSM + Google Maps + KML Reprojection</h1>
118 <div id="tags"></div>
121 Demonstrates loading and displaying a KML file on top of OpenStreetMap (OSM) and Google Maps data. Loads data from a KML file of sundials.
124 <div id="map" class="smallmap"></div>
126 <div id="docs"></div>