1 //= depend_on settings.yml
2 //= depend_on settings.local.yml
3 //= require querystring
6 <% if defined?(PIWIK) %>
7 PIWIK: <%= PIWIK.to_json %>,
10 MAX_REQUEST_AREA: <%= Settings.max_request_area.to_json %>,
11 SERVER_PROTOCOL: <%= Settings.server_protocol.to_json %>,
12 SERVER_URL: <%= Settings.server_url.to_json %>,
13 API_VERSION: <%= Settings.api_version.to_json %>,
14 STATUS: <%= Settings.status.to_json %>,
15 MAX_NOTE_REQUEST_AREA: <%= Settings.max_note_request_area.to_json %>,
16 OVERPASS_URL: <%= Settings.overpass_url.to_json %>,
17 NOMINATIM_URL: <%= Settings.nominatim_url.to_json %>,
18 GRAPHHOPPER_URL: <%= Settings.graphhopper_url.to_json %>,
19 FOSSGIS_OSRM_URL: <%= Settings.fossgis_osrm_url.to_json %>,
20 DEFAULT_LOCALE: <%= I18n.default_locale.to_json %>,
22 <% if Settings.key?(:thunderforest_key) %>
23 THUNDERFOREST_KEY: <%= Settings.thunderforest_key.to_json %>,
26 MARKER_GREEN: <%= image_path("marker-green.png").to_json %>,
27 MARKER_RED: <%= image_path("marker-red.png").to_json %>,
29 MARKER_ICON: <%= image_path("images/marker-icon.png").to_json %>,
30 MARKER_ICON_2X: <%= image_path("images/marker-icon-2x.png").to_json %>,
31 MARKER_SHADOW: <%= image_path("images/marker-shadow.png").to_json %>,
33 NEW_NOTE_MARKER: <%= image_path("new_note_marker.png").to_json %>,
34 OPEN_NOTE_MARKER: <%= image_path("open_note_marker.png").to_json %>,
35 CLOSED_NOTE_MARKER: <%= image_path("closed_note_marker.png").to_json %>,
37 SEARCHING: <%= image_path("searching.gif").to_json %>,
39 apiUrl: function (object) {
40 var url = "/api/" + OSM.API_VERSION + "/" + object.type + "/" + object.id;
42 if (object.type === "way" || object.type === "relation") {
44 } else if (object.version) {
45 url += "/" + object.version;
51 params: function(search) {
54 search = (search || window.location.search).replace('?', '').split(/&|;/);
56 for (var i = 0; i < search.length; ++i) {
58 j = pair.indexOf('='),
59 key = pair.slice(0, j),
60 val = pair.slice(++j);
63 params[key] = decodeURIComponent(val);
65 // Ignore parse exceptions
72 mapParams: function (search) {
73 var params = OSM.params(search), mapParams = {}, loc, match;
75 if (params.mlon && params.mlat) {
76 mapParams.marker = true;
77 mapParams.mlon = parseFloat(params.mlon);
78 mapParams.mlat = parseFloat(params.mlat);
81 // Old-style object parameters; still in use for edit links e.g. /edit?way=1234
83 mapParams.object = {type: 'node', id: parseInt(params.node)};
84 } else if (params.way) {
85 mapParams.object = {type: 'way', id: parseInt(params.way)};
86 } else if (params.relation) {
87 mapParams.object = {type: 'relation', id: parseInt(params.relation)};
90 var hash = OSM.parseHash(location.hash);
92 // Decide on a map starting position. Various ways of doing this.
94 mapParams.lon = hash.center.lng;
95 mapParams.lat = hash.center.lat;
96 mapParams.zoom = hash.zoom;
97 } else if (params.bbox) {
98 var bbox = params.bbox.split(',');
99 mapParams.bounds = L.latLngBounds(
100 [parseFloat(bbox[1]), parseFloat(bbox[0])],
101 [parseFloat(bbox[3]), parseFloat(bbox[2])]);
102 } else if (params.minlon && params.minlat && params.maxlon && params.maxlat) {
103 mapParams.bounds = L.latLngBounds(
104 [parseFloat(params.minlat), parseFloat(params.minlon)],
105 [parseFloat(params.maxlat), parseFloat(params.maxlon)]);
106 } else if (params.mlon && params.mlat) {
107 mapParams.lon = parseFloat(params.mlon);
108 mapParams.lat = parseFloat(params.mlat);
109 mapParams.zoom = parseInt(params.zoom || 12);
110 } else if (loc = $.cookie('_osm_location')) {
111 loc = loc.split("|");
112 mapParams.lon = parseFloat(loc[0]);
113 mapParams.lat = parseFloat(loc[1]);
114 mapParams.zoom = parseInt(loc[2]);
115 } else if (OSM.home) {
116 mapParams.lon = OSM.home.lon;
117 mapParams.lat = OSM.home.lat;
119 } else if (OSM.location) {
120 mapParams.bounds = L.latLngBounds(
121 [OSM.location.minlat,
122 OSM.location.minlon],
123 [OSM.location.maxlat,
124 OSM.location.maxlon]);
126 mapParams.lon = -0.1;
127 mapParams.lat = 51.5;
128 mapParams.zoom = parseInt(params.zoom || 5);
131 mapParams.layers = hash.layers || (loc && loc[3]) || '';
133 var scale = parseFloat(params.scale);
135 mapParams.zoom = Math.log(360.0 / (scale * 512.0)) / Math.log(2.0);
141 parseHash: function(hash) {
142 var querystring = require("querystring-component"),
145 var i = hash.indexOf('#');
150 hash = querystring.parse(hash.substr(i + 1));
152 var map = (hash.map || '').split('/'),
153 zoom = parseInt(map[0], 10),
154 lat = parseFloat(map[1]),
155 lon = parseFloat(map[2]);
157 if (!isNaN(zoom) && !isNaN(lat) && !isNaN(lon)) {
158 args.center = new L.LatLng(lat, lon);
163 args.layers = hash.layers;
169 formatHash: function(args) {
170 var center, zoom, layers;
172 if (args instanceof L.Map) {
173 center = args.getCenter();
174 zoom = args.getZoom();
175 layers = args.getLayersCode();
177 center = args.center || L.latLng(args.lat, args.lon);
179 layers = args.layers || '';
182 center = center.wrap();
183 layers = layers.replace('M', '');
185 var precision = OSM.zoomPrecision(zoom),
186 hash = '#map=' + zoom +
187 '/' + center.lat.toFixed(precision) +
188 '/' + center.lng.toFixed(precision);
191 hash += '&layers=' + layers;
197 zoomPrecision: function(zoom) {
198 return Math.max(0, Math.ceil(Math.log(zoom) / Math.LN2));
201 locationCookie: function(map) {
202 var center = map.getCenter().wrap(),
203 zoom = map.getZoom(),
204 precision = OSM.zoomPrecision(zoom);
205 return [center.lng.toFixed(precision), center.lat.toFixed(precision), zoom, map.getLayersCode()].join('|');
208 distance: function(latlng1, latlng2) {
209 var lat1 = latlng1.lat * Math.PI / 180,
210 lng1 = latlng1.lng * Math.PI / 180,
211 lat2 = latlng2.lat * Math.PI / 180,
212 lng2 = latlng2.lng * Math.PI / 180,
213 latdiff = lat2 - lat1,
214 lngdiff = lng2 - lng1;
216 return 6372795 * 2 * Math.asin(
218 Math.pow(Math.sin(latdiff / 2), 2) +
219 Math.cos(lat1) * Math.cos(lat2) * Math.pow(Math.sin(lngdiff / 2), 2)