1 /* Copyright (c) 2011 by MapQuery Contributors (see AUTHORS for
2 * full list of contributors). Published under the MIT license.
3 * See https://github.com/mapquery/mapquery/blob/master/LICENSE for the
4 * full text of the license. */
7 #jquery.mapquery.mqMousePosition.js
8 The file containing the mqMousePosition Widget
10 ### *$('selector')*.`mqMousePosition([options])`
12 ####**Description**: create a widget to show the location under the mouse pointer
15 - **map**: the mapquery instance
16 - **precision**: the number of decimals (default 2)
17 - **x**: the label for the x-coordinate (default x)
18 - **y**: the label for the y-coordinate (default y)
24 The mqMousePosition allows us to show the coordinates under the mouse pointer
27 $('#mousepointer').mqMousePointer({
33 $.template('mqMousePosition',
34 '<div class="mq-mouseposition ui-widget ui-helper-clearfix ">'+
35 '<span class="ui-widget-content ui-helper-clearfix ui-corner-all ui-corner-all">'+
36 '<div id="mq-mouseposition-x" class="mq-mouseposition-coordinate">'+
37 '</div><div id="mq-mouseposition-y" class="mq-mouseposition-coordinate">'+
38 '</div></div></span>');
40 $.widget("mapQuery.mqMousePosition", {
42 // The MapQuery instance
45 // The number of decimals for the coordinates
47 // TODO: JCB20110630 use dynamic precision based on the pixel
48 // resolution, no need to configure precision
51 // The label of the x-value
54 // The label of the y-value
60 //get the mapquery object
61 this.map = $(this.options.map).data('mapQuery');
63 this.map.element.bind('mousemove', {widget: this}, this._onMousemove);
64 $.tmpl('mqMousePosition', {}).appendTo(this.element);
67 _destroy: function() {
68 this.element.removeClass('ui-widget ui-helper-clearfix ' +
72 _onMousemove: function(evt) {
73 var self = evt.data.widget;
76 var mapProjection = new OpenLayers.Projection(self.map.projection);
77 var displayProjection = new OpenLayers.Projection(
78 self.map.displayProjection);
79 var pos = self.map.olMap.getLonLatFromLayerPx(
80 new OpenLayers.Pixel(x, y));
81 //if the coordinates should be displayed in something else,
82 //set them via the map displayProjection option
83 if(!mapProjection.equals(self.map.displayProjection)) {
84 pos = pos.transform(mapProjection, displayProjection);
86 $("#id_diabook_ELPosX", document.element).val(
87 self.options.x + pos.lon.toFixed(self.options.precision));
88 $("#id_diabook_ELPosY", document.element).val(
89 self.options.y + pos.lat.toFixed(self.options.precision));