mxn.cloudmade.core.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. mxn.register('cloudmade', {
  2. Mapstraction: {
  3. init: function(element, api) {
  4. var me = this;
  5. var cloudmade = new CM.Tiles.CloudMade.Web({key: cloudmade_key});
  6. this.maps[api] = new CM.Map(element, cloudmade);
  7. this.loaded[api] = true;
  8. CM.Event.addListener(this.maps[api], 'click', function(location,marker) {
  9. if ( marker && marker.mapstraction_marker ) {
  10. marker.mapstraction_marker.click.fire();
  11. }
  12. else if ( location ) {
  13. me.click.fire({'location': new mxn.LatLonPoint(location.lat(), location.lng())});
  14. }
  15. // If the user puts their own markers directly on the map
  16. // then there is no location and this event should not fire.
  17. if ( location ) {
  18. me.clickHandler(location.lat(),location.lng(),location,me);
  19. }
  20. });
  21. },
  22. applyOptions: function(){
  23. var map = this.maps[this.api];
  24. if(this.options.enableScrollWheelZoom){
  25. map.enableScrollWheelZoom();
  26. }
  27. },
  28. resizeTo: function(width, height){
  29. this.maps[this.api].checkResize();
  30. },
  31. addControls: function( args ) {
  32. var map = this.maps[this.api];
  33. var c = this.addControlsArgs;
  34. switch (c.zoom) {
  35. case 'large':
  36. this.addLargeControls();
  37. break;
  38. case 'small':
  39. this.addSmallControls();
  40. break;
  41. }
  42. if (c.map_type) {
  43. this.addMapTypeControls();
  44. }
  45. if (c.scale) {
  46. map.addControl(new CM.ScaleControl());
  47. this.addControlsArgs.scale = true;
  48. }
  49. },
  50. addSmallControls: function() {
  51. var map = this.maps[this.api];
  52. map.addControl(new CM.SmallMapControl());
  53. this.addControlsArgs.zoom = 'small';
  54. },
  55. addLargeControls: function() {
  56. var map = this.maps[this.api];
  57. map.addControl(new CM.LargeMapControl());
  58. this.addControlsArgs.zoom = 'large';
  59. },
  60. addMapTypeControls: function() {
  61. var map = this.maps[this.api];
  62. map.addControl(new CM.TileLayerControl());
  63. this.addControlsArgs.map_type = true;
  64. },
  65. dragging: function(on) {
  66. var map = this.maps[this.api];
  67. if (on) {
  68. map.enableDragging();
  69. } else {
  70. map.disableDragging();
  71. }
  72. },
  73. setCenterAndZoom: function(point, zoom) {
  74. var map = this.maps[this.api];
  75. var pt = point.toProprietary(this.api);
  76. map.setCenter(pt, zoom);
  77. },
  78. addMarker: function(marker, old) {
  79. var map = this.maps[this.api];
  80. var pin = marker.toProprietary(this.api);
  81. map.addOverlay(pin);
  82. return pin;
  83. },
  84. removeMarker: function(marker) {
  85. var map = this.maps[this.api];
  86. marker.proprietary_marker.closeInfoWindow();
  87. map.removeOverlay(marker.proprietary_marker);
  88. },
  89. removeAllMarkers: function() {
  90. // Done in mxn.core.js
  91. },
  92. declutterMarkers: function(opts) {
  93. var map = this.maps[this.api];
  94. // TODO: Add provider code
  95. },
  96. addPolyline: function(polyline, old) {
  97. var map = this.maps[this.api];
  98. var pl = polyline.toProprietary(this.api);
  99. map.addOverlay(pl);
  100. return pl;
  101. },
  102. removePolyline: function(polyline) {
  103. var map = this.maps[this.api];
  104. map.removeOverlay(polyline.proprietary_polyline);
  105. },
  106. getCenter: function() {
  107. var map = this.maps[this.api];
  108. var pt = map.getCenter();
  109. return new mxn.LatLonPoint(pt.lat(), pt.lng());
  110. },
  111. setCenter: function(point, options) {
  112. var map = this.maps[this.api];
  113. var pt = point.toProprietary(this.api);
  114. if(options !== null && options.pan) { map.panTo(pt); }
  115. else { map.setCenter(pt); }
  116. },
  117. setZoom: function(zoom) {
  118. var map = this.maps[this.api];
  119. map.setZoom(zoom);
  120. },
  121. getZoom: function() {
  122. var map = this.maps[this.api];
  123. return map.getZoom();
  124. },
  125. getZoomLevelForBoundingBox: function( bbox ) {
  126. var map = this.maps[this.api];
  127. // NE and SW points from the bounding box.
  128. var ne = bbox.getNorthEast();
  129. var sw = bbox.getSouthWest();
  130. var zoom = map.getBoundsZoomLevel(new CM.LatLngBounds(sw.toProprietary(this.api), ne.toProprietary(this.api)));
  131. return zoom;
  132. },
  133. setMapType: function(type) {
  134. var map = this.maps[this.api];
  135. // TODO: Are there any MapTypes for Cloudmade?
  136. switch(type) {
  137. case mxn.Mapstraction.ROAD:
  138. // TODO: Add provider code
  139. break;
  140. case mxn.Mapstraction.SATELLITE:
  141. // TODO: Add provider code
  142. break;
  143. case mxn.Mapstraction.HYBRID:
  144. // TODO: Add provider code
  145. break;
  146. default:
  147. // TODO: Add provider code
  148. }
  149. },
  150. getMapType: function() {
  151. var map = this.maps[this.api];
  152. // TODO: Are there any MapTypes for Cloudmade?
  153. return mxn.Mapstraction.ROAD;
  154. //return mxn.Mapstraction.SATELLITE;
  155. //return mxn.Mapstraction.HYBRID;
  156. },
  157. getBounds: function () {
  158. var map = this.maps[this.api];
  159. var box = map.getBounds();
  160. var sw = box.getSouthWest();
  161. var ne = box.getNorthEast();
  162. return new mxn.BoundingBox(sw.lat(), sw.lng(), ne.lat(), ne.lng());
  163. },
  164. setBounds: function(bounds){
  165. var map = this.maps[this.api];
  166. var sw = bounds.getSouthWest();
  167. var ne = bounds.getNorthEast();
  168. map.zoomToBounds(new CM.LatLngBounds(sw.toProprietary(this.api), ne.toProprietary(this.api)));
  169. },
  170. addImageOverlay: function(id, src, opacity, west, south, east, north, oContext) {
  171. var map = this.maps[this.api];
  172. // TODO: Add provider code
  173. },
  174. setImagePosition: function(id, oContext) {
  175. var map = this.maps[this.api];
  176. var topLeftPoint; var bottomRightPoint;
  177. // TODO: Add provider code
  178. },
  179. addOverlay: function(url, autoCenterAndZoom) {
  180. var map = this.maps[this.api];
  181. // TODO: Add provider code
  182. },
  183. addTileLayer: function(tile_url, opacity, copyright_text, min_zoom, max_zoom) {
  184. var map = this.maps[this.api];
  185. // TODO: Add provider code
  186. },
  187. toggleTileLayer: function(tile_url) {
  188. var map = this.maps[this.api];
  189. // TODO: Add provider code
  190. },
  191. getPixelRatio: function() {
  192. var map = this.maps[this.api];
  193. // TODO: Add provider code
  194. },
  195. mousePosition: function(element) {
  196. var map = this.maps[this.api];
  197. // TODO: Add provider code
  198. }
  199. },
  200. LatLonPoint: {
  201. toProprietary: function() {
  202. var cll = new CM.LatLng(this.lat,this.lon);
  203. return cll;
  204. },
  205. fromProprietary: function(point) {
  206. return new mxn.LatLonPoint(point.lat(),point.lng());
  207. }
  208. },
  209. Marker: {
  210. toProprietary: function() {
  211. var pt = this.location.toProprietary(this.api);
  212. var options = {};
  213. if (this.iconUrl) {
  214. var cicon = new CM.Icon();
  215. cicon.image = this.iconUrl;
  216. if (this.iconSize) {
  217. cicon.iconSize = new CM.Size(this.iconSize[0], this.iconSize[1]);
  218. if (this.iconAnchor) {
  219. cicon.iconAnchor = new CM.Point(this.iconAnchor[0], this.iconAnchor[1]);
  220. }
  221. }
  222. if (this.iconShadowUrl) {
  223. cicon.shadow = this.iconShadowUrl;
  224. if (this.iconShadowSize) {
  225. cicon.shadowSize = new CM.Size(this.iconShadowSize[0], this.iconShadowSize[1]);
  226. }
  227. }
  228. options.icon = cicon;
  229. }
  230. if (this.labelText) {
  231. options.title = this.labelText;
  232. }
  233. var cmarker = new CM.Marker(pt, options);
  234. if (this.infoBubble) {
  235. cmarker.bindInfoWindow(this.infoBubble);
  236. }
  237. return cmarker;
  238. },
  239. openBubble: function() {
  240. var pin = this.proprietary_marker;
  241. pin.openInfoWindow(this.infoBubble);
  242. },
  243. hide: function() {
  244. var pin = this.proprietary_marker;
  245. pin.hide();
  246. },
  247. show: function() {
  248. var pin = this.proprietary_marker;
  249. pin.show();
  250. },
  251. update: function() {
  252. // TODO: Add provider code
  253. }
  254. },
  255. Polyline: {
  256. toProprietary: function() {
  257. var pts = [];
  258. var poly;
  259. for (var i = 0, length = this.points.length ; i< length; i++){
  260. pts.push(this.points[i].toProprietary(this.api));
  261. }
  262. if (this.closed || pts[0].equals(pts[pts.length-1])) {
  263. poly = new CM.Polygon(pts, this.color, this.width, this.opacity, this.fillColor || "#5462E3", this.opacity || "0.3");
  264. } else {
  265. poly = new CM.Polyline(pts, this.color, this.width, this.opacity);
  266. }
  267. return poly;
  268. },
  269. show: function() {
  270. this.proprietary_polyline.show();
  271. },
  272. hide: function() {
  273. this.proprietary_polyline.hide();
  274. }
  275. }
  276. });