OverviewMap.js 26 KB

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