]> git.openstreetmap.org Git - rails.git/blob - public/javascripts/map.js
71f5b8ff8be18845881b77dab4db62fc4c53e2ae
[rails.git] / public / javascripts / map.js
1 var map;
2 var markers;
3 var popup;
4
5 function createMap(divName, centre, zoom) {
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://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    map.addControl(new OpenLayers.Control.LayerSwitcher());
28    map.setCenter(centre, zoom);
29
30    return map;
31 }
32
33 function getTileURL(bounds) {
34    var res = this.map.getResolution();
35    var x = Math.round((bounds.left - this.maxExtent.left) / (res * this.tileSize.w));
36    var y = Math.round((this.maxExtent.top - bounds.top) / (res * this.tileSize.h));
37    var z = this.map.getZoom();
38    var limit = Math.pow(2, z);
39
40    if (y < 0 || y >= limit)
41    {
42      return null;
43    }
44    else
45    {
46      x = ((x % limit) + limit) % limit;
47
48      return this.url + z + "/" + x + "/" + y + "." + this.type;
49    }
50 }
51
52 function addMarkerToMap(position, icon, description) {
53    if (markers == null) {
54       markers = new OpenLayers.Layer.Markers("markers");
55       map.addLayer(markers);
56    }
57
58    var marker = new OpenLayers.Marker(position, icon);
59
60    markers.addMarker(marker);
61
62    if (description) {
63       marker.events.register("click", marker, function() { openMapPopup(marker, description) });
64    }
65
66    return marker;
67 }
68
69 function openMapPopup(marker, description) {
70 //   var box = document.createElement("div");
71 //   box.innerHTML = description;
72 //   box.style.display = 'none';
73 //   box.style.width = "200px";
74 //   document.body.appendChild(box);
75
76    closeMapPopup();
77
78    popup = new OpenLayers.Popup.AnchoredBubble("popup", marker.lonlat,
79                                                new OpenLayers.Size(200, 50),
80                                                "<p>" + 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 removeMarkerFromMap(marker){
96    markers.removeMarker(marker);
97 }
98
99 function mercatorToLonLat(merc) {
100    var lon = (merc.lon / 20037508.34) * 180;
101    var lat = (merc.lat / 20037508.34) * 180;
102
103    lat = 180/Math.PI * (2 * Math.atan(Math.exp(lat * Math.PI / 180)) - Math.PI / 2);
104
105    return new OpenLayers.LonLat(lon, lat);
106 }
107
108 function lonLatToMercator(ll) {
109    var lon = ll.lon * 20037508.34 / 180;
110    var lat = Math.log(Math.tan((90 + ll.lat) * Math.PI / 360)) / (Math.PI / 180);
111
112    lat = lat * 20037508.34 / 180;
113
114    return new OpenLayers.LonLat(lon, lat);
115 }
116
117 function scaleToZoom(scale) {
118    return Math.log(360.0/(scale * 512.0)) / Math.log(2.0);
119 }