]> git.openstreetmap.org Git - rails.git/blob - public/javascripts/map.js
Give OpenLayers three different hosts (all really pointing at tile) to
[rails.git] / public / javascripts / map.js
1 var map;
2 var markers;
3 var popup;
4
5 function createMap(divName) {
6    OpenLayers.Util.onImageLoadError = function() {
7       this.src = OpenLayers.Util.getImagesLocation() + "404.png";
8    }
9
10    map = new OpenLayers.Map(divName,
11                             { maxExtent: new OpenLayers.Bounds(-20037508,-20037508,20037508,20037508),
12                               numZoomLevels: 19,
13                               maxResolution: 156543,
14                               units: 'm',
15                               projection: "EPSG:41001" });
16
17    var mapnik = new OpenLayers.Layer.TMS("Mapnik",
18                                          ["http://a.tile.openstreetmap.org/","http://b.tile.openstreetmap.org/","http://c.tile.openstreetmap.org/"],
19                                          { type: 'png', getURL: getTileURL, displayOutsideMaxExtent: true });
20    map.addLayer(mapnik);
21
22    var osmarender = new OpenLayers.Layer.TMS("Osmarender",
23                                              "http://dev.openstreetmap.org/~ojw/Tiles/tile.php/",
24                                              { type: 'png', getURL: getTileURL, displayOutsideMaxExtent: true });
25    map.addLayer(osmarender);
26
27    markers = new OpenLayers.Layer.Markers("markers", { visibility: false });
28    map.addLayer(markers);
29
30    map.addControl(new OpenLayers.Control.LayerSwitcher());
31
32    return map;
33 }
34
35 function getTileURL(bounds) {
36    var res = this.map.getResolution();
37    var x = Math.round((bounds.left - this.maxExtent.left) / (res * this.tileSize.w));
38    var y = Math.round((this.maxExtent.top - bounds.top) / (res * this.tileSize.h));
39    var z = this.map.getZoom();
40    var limit = Math.pow(2, z);
41
42    if (y < 0 || y >= limit)
43    {
44      return OpenLayers.Util.getImagesLocation() + "404.png";
45    }
46    else
47    {
48      x = ((x % limit) + limit) % limit;
49
50      return this.url + z + "/" + x + "/" + y + "." + this.type;
51    }
52 }
53
54 function getArrowIcon() {
55    var size = new OpenLayers.Size(25, 22);
56    var offset = new OpenLayers.Pixel(-30, -27);
57    var icon = new OpenLayers.Icon("/images/arrow.png", size, offset);
58
59    return icon;
60 }
61
62 function addMarkerToMap(position, icon, description) {
63    var marker = new OpenLayers.Marker(position, icon);
64
65    markers.addMarker(marker);
66    markers.setVisibility(true);
67
68    if (description) {
69       marker.events.register("click", marker, function() { openMapPopup(marker, description) });
70    }
71
72    return marker;
73 }
74
75 function openMapPopup(marker, description) {
76    closeMapPopup();
77
78    popup = new OpenLayers.Popup.AnchoredBubble("popup", marker.lonlat,
79                                                sizeMapPopup(description),
80                                                "<p style='padding-right: 28px'>" + description + "</p>",
81                                                marker.icon, true);
82    popup.setBackgroundColor("#E3FFC5");
83    map.addPopup(popup);
84
85    return popup;
86 }
87
88 function closeMapPopup() {
89    if (popup) {
90       map.removePopup(popup);
91       delete popup;
92    }
93 }
94
95 function sizeMapPopup(text) {
96    var box = document.createElement("div");
97
98    box.innerHTML = text;
99    box.style.visibility = "hidden";
100    box.style.position = "absolute";
101    box.style.top = "0px";
102    box.style.left = "0px";
103    box.style.width = "200px";
104    box.style.height = "auto";
105
106    document.body.appendChild(box);
107
108    var width = box.offsetWidth;
109    var height = box.offsetHeight;
110
111    document.body.removeChild(box);
112
113    return new OpenLayers.Size(width + 30, height + 24);
114 }
115
116 function removeMarkerFromMap(marker){
117    markers.removeMarker(marker);
118 }
119
120 function getMapLayers() {
121    var layers = "";
122
123    for (var i=0; i< this.map.layers.length; i++) {
124       var layer = this.map.layers[i];
125
126       if (layer.isBaseLayer) {
127          layers += (layer == this.map.baseLayer) ? "B" : "0";
128       } else {
129          layers += (layer.getVisibility()) ? "T" : "F";
130       }
131    }
132
133    return layers;
134 }
135
136 function setMapLayers(layers) {
137    for (var i=0; i < layers.length; i++) {
138       var layer = map.layers[i];
139       var c = layers.charAt(i);
140
141       if (c == "B") {
142          map.setBaseLayer(layer);
143       } else if ( (c == "T") || (c == "F") ) {
144          layer.setVisibility(c == "T");
145       }
146    }
147 }
148
149 function mercatorToLonLat(merc) {
150    var lon = (merc.lon / 20037508.34) * 180;
151    var lat = (merc.lat / 20037508.34) * 180;
152
153    lat = 180/Math.PI * (2 * Math.atan(Math.exp(lat * Math.PI / 180)) - Math.PI / 2);
154
155    return new OpenLayers.LonLat(lon, lat);
156 }
157
158 function lonLatToMercator(ll) {
159    var lon = ll.lon * 20037508.34 / 180;
160    var lat = Math.log(Math.tan((90 + ll.lat) * Math.PI / 360)) / (Math.PI / 180);
161
162    lat = lat * 20037508.34 / 180;
163
164    return new OpenLayers.LonLat(lon, lat);
165 }
166
167 function scaleToZoom(scale) {
168    return Math.log(360.0/(scale * 512.0)) / Math.log(2.0);
169 }