OverviewMap.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750
  1. /* Copyright (c) 2006-2013 by OpenLayers Contributors (see authors.txt for
  2. * full list of contributors). Published under the 2-clause BSD license.
  3. * See license.txt in the OpenLayers distribution or repository for the
  4. * full text of the license. */
  5. /**
  6. * @requires OpenLayers/Control.js
  7. * @requires OpenLayers/BaseTypes.js
  8. * @requires OpenLayers/Events/buttonclick.js
  9. * @requires OpenLayers/Map.js
  10. * @requires OpenLayers/Handler/Click.js
  11. * @requires OpenLayers/Handler/Drag.js
  12. */
  13. /**
  14. * Class: OpenLayers.Control.OverviewMap
  15. * The OverMap control creates a small overview map, useful to display the
  16. * extent of a zoomed map and your main map and provide additional
  17. * navigation options to the User. By default the overview map is drawn in
  18. * the lower right corner of the main map. Create a new overview map with the
  19. * <OpenLayers.Control.OverviewMap> constructor.
  20. *
  21. * Inherits from:
  22. * - <OpenLayers.Control>
  23. */
  24. OpenLayers.Control.OverviewMap = OpenLayers.Class(OpenLayers.Control, {
  25. /**
  26. * Property: element
  27. * {DOMElement} The DOM element that contains the overview map
  28. */
  29. element: null,
  30. /**
  31. * APIProperty: ovmap
  32. * {<OpenLayers.Map>} A reference to the overview map itself.
  33. */
  34. ovmap: null,
  35. /**
  36. * APIProperty: size
  37. * {<OpenLayers.Size>} The overvew map size in pixels. Note that this is
  38. * the size of the map itself - the element that contains the map (default
  39. * class name olControlOverviewMapElement) may have padding or other style
  40. * attributes added via CSS.
  41. */
  42. size: {w: 180, h: 90},
  43. /**
  44. * APIProperty: layers
  45. * {Array(<OpenLayers.Layer>)} Ordered list of layers in the overview map.
  46. * If none are sent at construction, the base layer for the main map is used.
  47. */
  48. layers: null,
  49. /**
  50. * APIProperty: minRectSize
  51. * {Integer} The minimum width or height (in pixels) of the extent
  52. * rectangle on the overview map. When the extent rectangle reaches
  53. * this size, it will be replaced depending on the value of the
  54. * <minRectDisplayClass> property. Default is 15 pixels.
  55. */
  56. minRectSize: 15,
  57. /**
  58. * APIProperty: minRectDisplayClass
  59. * {String} Replacement style class name for the extent rectangle when
  60. * <minRectSize> is reached. This string will be suffixed on to the
  61. * displayClass. Default is "RectReplacement".
  62. *
  63. * Example CSS declaration:
  64. * (code)
  65. * .olControlOverviewMapRectReplacement {
  66. * overflow: hidden;
  67. * cursor: move;
  68. * background-image: url("img/overview_replacement.gif");
  69. * background-repeat: no-repeat;
  70. * background-position: center;
  71. * }
  72. * (end)
  73. */
  74. minRectDisplayClass: "RectReplacement",
  75. /**
  76. * APIProperty: minRatio
  77. * {Float} The ratio of the overview map resolution to the main map
  78. * resolution at which to zoom farther out on the overview map.
  79. */
  80. minRatio: 8,
  81. /**
  82. * APIProperty: maxRatio
  83. * {Float} The ratio of the overview map resolution to the main map
  84. * resolution at which to zoom farther in on the overview map.
  85. */
  86. maxRatio: 32,
  87. /**
  88. * APIProperty: mapOptions
  89. * {Object} An object containing any non-default properties to be sent to
  90. * the overview map's map constructor. These should include any
  91. * non-default options that the main map was constructed with.
  92. */
  93. mapOptions: null,
  94. /**
  95. * APIProperty: autoPan
  96. * {Boolean} Always pan the overview map, so the extent marker remains in
  97. * the center. Default is false. If true, when you drag the extent
  98. * marker, the overview map will update itself so the marker returns
  99. * to the center.
  100. */
  101. autoPan: false,
  102. /**
  103. * Property: handlers
  104. * {Object}
  105. */
  106. handlers: null,
  107. /**
  108. * Property: resolutionFactor
  109. * {Object}
  110. */
  111. resolutionFactor: 1,
  112. /**
  113. * APIProperty: maximized
  114. * {Boolean} Start as maximized (visible). Defaults to false.
  115. */
  116. maximized: false,
  117. /**
  118. * APIProperty: maximizeTitle
  119. * {String} This property is used for showing a tooltip over the
  120. * maximize div. Defaults to "" (no title).
  121. */
  122. maximizeTitle: "",
  123. /**
  124. * APIProperty: minimizeTitle
  125. * {String} This property is used for showing a tooltip over the
  126. * minimize div. Defaults to "" (no title).
  127. */
  128. minimizeTitle: "",
  129. /**
  130. * Constructor: OpenLayers.Control.OverviewMap
  131. * Create a new overview map
  132. *
  133. * Parameters:
  134. * options - {Object} Properties of this object will be set on the overview
  135. * map object. Note, to set options on the map object contained in this
  136. * control, set <mapOptions> as one of the options properties.
  137. */
  138. initialize: function(options) {
  139. this.layers = [];
  140. this.handlers = {};
  141. OpenLayers.Control.prototype.initialize.apply(this, [options]);
  142. },
  143. /**
  144. * APIMethod: destroy
  145. * Deconstruct the control
  146. */
  147. destroy: function() {
  148. if (!this.mapDiv) { // we've already been destroyed
  149. return;
  150. }
  151. if (this.handlers.click) {
  152. this.handlers.click.destroy();
  153. }
  154. if (this.handlers.drag) {
  155. this.handlers.drag.destroy();
  156. }
  157. this.ovmap && this.ovmap.viewPortDiv.removeChild(this.extentRectangle);
  158. this.extentRectangle = null;
  159. if (this.rectEvents) {
  160. this.rectEvents.destroy();
  161. this.rectEvents = null;
  162. }
  163. if (this.ovmap) {
  164. this.ovmap.destroy();
  165. this.ovmap = null;
  166. }
  167. this.element.removeChild(this.mapDiv);
  168. this.mapDiv = null;
  169. this.div.removeChild(this.element);
  170. this.element = null;
  171. if (this.maximizeDiv) {
  172. this.div.removeChild(this.maximizeDiv);
  173. this.maximizeDiv = null;
  174. }
  175. if (this.minimizeDiv) {
  176. this.div.removeChild(this.minimizeDiv);
  177. this.minimizeDiv = null;
  178. }
  179. this.map.events.un({
  180. buttonclick: this.onButtonClick,
  181. moveend: this.update,
  182. changebaselayer: this.baseLayerDraw,
  183. scope: this
  184. });
  185. OpenLayers.Control.prototype.destroy.apply(this, arguments);
  186. },
  187. /**
  188. * Method: draw
  189. * Render the control in the browser.
  190. */
  191. draw: function() {
  192. OpenLayers.Control.prototype.draw.apply(this, arguments);
  193. if (this.layers.length === 0) {
  194. if (this.map.baseLayer) {
  195. var layer = this.map.baseLayer.clone();
  196. this.layers = [layer];
  197. } else {
  198. this.map.events.register("changebaselayer", this, this.baseLayerDraw);
  199. return this.div;
  200. }
  201. }
  202. // create overview map DOM elements
  203. this.element = document.createElement('div');
  204. this.element.className = this.displayClass + 'Element';
  205. this.element.style.display = 'none';
  206. this.mapDiv = document.createElement('div');
  207. this.mapDiv.style.width = this.size.w + 'px';
  208. this.mapDiv.style.height = this.size.h + 'px';
  209. this.mapDiv.style.position = 'relative';
  210. this.mapDiv.style.overflow = 'hidden';
  211. this.mapDiv.id = OpenLayers.Util.createUniqueID('overviewMap');
  212. this.extentRectangle = document.createElement('div');
  213. this.extentRectangle.style.position = 'absolute';
  214. this.extentRectangle.style.zIndex = 1000; //HACK
  215. this.extentRectangle.className = this.displayClass+'ExtentRectangle';
  216. this.element.appendChild(this.mapDiv);
  217. this.div.appendChild(this.element);
  218. // Optionally add min/max buttons if the control will go in the
  219. // map viewport.
  220. if(!this.outsideViewport) {
  221. this.div.className += " " + this.displayClass + 'Container';
  222. // maximize button div
  223. var img = OpenLayers.Util.getImageLocation('layer-switcher-maximize.png');
  224. this.maximizeDiv = OpenLayers.Util.createAlphaImageDiv(
  225. this.displayClass + 'MaximizeButton',
  226. null,
  227. null,
  228. img,
  229. 'absolute');
  230. this.maximizeDiv.style.display = 'none';
  231. this.maximizeDiv.className = this.displayClass + 'MaximizeButton olButton';
  232. if (this.maximizeTitle) {
  233. this.maximizeDiv.title = this.maximizeTitle;
  234. }
  235. this.div.appendChild(this.maximizeDiv);
  236. // minimize button div
  237. var img = OpenLayers.Util.getImageLocation('layer-switcher-minimize.png');
  238. this.minimizeDiv = OpenLayers.Util.createAlphaImageDiv(
  239. 'OpenLayers_Control_minimizeDiv',
  240. null,
  241. null,
  242. img,
  243. 'absolute');
  244. this.minimizeDiv.style.display = 'none';
  245. this.minimizeDiv.className = this.displayClass + 'MinimizeButton olButton';
  246. if (this.minimizeTitle) {
  247. this.minimizeDiv.title = this.minimizeTitle;
  248. }
  249. this.div.appendChild(this.minimizeDiv);
  250. this.minimizeControl();
  251. } else {
  252. // show the overview map
  253. this.element.style.display = '';
  254. }
  255. if(this.map.getExtent()) {
  256. this.update();
  257. }
  258. this.map.events.on({
  259. buttonclick: this.onButtonClick,
  260. moveend: this.update,
  261. scope: this
  262. });
  263. if (this.maximized) {
  264. this.maximizeControl();
  265. }
  266. return this.div;
  267. },
  268. /**
  269. * Method: baseLayerDraw
  270. * Draw the base layer - called if unable to complete in the initial draw
  271. */
  272. baseLayerDraw: function() {
  273. this.draw();
  274. this.map.events.unregister("changebaselayer", this, this.baseLayerDraw);
  275. },
  276. /**
  277. * Method: rectDrag
  278. * Handle extent rectangle drag
  279. *
  280. * Parameters:
  281. * px - {<OpenLayers.Pixel>} The pixel location of the drag.
  282. */
  283. rectDrag: function(px) {
  284. var deltaX = this.handlers.drag.last.x - px.x;
  285. var deltaY = this.handlers.drag.last.y - px.y;
  286. if(deltaX != 0 || deltaY != 0) {
  287. var rectTop = this.rectPxBounds.top;
  288. var rectLeft = this.rectPxBounds.left;
  289. var rectHeight = Math.abs(this.rectPxBounds.getHeight());
  290. var rectWidth = this.rectPxBounds.getWidth();
  291. // don't allow dragging off of parent element
  292. var newTop = Math.max(0, (rectTop - deltaY));
  293. newTop = Math.min(newTop,
  294. this.ovmap.size.h - this.hComp - rectHeight);
  295. var newLeft = Math.max(0, (rectLeft - deltaX));
  296. newLeft = Math.min(newLeft,
  297. this.ovmap.size.w - this.wComp - rectWidth);
  298. this.setRectPxBounds(new OpenLayers.Bounds(newLeft,
  299. newTop + rectHeight,
  300. newLeft + rectWidth,
  301. newTop));
  302. }
  303. },
  304. /**
  305. * Method: mapDivClick
  306. * Handle browser events
  307. *
  308. * Parameters:
  309. * evt - {<OpenLayers.Event>} evt
  310. */
  311. mapDivClick: function(evt) {
  312. var pxCenter = this.rectPxBounds.getCenterPixel();
  313. var deltaX = evt.xy.x - pxCenter.x;
  314. var deltaY = evt.xy.y - pxCenter.y;
  315. var top = this.rectPxBounds.top;
  316. var left = this.rectPxBounds.left;
  317. var height = Math.abs(this.rectPxBounds.getHeight());
  318. var width = this.rectPxBounds.getWidth();
  319. var newTop = Math.max(0, (top + deltaY));
  320. newTop = Math.min(newTop, this.ovmap.size.h - height);
  321. var newLeft = Math.max(0, (left + deltaX));
  322. newLeft = Math.min(newLeft, this.ovmap.size.w - width);
  323. this.setRectPxBounds(new OpenLayers.Bounds(newLeft,
  324. newTop + height,
  325. newLeft + width,
  326. newTop));
  327. this.updateMapToRect();
  328. },
  329. /**
  330. * Method: onButtonClick
  331. *
  332. * Parameters:
  333. * evt - {Event}
  334. */
  335. onButtonClick: function(evt) {
  336. if (evt.buttonElement === this.minimizeDiv) {
  337. this.minimizeControl();
  338. } else if (evt.buttonElement === this.maximizeDiv) {
  339. this.maximizeControl();
  340. }
  341. },
  342. /**
  343. * Method: maximizeControl
  344. * Unhide the control. Called when the control is in the map viewport.
  345. *
  346. * Parameters:
  347. * e - {<OpenLayers.Event>}
  348. */
  349. maximizeControl: function(e) {
  350. this.element.style.display = '';
  351. this.showToggle(false);
  352. if (e != null) {
  353. OpenLayers.Event.stop(e);
  354. }
  355. },
  356. /**
  357. * Method: minimizeControl
  358. * Hide all the contents of the control, shrink the size,
  359. * add the maximize icon
  360. *
  361. * Parameters:
  362. * e - {<OpenLayers.Event>}
  363. */
  364. minimizeControl: function(e) {
  365. this.element.style.display = 'none';
  366. this.showToggle(true);
  367. if (e != null) {
  368. OpenLayers.Event.stop(e);
  369. }
  370. },
  371. /**
  372. * Method: showToggle
  373. * Hide/Show the toggle depending on whether the control is minimized
  374. *
  375. * Parameters:
  376. * minimize - {Boolean}
  377. */
  378. showToggle: function(minimize) {
  379. if (this.maximizeDiv) {
  380. this.maximizeDiv.style.display = minimize ? '' : 'none';
  381. }
  382. if (this.minimizeDiv) {
  383. this.minimizeDiv.style.display = minimize ? 'none' : '';
  384. }
  385. },
  386. /**
  387. * Method: update
  388. * Update the overview map after layers move.
  389. */
  390. update: function() {
  391. if(this.ovmap == null) {
  392. this.createMap();
  393. }
  394. if(this.autoPan || !this.isSuitableOverview()) {
  395. this.updateOverview();
  396. }
  397. // update extent rectangle
  398. this.updateRectToMap();
  399. },
  400. /**
  401. * Method: isSuitableOverview
  402. * Determines if the overview map is suitable given the extent and
  403. * resolution of the main map.
  404. */
  405. isSuitableOverview: function() {
  406. var mapExtent = this.map.getExtent();
  407. var maxExtent = this.map.getMaxExtent();
  408. var testExtent = new OpenLayers.Bounds(
  409. Math.max(mapExtent.left, maxExtent.left),
  410. Math.max(mapExtent.bottom, maxExtent.bottom),
  411. Math.min(mapExtent.right, maxExtent.right),
  412. Math.min(mapExtent.top, maxExtent.top));
  413. if (this.ovmap.getProjection() != this.map.getProjection()) {
  414. testExtent = testExtent.transform(
  415. this.map.getProjectionObject(),
  416. this.ovmap.getProjectionObject() );
  417. }
  418. var resRatio = this.ovmap.getResolution() / this.map.getResolution();
  419. return ((resRatio > this.minRatio) &&
  420. (resRatio <= this.maxRatio) &&
  421. (this.ovmap.getExtent().containsBounds(testExtent)));
  422. },
  423. /**
  424. * Method updateOverview
  425. * Called by <update> if <isSuitableOverview> returns true
  426. */
  427. updateOverview: function() {
  428. var mapRes = this.map.getResolution();
  429. var targetRes = this.ovmap.getResolution();
  430. var resRatio = targetRes / mapRes;
  431. if(resRatio > this.maxRatio) {
  432. // zoom in overview map
  433. targetRes = this.minRatio * mapRes;
  434. } else if(resRatio <= this.minRatio) {
  435. // zoom out overview map
  436. targetRes = this.maxRatio * mapRes;
  437. }
  438. var center;
  439. if (this.ovmap.getProjection() != this.map.getProjection()) {
  440. center = this.map.center.clone();
  441. center.transform(this.map.getProjectionObject(),
  442. this.ovmap.getProjectionObject() );
  443. } else {
  444. center = this.map.center;
  445. }
  446. this.ovmap.setCenter(center, this.ovmap.getZoomForResolution(
  447. targetRes * this.resolutionFactor));
  448. this.updateRectToMap();
  449. },
  450. /**
  451. * Method: createMap
  452. * Construct the map that this control contains
  453. */
  454. createMap: function() {
  455. // create the overview map
  456. var options = OpenLayers.Util.extend(
  457. {controls: [], maxResolution: 'auto',
  458. fallThrough: false}, this.mapOptions);
  459. this.ovmap = new OpenLayers.Map(this.mapDiv, options);
  460. this.ovmap.viewPortDiv.appendChild(this.extentRectangle);
  461. // prevent ovmap from being destroyed when the page unloads, because
  462. // the OverviewMap control has to do this (and does it).
  463. OpenLayers.Event.stopObserving(window, 'unload', this.ovmap.unloadDestroy);
  464. this.ovmap.addLayers(this.layers);
  465. this.ovmap.zoomToMaxExtent();
  466. // check extent rectangle border width
  467. this.wComp = parseInt(OpenLayers.Element.getStyle(this.extentRectangle,
  468. 'border-left-width')) +
  469. parseInt(OpenLayers.Element.getStyle(this.extentRectangle,
  470. 'border-right-width'));
  471. this.wComp = (this.wComp) ? this.wComp : 2;
  472. this.hComp = parseInt(OpenLayers.Element.getStyle(this.extentRectangle,
  473. 'border-top-width')) +
  474. parseInt(OpenLayers.Element.getStyle(this.extentRectangle,
  475. 'border-bottom-width'));
  476. this.hComp = (this.hComp) ? this.hComp : 2;
  477. this.handlers.drag = new OpenLayers.Handler.Drag(
  478. this, {move: this.rectDrag, done: this.updateMapToRect},
  479. {map: this.ovmap}
  480. );
  481. this.handlers.click = new OpenLayers.Handler.Click(
  482. this, {
  483. "click": this.mapDivClick
  484. },{
  485. "single": true, "double": false,
  486. "stopSingle": true, "stopDouble": true,
  487. "pixelTolerance": 1,
  488. map: this.ovmap
  489. }
  490. );
  491. this.handlers.click.activate();
  492. this.rectEvents = new OpenLayers.Events(this, this.extentRectangle,
  493. null, true);
  494. this.rectEvents.register("mouseover", this, function(e) {
  495. if(!this.handlers.drag.active && !this.map.dragging) {
  496. this.handlers.drag.activate();
  497. }
  498. });
  499. this.rectEvents.register("mouseout", this, function(e) {
  500. if(!this.handlers.drag.dragging) {
  501. this.handlers.drag.deactivate();
  502. }
  503. });
  504. if (this.ovmap.getProjection() != this.map.getProjection()) {
  505. var sourceUnits = this.map.getProjectionObject().getUnits() ||
  506. this.map.units || this.map.baseLayer.units;
  507. var targetUnits = this.ovmap.getProjectionObject().getUnits() ||
  508. this.ovmap.units || this.ovmap.baseLayer.units;
  509. this.resolutionFactor = sourceUnits && targetUnits ?
  510. OpenLayers.INCHES_PER_UNIT[sourceUnits] /
  511. OpenLayers.INCHES_PER_UNIT[targetUnits] : 1;
  512. }
  513. },
  514. /**
  515. * Method: updateRectToMap
  516. * Updates the extent rectangle position and size to match the map extent
  517. */
  518. updateRectToMap: function() {
  519. // If the projections differ we need to reproject
  520. var bounds;
  521. if (this.ovmap.getProjection() != this.map.getProjection()) {
  522. bounds = this.map.getExtent().transform(
  523. this.map.getProjectionObject(),
  524. this.ovmap.getProjectionObject() );
  525. } else {
  526. bounds = this.map.getExtent();
  527. }
  528. var pxBounds = this.getRectBoundsFromMapBounds(bounds);
  529. if (pxBounds) {
  530. this.setRectPxBounds(pxBounds);
  531. }
  532. },
  533. /**
  534. * Method: updateMapToRect
  535. * Updates the map extent to match the extent rectangle position and size
  536. */
  537. updateMapToRect: function() {
  538. var lonLatBounds = this.getMapBoundsFromRectBounds(this.rectPxBounds);
  539. if (this.ovmap.getProjection() != this.map.getProjection()) {
  540. lonLatBounds = lonLatBounds.transform(
  541. this.ovmap.getProjectionObject(),
  542. this.map.getProjectionObject() );
  543. }
  544. this.map.panTo(lonLatBounds.getCenterLonLat());
  545. },
  546. /**
  547. * Method: setRectPxBounds
  548. * Set extent rectangle pixel bounds.
  549. *
  550. * Parameters:
  551. * pxBounds - {<OpenLayers.Bounds>}
  552. */
  553. setRectPxBounds: function(pxBounds) {
  554. var top = Math.max(pxBounds.top, 0);
  555. var left = Math.max(pxBounds.left, 0);
  556. var bottom = Math.min(pxBounds.top + Math.abs(pxBounds.getHeight()),
  557. this.ovmap.size.h - this.hComp);
  558. var right = Math.min(pxBounds.left + pxBounds.getWidth(),
  559. this.ovmap.size.w - this.wComp);
  560. var width = Math.max(right - left, 0);
  561. var height = Math.max(bottom - top, 0);
  562. if(width < this.minRectSize || height < this.minRectSize) {
  563. this.extentRectangle.className = this.displayClass +
  564. this.minRectDisplayClass;
  565. var rLeft = left + (width / 2) - (this.minRectSize / 2);
  566. var rTop = top + (height / 2) - (this.minRectSize / 2);
  567. this.extentRectangle.style.top = Math.round(rTop) + 'px';
  568. this.extentRectangle.style.left = Math.round(rLeft) + 'px';
  569. this.extentRectangle.style.height = this.minRectSize + 'px';
  570. this.extentRectangle.style.width = this.minRectSize + 'px';
  571. } else {
  572. this.extentRectangle.className = this.displayClass +
  573. 'ExtentRectangle';
  574. this.extentRectangle.style.top = Math.round(top) + 'px';
  575. this.extentRectangle.style.left = Math.round(left) + 'px';
  576. this.extentRectangle.style.height = Math.round(height) + 'px';
  577. this.extentRectangle.style.width = Math.round(width) + 'px';
  578. }
  579. this.rectPxBounds = new OpenLayers.Bounds(
  580. Math.round(left), Math.round(bottom),
  581. Math.round(right), Math.round(top)
  582. );
  583. },
  584. /**
  585. * Method: getRectBoundsFromMapBounds
  586. * Get the rect bounds from the map bounds.
  587. *
  588. * Parameters:
  589. * lonLatBounds - {<OpenLayers.Bounds>}
  590. *
  591. * Returns:
  592. * {<OpenLayers.Bounds>}A bounds which is the passed-in map lon/lat extent
  593. * translated into pixel bounds for the overview map
  594. */
  595. getRectBoundsFromMapBounds: function(lonLatBounds) {
  596. var leftBottomPx = this.getOverviewPxFromLonLat({
  597. lon: lonLatBounds.left,
  598. lat: lonLatBounds.bottom
  599. });
  600. var rightTopPx = this.getOverviewPxFromLonLat({
  601. lon: lonLatBounds.right,
  602. lat: lonLatBounds.top
  603. });
  604. var bounds = null;
  605. if (leftBottomPx && rightTopPx) {
  606. bounds = new OpenLayers.Bounds(leftBottomPx.x, leftBottomPx.y,
  607. rightTopPx.x, rightTopPx.y);
  608. }
  609. return bounds;
  610. },
  611. /**
  612. * Method: getMapBoundsFromRectBounds
  613. * Get the map bounds from the rect bounds.
  614. *
  615. * Parameters:
  616. * pxBounds - {<OpenLayers.Bounds>}
  617. *
  618. * Returns:
  619. * {<OpenLayers.Bounds>} Bounds which is the passed-in overview rect bounds
  620. * translated into lon/lat bounds for the overview map
  621. */
  622. getMapBoundsFromRectBounds: function(pxBounds) {
  623. var leftBottomLonLat = this.getLonLatFromOverviewPx({
  624. x: pxBounds.left,
  625. y: pxBounds.bottom
  626. });
  627. var rightTopLonLat = this.getLonLatFromOverviewPx({
  628. x: pxBounds.right,
  629. y: pxBounds.top
  630. });
  631. return new OpenLayers.Bounds(leftBottomLonLat.lon, leftBottomLonLat.lat,
  632. rightTopLonLat.lon, rightTopLonLat.lat);
  633. },
  634. /**
  635. * Method: getLonLatFromOverviewPx
  636. * Get a map location from a pixel location
  637. *
  638. * Parameters:
  639. * overviewMapPx - {<OpenLayers.Pixel>|Object} OpenLayers.Pixel or
  640. * an object with a
  641. * 'x' and 'y' properties.
  642. *
  643. * Returns:
  644. * {Object} Location which is the passed-in overview map
  645. * OpenLayers.Pixel, translated into lon/lat by the overview
  646. * map. An object with a 'lon' and 'lat' properties.
  647. */
  648. getLonLatFromOverviewPx: function(overviewMapPx) {
  649. var size = this.ovmap.size;
  650. var res = this.ovmap.getResolution();
  651. var center = this.ovmap.getExtent().getCenterLonLat();
  652. var deltaX = overviewMapPx.x - (size.w / 2);
  653. var deltaY = overviewMapPx.y - (size.h / 2);
  654. return {
  655. lon: center.lon + deltaX * res,
  656. lat: center.lat - deltaY * res
  657. };
  658. },
  659. /**
  660. * Method: getOverviewPxFromLonLat
  661. * Get a pixel location from a map location
  662. *
  663. * Parameters:
  664. * lonlat - {<OpenLayers.LonLat>|Object} OpenLayers.LonLat or an
  665. * object with a 'lon' and 'lat' properties.
  666. *
  667. * Returns:
  668. * {Object} Location which is the passed-in OpenLayers.LonLat,
  669. * translated into overview map pixels
  670. */
  671. getOverviewPxFromLonLat: function(lonlat) {
  672. var res = this.ovmap.getResolution();
  673. var extent = this.ovmap.getExtent();
  674. if (extent) {
  675. return {
  676. x: Math.round(1/res * (lonlat.lon - extent.left)),
  677. y: Math.round(1/res * (extent.top - lonlat.lat))
  678. };
  679. }
  680. },
  681. CLASS_NAME: 'OpenLayers.Control.OverviewMap'
  682. });