PageRenderTime 40ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 1ms

/lib/com/modestmaps/extras/MapCopyright.as

https://github.com/jenrios/Elastic-Lists
ActionScript | 279 lines | 214 code | 43 blank | 22 comment | 14 complexity | 0600124bea7278f3f92190552f5f6e34 MD5 | raw file
  1. package com.modestmaps.extras
  2. {
  3. import com.modestmaps.Map;
  4. import com.modestmaps.core.MapExtent;
  5. import com.modestmaps.events.MapEvent;
  6. import com.modestmaps.geo.Location;
  7. import flash.display.Sprite;
  8. import flash.external.ExternalInterface;
  9. import flash.text.TextField;
  10. import flash.text.TextFormat;
  11. import flash.text.TextFormatAlign;
  12. import flash.utils.clearTimeout;
  13. import flash.utils.setTimeout;
  14. /**
  15. * VERY EXPERIMENTAL, requires javascript, uses this technique:
  16. * http://www.actionscript.org/resources/articles/745/1/JavaScript-and-VBScript-Injection-in-ActionScript-3/Page1.html
  17. *
  18. * TODO: update spans for Microsoft, add Yahoo :)
  19. *
  20. * In general, lots of this needs reworking... at least, IMapProviders should be able to provide copyright strings
  21. * without requiring javascript, and without needing to edit this file.
  22. */
  23. [Event(name="copyrightChanged", type="com.modestmaps.events.MapEvent")]
  24. public class MapCopyright extends Sprite
  25. {
  26. private static const script_js:XML = <script>
  27. <![CDATA[
  28. function() {
  29. modestMaps = {
  30. swfId: 'replaceMe',
  31. setSwfId:
  32. function(id)
  33. {
  34. this.swfId = id;
  35. },
  36. copyright:
  37. function(provider, cenLat, cenLon, minLat, minLon, maxLat, maxLon, zoom)
  38. {
  39. switch(provider) {
  40. case 'BLUE_MARBLE':
  41. case 'DAILY_PLANET':
  42. document.getElementById(this.swfId).copyrightCallback('Image courtesy of NASA');
  43. break;
  44. case 'NATURAL_EARTH_III':
  45. document.getElementById(this.swfId).copyrightCallback('Public domain map from <a href=\'http://www.shadedrelief.com/natural3/pages/use.html\'>Tom Patterson, www.shadedrelief.com</a>');
  46. break;
  47. case 'OPEN_STREET_MAP':
  48. document.getElementById(this.swfId).copyrightCallback('Map data <a href=\'http://www.openstreetmap.org\'>CC-BY-SA OpenStreetMap.org</a>');
  49. break;
  50. case 'CLOUDMADE':
  51. document.getElementById(this.swfId).copyrightCallback('Map tiles &copy; <a href="http://www.cloudmade.com">CloudMade</a>. Map data <a href=\'http://www.openstreetmap.org\'>CC-BY-SA OpenStreetMap.org</a>');
  52. break;
  53. case 'MICROSOFT_ROAD':
  54. this.microsoft.copyright('road', minLat, minLon, maxLat, maxLon, zoom);
  55. break;
  56. case 'MICROSOFT_AERIAL':
  57. this.microsoft.copyright('aerial', minLat, minLon, maxLat, maxLon, zoom);
  58. break;
  59. case 'MICROSOFT_HYBRID':
  60. this.microsoft.copyright(undefined, minLat, minLon, maxLat, maxLon, zoom);
  61. break;
  62. case 'YAHOO_ROAD':
  63. case 'YAHOO_AERIAL':
  64. case 'YAHOO_HYBRID':
  65. case 'YAHOO_OVERLAY':
  66. document.getElementById(this.swfId).copyrightCallback('&copy; Yahoo');
  67. break;
  68. default:
  69. document.getElementById(this.swfId).copyrightCallback('Copyright owner not found.');
  70. break;
  71. }
  72. },
  73. microsoft: {
  74. holders:
  75. {'microsoft': '&copy; 2006 Microsoft Corporation',
  76. 'navteq': '&copy; 2006 NAVTEQ',
  77. 'and': '&copy; AND',
  78. 'mds': '&copy; 2006 MapData Sciences Pty Ltd',
  79. 'zenrin': '&copy; 2006 Zenrin',
  80. 'nasa': 'Image courtesy of NASA',
  81. 'harris': '&copy; Harris Corp, Earthstar Geographics LLC',
  82. 'usgs': 'Image courtesy of USGS',
  83. 'earthdata': '&copy; EarthData',
  84. 'getmap': '&copy; Getmapping plc',
  85. 'geoeye': '&copy; 2006 GeoEye',
  86. 'pasco': '&copy; 2005 Pasco'},
  87. // tract: [kind, holder, min zoom, max zoom, min lat, min lon, max lat, max lon]
  88. tracts:
  89. [['road', 'microsoft', 1, 20, -90, -180, 90, 180],
  90. ['road', 'navteq', 1, 9, -90, -180, 90, 180],
  91. ['road', 'navteq', 10, 19, 16, -180, 90, -50],
  92. ['road', 'navteq', 10, 19, 27, -32, 40, -13],
  93. ['road', 'navteq', 10, 19, 35, -11, 72, 20],
  94. ['road', 'navteq', 10, 19, 21, 20, 72, 32],
  95. ['road', 'navteq', 10, 17, 21.92, 113.14, 22.79, 114.52],
  96. ['road', 'navteq', 10, 17, 21.73, 119.7, 25.65, 122.39],
  97. ['road', 'navteq', 10, 17, 0, 98.7, 8, 120.17],
  98. ['road', 'navteq', 10, 17, 0.86, 103.2, 1.92, 104.45],
  99. ['road', 'and', 10, 19, -90, -180, 90, 180],
  100. ['road', 'mds', 5, 17, -45, 111, -9, 156],
  101. ['road', 'mds', 5, 17, -49.7, 164.42, -30.82, 180],
  102. ['road', 'zenrin', 4, 18, 23.5, 122.5, 46.65, 151.66],
  103. ['road', 'microsoft', 1, 20, -90, -180, 90, 180],
  104. ['aerial', 'nasa', 1, 8, -90, -180, 90, 180],
  105. ['aerial', 'harris', 9, 13, -90, -180, 90, 180],
  106. ['aerial', 'usgs', 14, 19, 17.99, -150.11, 61.39, -65.57],
  107. ['aerial', 'earthdata', 14, 19, 21.25, -158.3, 21.72, -157.64],
  108. ['aerial', 'earthdata', 14, 19, 39.99, -80.53, 40.87, -79.43],
  109. ['aerial', 'earthdata', 14, 19, 34.86, -90.27, 35.39, -89.6],
  110. ['aerial', 'earthdata', 14, 19, 40.6, -74.18, 41.37, -73.51],
  111. ['aerial', 'getmap', 14, 19, 49.94, -6.35, 58.71, 1.78],
  112. ['aerial', 'geoeye', 14, 17, 44.43, -63.75, 45.06, -63.45],
  113. ['aerial', 'geoeye', 14, 17, 45.39, -73.78, 45.66, -73.4],
  114. ['aerial', 'geoeye', 14, 17, 45.2, -75.92, 45.59, -75.55],
  115. ['aerial', 'geoeye', 14, 17, 42.95, -79.81, 44.06, -79.42],
  116. ['aerial', 'geoeye', 14, 17, 50.35, -114.26, 51.25, -113.82],
  117. ['aerial', 'geoeye', 14, 17, 48.96, -123.33, 49.54, -122.97],
  118. ['aerial', 'geoeye', 14, 17, -35.42, 138.32, -34.47, 139.07],
  119. ['aerial', 'geoeye', 14, 17, -32.64, 115.58, -32.38, 115.85],
  120. ['aerial', 'geoeye', 14, 17, -34.44, 150.17, -33.27, 151.49],
  121. ['aerial', 'geoeye', 14, 17, -28.3, 152.62, -26.94, 153.64],
  122. ['aerial', 'pasco', 14, 17, 23.5, 122.5, 46.65, 151.66]],
  123. copyright:
  124. function(kind, minLat, minLon, maxLat, maxLon, zoom)
  125. {
  126. var tracts = this.tracts;
  127. var holders = [];
  128. var matches = {};
  129. for(var i = 0; i < tracts.length; i += 1) {
  130. var tract = tracts[i];
  131. if((tract[0] == kind || !kind) && tract[2] <= zoom && zoom <= tract[3] && tract[4] <= maxLat && minLat <= tract[6] && tract[5] <= maxLon && minLon <= tract[7]) {
  132. matches[tract[1]] = true;
  133. }
  134. }
  135. for(var p in matches) {
  136. holders.push(this.holders[p]);
  137. }
  138. document.getElementById(modestMaps.swfId).copyrightCallback(holders.join(', '));
  139. }
  140. }
  141. };
  142. }
  143. ]]>
  144. </script>;
  145. private static var scriptAdded:Boolean = false;
  146. protected var map:Map;
  147. /** htmlText to be added to a label - listen for MapEvent.COPYRIGHT_CHANGED */
  148. public var copyright:String = "";
  149. public var copyrightField:TextField;
  150. protected var offsetX:Number=10;
  151. protected var offsetY:Number=10;
  152. public function MapCopyright(map:Map, offsetX:Number=10, offsetY:Number=10)
  153. {
  154. this.map = map;
  155. this.offsetX = offsetX;
  156. this.offsetY = offsetY;
  157. if (!scriptAdded) {
  158. try {
  159. ExternalInterface.call(script_js);
  160. ExternalInterface.call('modestMaps.setSwfId', ExternalInterface.objectID);
  161. ExternalInterface.addCallback("copyrightCallback", setCopyright);
  162. }
  163. catch (error:Error) {
  164. trace("problem adding setCopyright as callback in Map.as");
  165. trace(error.getStackTrace());
  166. }
  167. }
  168. map.addEventListener(MapEvent.STOP_ZOOMING, onMapChange);
  169. map.addEventListener(MapEvent.ZOOMED_BY, onMapChange);
  170. map.addEventListener(MapEvent.STOP_PANNING, onMapChange);
  171. map.addEventListener(MapEvent.PANNED, onMapChange);
  172. map.addEventListener(MapEvent.EXTENT_CHANGED, onMapChange);
  173. map.addEventListener(MapEvent.MAP_PROVIDER_CHANGED, onMapChange);
  174. map.addEventListener(MapEvent.RESIZED, onMapResized);
  175. copyrightField = new TextField();
  176. copyrightField.defaultTextFormat = new TextFormat('Arial', 10, 0x000000, false, null, null, null, '_blank');
  177. copyrightField.selectable = false;
  178. copyrightField.mouseEnabled = true;
  179. copyrightField.multiline = true;
  180. addChild(copyrightField);
  181. onMapChange(null);
  182. }
  183. protected var copyrightTimeout:uint;
  184. protected function onMapChange(event:MapEvent):void
  185. {
  186. if (copyrightTimeout) {
  187. clearTimeout(copyrightTimeout);
  188. }
  189. copyrightTimeout = setTimeout(callCopyright, 250);
  190. }
  191. protected function onMapResized(event:MapEvent):void
  192. {
  193. copyrightField.x = map.getWidth() - copyrightField.width - offsetX;
  194. copyrightField.y = map.getHeight() - copyrightField.height - offsetY;
  195. }
  196. /**
  197. * Call javascript:modestMaps.copyright() with details about current view.
  198. * See js/copyright.js.
  199. */
  200. protected function callCopyright():void
  201. {
  202. if (copyrightTimeout) {
  203. clearTimeout(copyrightTimeout);
  204. }
  205. var extent:MapExtent = map.getExtent();
  206. var cenL:Location = extent.center;
  207. var minL:Location = extent.southEast;
  208. var maxL:Location = extent.northWest;
  209. var minLat:Number = Math.min(minL.lat, maxL.lat);
  210. var minLon:Number = Math.min(minL.lon, maxL.lon);
  211. var maxLat:Number = Math.max(minL.lat, maxL.lat);
  212. var maxLon:Number = Math.max(minL.lon, maxL.lon);
  213. try {
  214. ExternalInterface.call("modestMaps.copyright", map.getMapProvider().toString(), cenL.lat, cenL.lon, minLat, minLon, maxLat, maxLon, map.getZoom());
  215. }
  216. catch (error:Error) {
  217. //trace("problem setting copyright in Map.as");
  218. //trace(error.getStackTrace());
  219. }
  220. }
  221. /** this function gets exposed to javascript as a callback
  222. *
  223. * to display the copyright string in your flash piece, you then need to listen for
  224. * the COPYRIGHT_CHANGED MapEvent, or add MapCopyright as a child of map for a basic implementation.
  225. */
  226. public function setCopyright(copyright:String):void {
  227. this.copyright = copyright;
  228. this.copyright = this.copyright.replace(/&copy;/g,"©");
  229. copyrightField.htmlText = this.copyright;
  230. copyrightField.width = copyrightField.textWidth + 4;
  231. copyrightField.height = copyrightField.textHeight + 4;
  232. onMapResized(null);
  233. dispatchEvent(new MapEvent(MapEvent.COPYRIGHT_CHANGED, this.copyright));
  234. }
  235. }
  236. }