mxn.openlayers.core.js 16 KB

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