mxn.openlayers.core.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  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. // switch(type) {
  224. // case mxn.Mapstraction.ROAD:
  225. // map.setMapTypeId(google.maps.MapTypeId.ROADMAP);
  226. // break;
  227. // case mxn.Mapstraction.SATELLITE:
  228. // map.setMapTypeId(google.maps.MapTypeId.SATELLITE);
  229. // break;
  230. // case mxn.Mapstraction.HYBRID:
  231. // map.setMapTypeId(google.maps.MapTypeId.HYBRID);
  232. // break;
  233. // default:
  234. // map.setMapTypeId(google.maps.MapTypeId.ROADMAP);
  235. // }
  236. },
  237. getMapType: function() {
  238. var map = this.maps[this.api];
  239. // TODO: implement actual layer support
  240. return mxn.Mapstraction.ROAD;
  241. // var type = map.getMapTypeId();
  242. // switch(type) {
  243. // case google.maps.MapTypeId.ROADMAP:
  244. // return mxn.Mapstraction.ROAD;
  245. // case google.maps.MapTypeId.SATELLITE:
  246. // return mxn.Mapstraction.SATELLITE;
  247. // case google.maps.MapTypeId.HYBRID:
  248. // return mxn.Mapstraction.HYBRID;
  249. // //case google.maps.MapTypeId.TERRAIN:
  250. // // return something;
  251. // default:
  252. // return null;
  253. // }
  254. },
  255. getBounds: function () {
  256. var map = this.maps[this.api];
  257. var olbox = map.calculateBounds();
  258. return new mxn.BoundingBox(olbox.bottom, olbox.left, olbox.top, olbox.right);
  259. },
  260. setBounds: function(bounds){
  261. var map = this.maps[this.api];
  262. var sw = bounds.getSouthWest();
  263. var ne = bounds.getNorthEast();
  264. if(sw.lon > ne.lon) {
  265. sw.lon -= 360;
  266. }
  267. var obounds = new OpenLayers.Bounds();
  268. obounds.extend(new mxn.LatLonPoint(sw.lat,sw.lon).toProprietary(this.api));
  269. obounds.extend(new mxn.LatLonPoint(ne.lat,ne.lon).toProprietary(this.api));
  270. map.zoomToExtent(obounds);
  271. },
  272. addImageOverlay: function(id, src, opacity, west, south, east, north, oContext) {
  273. var map = this.maps[this.api];
  274. // TODO: Add provider code
  275. },
  276. setImagePosition: function(id, oContext) {
  277. var map = this.maps[this.api];
  278. var topLeftPoint; var bottomRightPoint;
  279. // TODO: Add provider code
  280. //oContext.pixels.top = ...;
  281. //oContext.pixels.left = ...;
  282. //oContext.pixels.bottom = ...;
  283. //oContext.pixels.right = ...;
  284. },
  285. addOverlay: function(url, autoCenterAndZoom) {
  286. var map = this.maps[this.api];
  287. // TODO: Add provider code
  288. },
  289. addTileLayer: function(tile_url, opacity, copyright_text, min_zoom, max_zoom) {
  290. var map = this.maps[this.api];
  291. // TODO: Add provider code
  292. },
  293. toggleTileLayer: function(tile_url) {
  294. var map = this.maps[this.api];
  295. // TODO: Add provider code
  296. },
  297. getPixelRatio: function() {
  298. var map = this.maps[this.api];
  299. // TODO: Add provider code
  300. },
  301. mousePosition: function(element) {
  302. var map = this.maps[this.api];
  303. // TODO: Add provider code
  304. }
  305. },
  306. LatLonPoint: {
  307. toProprietary: function() {
  308. var ollon = this.lon * 20037508.34 / 180;
  309. var ollat = Math.log(Math.tan((90 + this.lat) * Math.PI / 360)) / (Math.PI / 180);
  310. ollat = ollat * 20037508.34 / 180;
  311. return new OpenLayers.LonLat(ollon, ollat);
  312. },
  313. fromProprietary: function(olPoint) {
  314. var lon = (olPoint.lon / 20037508.34) * 180;
  315. var lat = (olPoint.lat / 20037508.34) * 180;
  316. lat = 180/Math.PI * (2 * Math.atan(Math.exp(lat * Math.PI / 180)) - Math.PI / 2);
  317. this.lon = lon;
  318. this.lat = lat;
  319. }
  320. },
  321. Marker: {
  322. toProprietary: function() {
  323. var size, anchor, icon;
  324. if(this.iconSize) {
  325. size = new OpenLayers.Size(this.iconSize[0], this.iconSize[1]);
  326. }
  327. else {
  328. size = new OpenLayers.Size(21,25);
  329. }
  330. if(this.iconAnchor) {
  331. anchor = new OpenLayers.Pixel(this.iconAnchor[0], this.iconAnchor[1]);
  332. }
  333. else {
  334. // FIXME: hard-coding the anchor point
  335. anchor = new OpenLayers.Pixel(-(size.w/2), -size.h);
  336. }
  337. if(this.iconUrl) {
  338. icon = new OpenLayers.Icon(this.iconUrl, size, anchor);
  339. }
  340. else {
  341. icon = new OpenLayers.Icon('http://openlayers.org/dev/img/marker-gold.png', size, anchor);
  342. }
  343. var marker = new OpenLayers.Marker(this.location.toProprietary("openlayers"), icon);
  344. if(this.infoBubble) {
  345. var popup = new OpenLayers.Popup(null,
  346. this.location.toProprietary("openlayers"),
  347. new OpenLayers.Size(100,100),
  348. this.infoBubble,
  349. true);
  350. popup.autoSize = true;
  351. var theMap = this.map;
  352. if(this.hover) {
  353. marker.events.register("mouseover", marker, function(event) {
  354. theMap.addPopup(popup);
  355. popup.show();
  356. });
  357. marker.events.register("mouseout", marker, function(event) {
  358. popup.hide();
  359. theMap.removePopup(popup);
  360. });
  361. }
  362. else {
  363. var shown = false;
  364. marker.events.register("mousedown", marker, function(event) {
  365. if (shown) {
  366. popup.hide();
  367. theMap.removePopup(popup);
  368. shown = false;
  369. } else {
  370. theMap.addPopup(popup);
  371. popup.show();
  372. shown = true;
  373. }
  374. });
  375. }
  376. }
  377. if(this.hoverIconUrl) {
  378. // TODO
  379. }
  380. if(this.infoDiv){
  381. // TODO
  382. }
  383. return marker;
  384. },
  385. openBubble: function() {
  386. // TODO: Add provider code
  387. },
  388. hide: function() {
  389. this.proprietary_marker.setOptions({visible:false});
  390. },
  391. show: function() {
  392. this.proprietary_marker.setOptions({visible:true});
  393. },
  394. update: function() {
  395. // TODO: Add provider code
  396. }
  397. },
  398. Polyline: {
  399. toProprietary: function() {
  400. var olpolyline;
  401. var olpoints = [];
  402. var ring;
  403. var style = {
  404. strokeColor: this.color || "#000000",
  405. strokeOpacity: this.opacity || 1,
  406. strokeWidth: this.width || 1,
  407. fillColor: this.fillColor || "#000000",
  408. fillOpacity: this.getAttribute('fillOpacity') || 0.2
  409. };
  410. //TODO Handle closed attribute
  411. for (var i = 0, length = this.points.length ; i< length; i++){
  412. olpoint = this.points[i].toProprietary("openlayers");
  413. olpoints.push(new OpenLayers.Geometry.Point(olpoint.lon, olpoint.lat));
  414. }
  415. if (this.closed) {
  416. // a closed polygon
  417. ring = new OpenLayers.Geometry.LinearRing(olpoints);
  418. } else {
  419. // a line
  420. ring = new OpenLayers.Geometry.LineString(olpoints);
  421. }
  422. olpolyline = new OpenLayers.Feature.Vector(ring, null, style);
  423. return olpolyline;
  424. },
  425. show: function() {
  426. throw 'Not implemented';
  427. },
  428. hide: function() {
  429. throw 'Not implemented';
  430. }
  431. }
  432. });