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 %>,
12 apiUrl: function (object) {
13 var url = "/api/" + OSM.API_VERSION + "/" + object.type + "/" + object.id;
15 if (object.type === "way" || object.type === "relation") {
17 } else if (object.version) {
18 url += "/" + object.version;
24 params: function(search) {
27 search = (search || window.location.search).replace('?', '').split(/&|;/);
29 for (var i = 0; i < search.length; ++i) {
31 j = pair.indexOf('='),
32 key = pair.slice(0, j),
33 val = pair.slice(++j);
34 params[key] = decodeURIComponent(val);
40 mapParams: function (search) {
41 var params = OSM.params(search), mapParams = {}, loc, match;
43 if (params.mlon && params.mlat) {
44 mapParams.marker = true;
45 mapParams.mlon = parseFloat(params.mlon);
46 mapParams.mlat = parseFloat(params.mlat);
49 // Old-style object parameters; still in use for edit links e.g. /edit?way=1234
51 mapParams.object = {type: 'node', id: parseInt(params.node)};
52 } else if (params.way) {
53 mapParams.object = {type: 'way', id: parseInt(params.way)};
54 } else if (params.relation) {
55 mapParams.object = {type: 'relation', id: parseInt(params.relation)};
58 var hash = OSM.parseHash(location.hash);
60 // Decide on a map starting position. Various ways of doing this.
62 mapParams.lon = hash.center.lng;
63 mapParams.lat = hash.center.lat;
64 mapParams.zoom = hash.zoom;
65 } else if (params.bbox) {
66 var bbox = params.bbox.split(',');
67 mapParams.bounds = L.latLngBounds(
68 [parseFloat(bbox[1]), parseFloat(bbox[0])],
69 [parseFloat(bbox[3]), parseFloat(bbox[2])]);
70 } else if (params.minlon && params.minlat && params.maxlon && params.maxlat) {
71 mapParams.bounds = L.latLngBounds(
72 [parseFloat(params.minlat), parseFloat(params.minlon)],
73 [parseFloat(params.maxlat), parseFloat(params.maxlon)]);
74 } else if (params.mlon && params.mlat) {
75 mapParams.lon = parseFloat(params.mlon);
76 mapParams.lat = parseFloat(params.mlat);
77 mapParams.zoom = parseInt(params.zoom || 12);
78 } else if (loc = $.cookie('_osm_location')) {
80 mapParams.lon = parseFloat(loc[0]);
81 mapParams.lat = parseFloat(loc[1]);
82 mapParams.zoom = parseInt(loc[2]);
83 } else if (OSM.home) {
84 mapParams.lon = OSM.home.lon;
85 mapParams.lat = OSM.home.lat;
87 } else if (OSM.location) {
88 mapParams.bounds = L.latLngBounds(
92 OSM.location.maxlon]);
96 mapParams.zoom = parseInt(params.zoom || 5);
99 mapParams.layers = hash.layers || (loc && loc[3]) || '';
101 var scale = parseFloat(params.scale);
103 mapParams.zoom = Math.log(360.0 / (scale * 512.0)) / Math.log(2.0);
109 parseHash: function(hash) {
112 var i = hash.indexOf('#');
117 hash = querystring.parse(hash.substr(i + 1));
119 var map = (hash.map || '').split('/'),
120 zoom = parseInt(map[0], 10),
121 lat = parseFloat(map[1]),
122 lon = parseFloat(map[2]);
124 if (!isNaN(zoom) && !isNaN(lat) && !isNaN(lon)) {
125 args.center = new L.LatLng(lat, lon);
130 args.layers = hash.layers;
136 formatHash: function(args) {
137 var center, zoom, layers;
139 if (args instanceof L.Map) {
140 center = args.getCenter();
141 zoom = args.getZoom();
142 layers = args.getLayersCode();
144 center = args.center || L.latLng(args.lat, args.lon);
146 layers = args.layers || '';
149 center = center.wrap();
150 layers = layers.replace('M', '');
152 var precision = OSM.zoomPrecision(zoom),
153 hash = '#map=' + zoom +
154 '/' + center.lat.toFixed(precision) +
155 '/' + center.lng.toFixed(precision);
158 hash += '&layers=' + layers;
164 zoomPrecision: function(zoom) {
165 return Math.max(0, Math.ceil(Math.log(zoom) / Math.LN2));
168 locationCookie: function(map) {
169 var center = map.getCenter().wrap(),
170 zoom = map.getZoom(),
171 precision = OSM.zoomPrecision(zoom);
172 return [center.lng.toFixed(precision), center.lat.toFixed(precision), zoom, map.getLayersCode()].join('|');
175 distance: function(latlng1, latlng2) {
176 var lat1 = latlng1.lat * Math.PI / 180,
177 lng1 = latlng1.lng * Math.PI / 180,
178 lat2 = latlng2.lat * Math.PI / 180,
179 lng2 = latlng2.lng * Math.PI / 180,
180 latdiff = lat2 - lat1,
181 lngdiff = lng2 - lng1;
183 return 6372795 * 2 * Math.asin(
185 Math.pow(Math.sin(latdiff / 2), 2) +
186 Math.cos(lat1) * Math.cos(lat2) * Math.pow(Math.sin(lngdiff / 2), 2)