]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - public/plugins/Mapstraction/js/mxn.openlayers.core.js
Added type-hint for StartShowNoticeFormData hook
[quix0rs-gnu-social.git] / public / plugins / Mapstraction / js / mxn.openlayers.core.js
1 mxn.register('openlayers', {
2
3     Mapstraction: {
4
5         init: function(element, api){
6             var me = this;
7             this.maps[api] = new OpenLayers.Map(
8                 element.id,
9                 {
10                     maxExtent: new OpenLayers.Bounds(-20037508.34,-20037508.34,20037508.34,20037508.34),
11                     maxResolution:156543,
12                     numZoomLevels:18,
13                     units:'meters',
14                     projection: "EPSG:41001"
15                 }
16             );
17
18             this.layers['osmmapnik'] = new OpenLayers.Layer.TMS(
19                 'OSM Mapnik',
20                 [
21                 "http://a.tile.openstreetmap.org/",
22                 "http://b.tile.openstreetmap.org/",
23                 "http://c.tile.openstreetmap.org/"
24                 ],
25                 {
26                     type:'png',
27                     getURL: function (bounds) {
28                         var res = this.map.getResolution();
29                         var x = Math.round ((bounds.left - this.maxExtent.left) / (res * this.tileSize.w));
30                         var y = Math.round ((this.maxExtent.top - bounds.top) / (res * this.tileSize.h));
31                         var z = this.map.getZoom();
32                         var limit = Math.pow(2, z);
33                         if (y < 0 || y >= limit) {
34                             return null;
35                         } else {
36                             x = ((x % limit) + limit) % limit;
37                             var path = z + "/" + x + "/" + y + "." + this.type;
38                             var url = this.url;
39                             if (url instanceof Array) {
40                                 url = this.selectUrl(path, url);
41                             }
42                             return url + path;
43                         }
44                     },
45                     displayOutsideMaxExtent: true
46                 }
47             );
48
49             this.layers['osm'] = new OpenLayers.Layer.TMS(
50                 'OSM',
51                 [
52                 "http://a.tah.openstreetmap.org/Tiles/tile.php/",
53                 "http://b.tah.openstreetmap.org/Tiles/tile.php/",
54                 "http://c.tah.openstreetmap.org/Tiles/tile.php/"
55                 ],
56                 {
57                     type:'png',
58                     getURL: function (bounds) {
59                         var res = this.map.getResolution();
60                         var x = Math.round ((bounds.left - this.maxExtent.left) / (res * this.tileSize.w));
61                         var y = Math.round ((this.maxExtent.top - bounds.top) / (res * this.tileSize.h));
62                         var z = this.map.getZoom();
63                         var limit = Math.pow(2, z);
64                         if (y < 0 || y >= limit) {
65                             return null;
66                         } else {
67                             x = ((x % limit) + limit) % limit;
68                             var path = z + "/" + x + "/" + y + "." + this.type;
69                             var url = this.url;
70                             if (url instanceof Array) {
71                                 url = this.selectUrl(path, url);
72                             }
73                             return url + path;
74                         }
75                     },
76                     displayOutsideMaxExtent: true
77                 }
78             );
79
80             this.maps[api].addLayer(this.layers['osmmapnik']);
81             this.maps[api].addLayer(this.layers['osm']);
82         },
83
84         applyOptions: function(){
85             // var map = this.maps[this.api];
86             // var myOptions = [];
87             // if (this.options.enableDragging) {
88             //     myOptions.draggable = true;
89             // }
90             // if (this.options.enableScrollWheelZoom){
91             //     myOptions.scrollwheel = true;
92             // }
93             // map.setOptions(myOptions);
94         },
95
96         resizeTo: function(width, height){
97             this.currentElement.style.width = width;
98             this.currentElement.style.height = height;
99             this.maps[this.api].updateSize();
100         },
101
102         addControls: function( args ) {
103             var map = this.maps[this.api];
104             // FIXME: OpenLayers has a bug removing all the controls says crschmidt
105             for (var i = map.controls.length; i>1; i--) {
106                 map.controls[i-1].deactivate();
107                 map.removeControl(map.controls[i-1]);
108             }
109             if ( args.zoom == 'large' )      {
110                 map.addControl(new OpenLayers.Control.PanZoomBar());
111             }
112             else if ( args.zoom == 'small' ) {
113                 map.addControl(new OpenLayers.Control.ZoomPanel());
114                 if ( args.pan) {
115                     map.addControl(new OpenLayers.Control.PanPanel());
116                 }
117             }
118             else {
119                 if ( args.pan){
120                     map.addControl(new OpenLayers.Control.PanPanel());
121                 }
122             }
123             if ( args.overview ) {
124                 map.addControl(new OpenLayers.Control.OverviewMap());
125             }
126             if ( args.map_type ) {
127                 map.addControl(new OpenLayers.Control.LayerSwitcher());
128             }
129         },
130
131         addSmallControls: function() {
132             var map = this.maps[this.api];
133             this.addControlsArgs.pan = false;
134             this.addControlsArgs.scale = false;
135             this.addControlsArgs.zoom = 'small';
136             map.addControl(new OpenLayers.Control.ZoomBox());
137             map.addControl(new OpenLayers.Control.LayerSwitcher({
138                 'ascending':false
139             }));
140         },
141
142         addLargeControls: function() {
143             var map = this.maps[this.api];
144             map.addControl(new OpenLayers.Control.PanZoomBar());
145             this.addControlsArgs.pan = true;
146             this.addControlsArgs.zoom = 'large';
147         },
148
149         addMapTypeControls: function() {
150             var map = this.maps[this.api];
151             map.addControl( new OpenLayers.Control.LayerSwitcher({
152                 'ascending':false
153             }) );
154             this.addControlsArgs.map_type = true;
155         },
156
157         setCenterAndZoom: function(point, zoom) {
158             var map = this.maps[this.api];
159             var pt = point.toProprietary(this.api);
160             map.setCenter(point.toProprietary(this.api), zoom);
161         },
162
163         addMarker: function(marker, old) {
164             var map = this.maps[this.api];
165             var pin = marker.toProprietary(this.api);
166             if (!this.layers['markers']) {
167                 this.layers['markers'] = new OpenLayers.Layer.Markers('markers');
168                 map.addLayer(this.layers['markers']);
169             }
170             this.layers['markers'].addMarker(pin);
171
172             return pin;
173         },
174
175         removeMarker: function(marker) {
176             var map = this.maps[this.api];
177             var pin = marker.toProprietary(this.api);
178             this.layers['markers'].removeMarker(pin);
179             pin.destroy();
180
181         },
182
183         removeAllMarkers: function() {
184             var map = this.maps[this.api];
185
186             // TODO: Add provider code
187         },
188
189         declutterMarkers: function(opts) {
190             var map = this.maps[this.api];
191
192             // TODO: Add provider code
193         },
194
195         addPolyline: function(polyline, old) {
196             var map = this.maps[this.api];
197             var pl = polyline.toProprietary(this.api);
198
199             if (!this.layers['polylines']) {
200                 this.layers['polylines'] = new OpenLayers.Layer.Vector('polylines');
201                 map.addLayer(this.layers['polylines']);
202             }
203             polyline.setChild(pl);
204             this.layers['polylines'].addFeatures([pl]);
205             return pl;
206         },
207
208         removePolyline: function(polyline) {
209             var map = this.maps[this.api];
210             var pl = polyline.toProprietary(this.api);
211             this.layers['polylines'].removeFeatures([pl]);
212         },
213         removeAllPolylines: function() {
214             var olpolylines = [];
215             for(var i = 0, length = this.polylines.length; i < length; i++){
216                 olpolylines.push(this.polylines[i].toProprietary(this.api));
217             }
218             if (this.layers['polylines']) this.layers['polylines'].removeFeatures(olpolylines);
219         },
220
221         getCenter: function() {
222             var map = this.maps[this.api];
223             pt = map.getCenter();
224             return new mxn.LatLonPoint(pt.lat, pt.lon);
225         },
226
227         setCenter: function(point, options) {
228             var map = this.maps[this.api];
229             var pt = point.toProprietary(this.api);
230             map.setCenter(pt);
231
232         },
233
234         setZoom: function(zoom) {
235             var map = this.maps[this.api];
236             map.zoomTo(zoom);
237         },
238
239         getZoom: function() {
240             var map = this.maps[this.api];
241             return map.zoom;
242         },
243
244         getZoomLevelForBoundingBox: function( bbox ) {
245             var map = this.maps[this.api];
246             // throw 'Not implemented';
247             return zoom;
248         },
249
250         setMapType: function(type) {
251             var map = this.maps[this.api];
252             throw 'Not implemented (setMapType)';
253         },
254
255         getMapType: function() {
256             var map = this.maps[this.api];
257             // TODO: implement actual layer support
258             return mxn.Mapstraction.ROAD;
259         },
260
261         getBounds: function () {
262             var map = this.maps[this.api];
263             var olbox = map.calculateBounds();
264             return new mxn.BoundingBox(olbox.bottom, olbox.left, olbox.top, olbox.right);
265         },
266
267         setBounds: function(bounds){
268             var map = this.maps[this.api];
269             var sw = bounds.getSouthWest();
270             var ne = bounds.getNorthEast();
271
272             if(sw.lon > ne.lon) {
273                 sw.lon -= 360;
274             }
275
276             var obounds = new OpenLayers.Bounds();
277
278             obounds.extend(new mxn.LatLonPoint(sw.lat,sw.lon).toProprietary(this.api));
279             obounds.extend(new mxn.LatLonPoint(ne.lat,ne.lon).toProprietary(this.api));
280             map.zoomToExtent(obounds);
281         },
282
283         addImageOverlay: function(id, src, opacity, west, south, east, north, oContext) {
284             var map = this.maps[this.api];
285
286             // TODO: Add provider code
287         },
288
289         setImagePosition: function(id, oContext) {
290             var map = this.maps[this.api];
291             var topLeftPoint; var bottomRightPoint;
292
293             // TODO: Add provider code
294
295             //oContext.pixels.top = ...;
296             //oContext.pixels.left = ...;
297             //oContext.pixels.bottom = ...;
298             //oContext.pixels.right = ...;
299         },
300
301         addOverlay: function(url, autoCenterAndZoom) {
302             var map = this.maps[this.api];
303
304             // TODO: Add provider code
305
306         },
307
308         addTileLayer: function(tile_url, opacity, copyright_text, min_zoom, max_zoom) {
309             var map = this.maps[this.api];
310
311             // TODO: Add provider code
312         },
313
314         toggleTileLayer: function(tile_url) {
315             var map = this.maps[this.api];
316
317             // TODO: Add provider code
318         },
319
320         getPixelRatio: function() {
321             var map = this.maps[this.api];
322
323             // TODO: Add provider code
324         },
325
326         mousePosition: function(element) {
327             var map = this.maps[this.api];
328
329             // TODO: Add provider code
330         }
331     },
332
333     LatLonPoint: {
334
335         toProprietary: function() {
336             var ollon = this.lon * 20037508.34 / 180;
337             var ollat = Math.log(Math.tan((90 + this.lat) * Math.PI / 360)) / (Math.PI / 180);
338             ollat = ollat * 20037508.34 / 180;
339             return new OpenLayers.LonLat(ollon, ollat);
340         },
341
342         fromProprietary: function(olPoint) {
343             var lon = (olPoint.lon / 20037508.34) * 180;
344             var lat = (olPoint.lat / 20037508.34) * 180;
345             lat = 180/Math.PI * (2 * Math.atan(Math.exp(lat * Math.PI / 180)) - Math.PI / 2);
346             this.lon = lon;
347             this.lat = lat;
348         }
349
350     },
351
352     Marker: {
353
354         toProprietary: function() {
355             var size, anchor, icon;
356             if(this.iconSize) {
357                 size = new OpenLayers.Size(this.iconSize[0], this.iconSize[1]);
358             }
359             else {
360                 size = new OpenLayers.Size(21,25);
361             }
362
363             if(this.iconAnchor) {
364                 anchor = new OpenLayers.Pixel(this.iconAnchor[0], this.iconAnchor[1]);
365             }
366             else {
367                 // FIXME: hard-coding the anchor point
368                 anchor = new OpenLayers.Pixel(-(size.w/2), -size.h);
369             }
370
371             if(this.iconUrl) {
372                 icon = new OpenLayers.Icon(this.iconUrl, size, anchor);
373             }
374             else {
375                 icon = new OpenLayers.Icon('http://openlayers.org/dev/img/marker-gold.png', size, anchor);
376             }
377             var marker = new OpenLayers.Marker(this.location.toProprietary("openlayers"), icon);
378
379             if(this.infoBubble) {
380                 var popup = new OpenLayers.Popup(null,
381                     this.location.toProprietary("openlayers"),
382                     new OpenLayers.Size(100,100),
383                     this.infoBubble,
384                     true);
385                     popup.autoSize = true;
386                     var theMap = this.map;
387                     if(this.hover) {
388                         marker.events.register("mouseover", marker, function(event) {
389                             theMap.addPopup(popup);
390                             popup.show();
391                         });
392                         marker.events.register("mouseout", marker, function(event) {
393                             popup.hide();
394                             theMap.removePopup(popup);
395                         });
396                     }
397                     else {
398                         var shown = false;
399                         marker.events.register("mousedown", marker, function(event) {
400                             if (shown) {
401                                 popup.hide();
402                                 theMap.removePopup(popup);
403                                 shown = false;
404                             } else {
405                                 theMap.addPopup(popup);
406                                 popup.show();
407                                 shown = true;
408                             }
409                         });
410                     }
411                 }
412
413                 if(this.hoverIconUrl) {
414                     // TODO
415                 }
416
417                 if(this.infoDiv){
418                     // TODO
419                 }
420                 return marker;
421             },
422
423         openBubble: function() {
424             // TODO: Add provider code
425         },
426
427         hide: function() {
428             this.proprietary_marker.setOptions({visible:false});
429         },
430
431         show: function() {
432             this.proprietary_marker.setOptions({visible:true});
433         },
434
435         update: function() {
436             // TODO: Add provider code
437         }
438
439     },
440
441     Polyline: {
442
443         toProprietary: function() {
444             var olpolyline;
445             var olpoints = [];
446             var ring;
447             var style = {
448                 strokeColor: this.color || "#000000",
449                 strokeOpacity: this.opacity || 1,
450                 strokeWidth: this.width || 1,
451                 fillColor: this.fillColor || "#000000",
452                 fillOpacity: this.getAttribute('fillOpacity') || 0.2
453             };
454
455             //TODO Handle closed attribute
456
457             for (var i = 0, length = this.points.length ; i< length; i++){
458                 olpoint = this.points[i].toProprietary("openlayers");
459                 olpoints.push(new OpenLayers.Geometry.Point(olpoint.lon, olpoint.lat));
460             }
461
462             if (this.closed) {
463                 // a closed polygon
464                 ring = new OpenLayers.Geometry.LinearRing(olpoints);
465             } else {
466                 // a line
467                 ring = new OpenLayers.Geometry.LineString(olpoints);
468             }
469
470             olpolyline = new OpenLayers.Feature.Vector(ring, null, style);
471
472             return olpolyline;
473         },
474
475         show: function() {
476             throw 'Not implemented';
477         },
478
479         hide: function() {
480             throw 'Not implemented';
481         }
482
483     }
484
485 });