Development

/plugins/swToolboxPlugin/sf1.2/trunk/web/js/swGmapWidget.js

You must first sign up to be able to contribute.

root/plugins/swToolboxPlugin/sf1.2/trunk/web/js/swGmapWidget.js

Revision 15932, 4.0 kB (checked in by rande, 2 years ago)

add swWidgetFormGMapAddress

Line 
1 /*
2  *  $Id$
3  *
4  * (c) 2009 Thomas Rabaix <thomas.rabaix@soleoweb.com>
5  *
6  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
7  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
8  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
9  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
10  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
11  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
12  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
13  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
14  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
15  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
16  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
17  *
18  * This software consists of voluntary contributions made by many individuals
19  * and is licensed under the LGPL. For more information, see
20  * <http://www.soleoweb.com>.
21  */
22  
23 function swGmapWidget(options){
24   // this global attributes 
25   this.lng      = null;
26   this.lat      = null;
27   this.address  = null;
28   this.map      = null;
29   this.geocoder = null;
30   this.options  = options;
31  
32   this.init();
33 }
34
35 swGmapWidget.prototype = new Object();
36
37 swGmapWidget.prototype.init = function(options) {
38  
39   if(!GBrowserIsCompatible())
40   {
41     return;
42   }
43  
44   // retrieve dom element
45   this.lng      = jQuery("#" + this.options.lng);
46   this.lat      = jQuery("#" + this.options.lat);
47   this.address  = jQuery("#" + this.options.address);
48   this.lookup   = jQuery("#" + this.options.lookup);
49  
50   // create the google geocoder object
51   this.geocoder = new GClientGeocoder();
52  
53   // create the map
54   this.map = new GMap2(jQuery("#" + this.options.map).get(0));
55   this.map.setCenter(new GLatLng(this.lat.val(), this.lng.val()), 13);
56   this.map.setUIToDefault();
57  
58   // cross reference object
59   this.map.swGmapWidget = this;
60   this.geocoder.swGmapWidget = this;
61   this.lookup.get(0).swGmapWidget = this;
62  
63   // bind the move action on the map
64   GEvent.addListener(this.map, "move", function() {
65      var center = this.getCenter();
66      this.swGmapWidget.lng.val(center.lng());
67      this.swGmapWidget.lat.val(center.lat());
68   });
69  
70   // bind the click action on the map
71   GEvent.addListener(this.map, "click", function(overlay, latlng) {
72     if (latlng != null) {
73       swGmapWidget.activeWidget = this.swGmapWidget;
74      
75       this.swGmapWidget.geocoder.getLocations(
76         latlng,
77         swGmapWidget.reverseLookupCallback
78       );
79     }
80   });
81  
82   // bind the click action on the lookup field
83   this.lookup.bind('click', function(){
84     swGmapWidget.activeWidget = this.swGmapWidget;
85    
86     this.swGmapWidget.geocoder.getLatLng(
87       this.swGmapWidget.address.val(),
88       swGmapWidget.lookupCallback
89     );
90    
91     return false;
92   })
93 }
94
95 swGmapWidget.activeWidget = null;
96 swGmapWidget.lookupCallback = function(point)
97 {
98   // get the widget and clear the state variable
99   var widget = swGmapWidget.activeWidget;
100   swGmapWidget.activeWidget = null;
101  
102   if (!point) {
103     alert("address not found");
104     return;
105   }
106  
107   widget.map.clearOverlays();
108   widget.map.setCenter(point, 15);
109   var marker = new GMarker(point);
110   widget.map.addOverlay(marker);
111 }
112
113 swGmapWidget.reverseLookupCallback = function(response)
114 {
115   // get the widget and clear the state variable
116   var widget = swGmapWidget.activeWidget;
117   swGmapWidget.activeWidget = null;
118  
119   widget.map.clearOverlays();
120  
121   if (!response || response.Status.code != 200) {
122     alert('no address found');
123     return;
124   }
125  
126   // get information location and init variables
127   var place = response.Placemark[0];
128   var point = new GLatLng(place.Point.coordinates[1],place.Point.coordinates[0]);
129   var marker = new GMarker(point);
130  
131   // add marker and center the map
132   widget.map.setCenter(point, 15);
133   widget.map.addOverlay(marker);
134
135   // update values
136   widget.address.val(place.address);
137   widget.lat.val(place.Point.coordinates[1]);
138   widget.lng.val(place.Point.coordinates[0]);
139 }
Note: See TracBrowser for help on using the browser.

The Sensio Labs Network

Since 1998, Sensio Labs has been promoting the Open-Source software movement by providing quality web application development, training, consulting.
Sensio Labs also supports several large Open-Source projects.