2 <% if defined?(PIWIK) %>
3 PIWIK: <%= PIWIK.to_json %>,
6 MAX_REQUEST_AREA: <%= MAX_REQUEST_AREA.to_json %>,
7 SERVER_URL: <%= SERVER_URL.to_json %>,
8 API_VERSION: <%= API_VERSION.to_json %>,
9 STATUS: <%= STATUS.to_json %>,
10 MAX_NOTE_REQUEST_AREA: <%= MAX_NOTE_REQUEST_AREA.to_json %>,
11 OVERPASS_URL: <%= OVERPASS_URL.to_json %>,
13 apiUrl: function (object) {
14 var url = "/api/" + OSM.API_VERSION + "/" + object.type + "/" + object.id;
16 if (object.type === "way" || object.type === "relation") {
18 } else if (object.version) {
19 url += "/" + object.version;
25 params: function(search) {
28 search = (search || window.location.search).replace('?', '').split(/&|;/);
30 for (var i = 0; i < search.length; ++i) {
32 j = pair.indexOf('='),
33 key = pair.slice(0, j),
34 val = pair.slice(++j);
35 params[key] = decodeURIComponent(val);
41 mapParams: function (search) {
42 var params = OSM.params(search), mapParams = {}, loc, match;
44 if (params.mlon && params.mlat) {
45 mapParams.marker = true;
46 mapParams.mlon = parseFloat(params.mlon);
47 mapParams.mlat = parseFloat(params.mlat);
50 // Old-style object parameters; still in use for edit links e.g. /edit?way=1234
52 mapParams.object = {type: 'node', id: parseInt(params.node)};
53 } else if (params.way) {
54 mapParams.object = {type: 'way', id: parseInt(params.way)};
55 } else if (params.relation) {
56 mapParams.object = {type: 'relation', id: parseInt(params.relation)};
59 var hash = OSM.parseHash(location.hash);
61 // Decide on a map starting position. Various ways of doing this.
63 mapParams.lon = hash.center.lng;
64 mapParams.lat = hash.center.lat;
65 mapParams.zoom = hash.zoom;
66 } else if (params.bbox) {
67 var bbox = params.bbox.split(',');
68 mapParams.bounds = L.latLngBounds(
69 [parseFloat(bbox[1]), parseFloat(bbox[0])],
70 [parseFloat(bbox[3]), parseFloat(bbox[2])]);
71 } else if (params.minlon && params.minlat && params.maxlon && params.maxlat) {
72 mapParams.bounds = L.latLngBounds(
73 [parseFloat(params.minlat), parseFloat(params.minlon)],
74 [parseFloat(params.maxlat), parseFloat(params.maxlon)]);
75 } else if (params.mlon && params.mlat) {
76 mapParams.lon = parseFloat(params.mlon);
77 mapParams.lat = parseFloat(params.mlat);
78 mapParams.zoom = parseInt(params.zoom || 12);
79 } else if (loc = $.cookie('_osm_location')) {
81 mapParams.lon = parseFloat(loc[0]);
82 mapParams.lat = parseFloat(loc[1]);
83 mapParams.zoom = parseInt(loc[2]);
84 } else if (OSM.home) {
85 mapParams.lon = OSM.home.lon;
86 mapParams.lat = OSM.home.lat;
88 } else if (OSM.location) {
89 mapParams.bounds = L.latLngBounds(
93 OSM.location.maxlon]);
97 mapParams.zoom = parseInt(params.zoom || 5);
100 mapParams.layers = hash.layers || (loc && loc[3]) || '';
102 var scale = parseFloat(params.scale);
104 mapParams.zoom = Math.log(360.0 / (scale * 512.0)) / Math.log(2.0);
110 parseHash: function(hash) {
113 var i = hash.indexOf('#');
118 hash = querystring.parse(hash.substr(i + 1));
120 var map = (hash.map || '').split('/'),
121 zoom = parseInt(map[0], 10),
122 lat = parseFloat(map[1]),
123 lon = parseFloat(map[2]);
125 if (!isNaN(zoom) && !isNaN(lat) && !isNaN(lon)) {
126 args.center = new L.LatLng(lat, lon);
131 args.layers = hash.layers;
137 formatHash: function(args) {
138 var center, zoom, layers;
140 if (args instanceof L.Map) {
141 center = args.getCenter();
142 zoom = args.getZoom();
143 layers = args.getLayersCode();
145 center = args.center || L.latLng(args.lat, args.lon);
147 layers = args.layers || '';
150 center = center.wrap();
151 layers = layers.replace('M', '');
153 var precision = OSM.zoomPrecision(zoom),
154 hash = '#map=' + zoom +
155 '/' + center.lat.toFixed(precision) +
156 '/' + center.lng.toFixed(precision);
159 hash += '&layers=' + layers;
165 zoomPrecision: function(zoom) {
166 return Math.max(0, Math.ceil(Math.log(zoom) / Math.LN2));
169 locationCookie: function(map) {
170 var center = map.getCenter().wrap(),
171 zoom = map.getZoom(),
172 precision = OSM.zoomPrecision(zoom);
173 return [center.lng.toFixed(precision), center.lat.toFixed(precision), zoom, map.getLayersCode()].join('|');
176 distance: function(latlng1, latlng2) {
177 var lat1 = latlng1.lat * Math.PI / 180,
178 lng1 = latlng1.lng * Math.PI / 180,
179 lat2 = latlng2.lat * Math.PI / 180,
180 lng2 = latlng2.lng * Math.PI / 180,
181 latdiff = lat2 - lat1,
182 lngdiff = lng2 - lng1;
184 return 6372795 * 2 * Math.asin(
186 Math.pow(Math.sin(latdiff / 2), 2) +
187 Math.cos(lat1) * Math.cos(lat2) * Math.pow(Math.sin(lngdiff / 2), 2)