Roo/bootstrap/LocationPicker.js
[roojs1] / Roo / bootstrap / LocationPicker.js
1 /*
2  * - LGPL
3  *
4  * Location Picker
5  * 
6  */
7
8 /**
9  * @class Roo.bootstrap.LocationPicker
10  * @extends Roo.bootstrap.Component
11  * Bootstrap LocationPicker class
12  * @cfg {Number} latitude Position when init default 0
13  * @cfg {Number} longitude Position when init default 0
14  * @cfg {Number} zoom default 15
15  * @cfg {String} mapTypeId default google.maps.MapTypeId.ROADMAP
16  * @cfg {Boolean} mapTypeControl default false
17  * @cfg {Boolean} disableDoubleClickZoom default false
18  * @cfg {Boolean} scrollwheel default true
19  * @cfg {Boolean} streetViewControl default false
20  * @cfg {Number} radius default 0
21  * @cfg {String} locationName
22  * @cfg {Boolean} draggable default true
23  * @cfg {Boolean} enableAutocomplete default false
24  * @cfg {Boolean} enableReverseGeocode default true
25  * @cfg {String} markerTitle
26  * 
27  * @constructor
28  * Create a new LocationPicker
29  * @param {Object} config The config object
30  */
31
32
33 Roo.bootstrap.LocationPicker = function(config){
34     
35     Roo.bootstrap.LocationPicker.superclass.constructor.call(this, config);
36     
37     this.addEvents({
38         /**
39          * @event initial
40          * Fires when the picker initialized.
41          * @param {Roo.bootstrap.LocationPicker} this
42          * @param {Google Location} location
43          */
44         initial : true,
45         /**
46          * @event positionchanged
47          * Fires when the picker position changed.
48          * @param {Roo.bootstrap.LocationPicker} this
49          * @param {Google Location} location
50          */
51         positionchanged : true,
52         /**
53          * @event resize
54          * Fires when the map resize.
55          * @param {Roo.bootstrap.LocationPicker} this
56          */
57         resize : true,
58         /**
59          * @event show
60          * Fires when the map show.
61          * @param {Roo.bootstrap.LocationPicker} this
62          */
63         show : true,
64         /**
65          * @event hide
66          * Fires when the map hide.
67          * @param {Roo.bootstrap.LocationPicker} this
68          */
69         hide : true,
70         /**
71          * @event mapClick
72          * Fires when click the map.
73          * @param {Roo.bootstrap.LocationPicker} this
74          * @param {Map event} e
75          */
76         mapClick : true,
77         /**
78          * @event mapRightClick
79          * Fires when right click the map.
80          * @param {Roo.bootstrap.LocationPicker} this
81          * @param {Map event} e
82          */
83         mapRightClick : true,
84         /**
85          * @event markerClick
86          * Fires when click the marker.
87          * @param {Roo.bootstrap.LocationPicker} this
88          * @param {Map event} e
89          */
90         markerClick : true,
91         /**
92          * @event markerRightClick
93          * Fires when right click the marker.
94          * @param {Roo.bootstrap.LocationPicker} this
95          * @param {Map event} e
96          */
97         markerRightClick : true,
98         /**
99          * @event OverlayViewDraw
100          * Fires when OverlayView Draw
101          * @param {Roo.bootstrap.LocationPicker} this
102          */
103         OverlayViewDraw : true,
104         /**
105          * @event OverlayViewOnAdd
106          * Fires when OverlayView Draw
107          * @param {Roo.bootstrap.LocationPicker} this
108          */
109         OverlayViewOnAdd : true,
110         /**
111          * @event OverlayViewOnRemove
112          * Fires when OverlayView Draw
113          * @param {Roo.bootstrap.LocationPicker} this
114          */
115         OverlayViewOnRemove : true,
116         /**
117          * @event OverlayViewShow
118          * Fires when OverlayView Draw
119          * @param {Roo.bootstrap.LocationPicker} this
120          * @param {Pixel} cpx
121          */
122         OverlayViewShow : true,
123         /**
124          * @event OverlayViewHide
125          * Fires when OverlayView Draw
126          * @param {Roo.bootstrap.LocationPicker} this
127          */
128         OverlayViewHide : true
129     });
130         
131 };
132
133 Roo.extend(Roo.bootstrap.LocationPicker, Roo.bootstrap.Component,  {
134     
135     gMapContext: false,
136     
137     latitude: 0,
138     longitude: 0,
139     zoom: 15,
140     mapTypeId: false,
141     mapTypeControl: false,
142     disableDoubleClickZoom: false,
143     scrollwheel: true,
144     streetViewControl: false,
145     radius: 0,
146     locationName: '',
147     draggable: true,
148     enableAutocomplete: false,
149     enableReverseGeocode: true,
150     markerTitle: '',
151     
152     getAutoCreate: function()
153     {
154
155         var cfg = {
156             tag: 'div',
157             cls: 'roo-location-picker'
158         };
159         
160         return cfg
161     },
162     
163     initEvents: function(ct, position)
164     {       
165         if(!this.el.getWidth() || this.isApplied()){
166             return;
167         }
168         
169         this.el.setVisibilityMode(Roo.Element.DISPLAY);
170         
171         this.initial();
172     },
173     
174     initial: function()
175     {
176         if(!this.mapTypeId){
177             this.mapTypeId = google.maps.MapTypeId.ROADMAP;
178         }
179         
180         this.gMapContext = this.GMapContext();
181         
182         this.initOverlayView();
183         
184         this.OverlayView = new Roo.bootstrap.LocationPicker.OverlayView(this.gMapContext.map);
185         
186         var _this = this;
187                 
188         google.maps.event.addListener(this.gMapContext.marker, "dragend", function(event) {
189             _this.setPosition(_this.gMapContext.marker.position);
190         });
191         
192         google.maps.event.addListener(this.gMapContext.map, 'click', function(event){
193             _this.fireEvent('mapClick', this, event);
194             
195         });
196
197         google.maps.event.addListener(this.gMapContext.map, 'rightclick', function(event){
198             _this.fireEvent('mapRightClick', this, event);
199             
200         });
201         
202         google.maps.event.addListener(this.gMapContext.marker, 'click', function(event){
203             _this.fireEvent('markerClick', this, event);
204             
205         });
206
207         google.maps.event.addListener(this.gMapContext.marker, 'rightclick', function(event){
208             _this.fireEvent('markerRightClick', this, event);
209             
210         });
211         
212         this.setPosition(this.gMapContext.location);
213         
214         this.fireEvent('initial', this, this.gMapContext.location);
215     },
216     
217     initOverlayView: function()
218     {
219         var _this = this;
220         
221         Roo.bootstrap.LocationPicker.OverlayView.prototype = Roo.apply(new google.maps.OverlayView(), {
222             
223             draw: function()
224             {
225                 _this.fireEvent('OverlayViewDraw', _this);
226             },
227             
228             onAdd: function()
229             {
230                 _this.fireEvent('OverlayViewOnAdd', _this);
231             },
232             
233             onRemove: function()
234             {
235                 _this.fireEvent('OverlayViewOnRemove', _this);
236             },
237             
238             show: function(cpx)
239             {
240                 _this.fireEvent('OverlayViewShow', _this, cpx);
241             },
242             
243             hide: function()
244             {
245                 _this.fireEvent('OverlayViewHide', _this);
246             }
247             
248         });
249     },
250     
251     fromLatLngToContainerPixel: function(event)
252     {
253         return this.OverlayView.getProjection().fromLatLngToContainerPixel(event.latLng);
254     },
255     
256     isApplied: function() 
257     {
258         return this.getGmapContext() == false ? false : true;
259     },
260     
261     getGmapContext: function() 
262     {
263         return this.gMapContext
264     },
265     
266     GMapContext: function() 
267     {
268         var position = new google.maps.LatLng(this.latitude * 1.0, this.longitude * 1.0);
269         
270         var _map = new google.maps.Map(this.el.dom, this);
271         var _marker = new google.maps.Marker({
272             position: position,
273             map: _map,
274             title: this.markerTitle,
275             draggable: this.draggable
276         });
277         
278         return {
279             map: _map,
280             marker: _marker,
281             circle: null,
282             location: position,
283             radius: this.radius,
284             locationName: this.locationName,
285             addressComponents: {
286                 formatted_address: null,
287                 addressLine1: null,
288                 addressLine2: null,
289                 streetName: null,
290                 streetNumber: null,
291                 city: null,
292                 district: null,
293                 state: null,
294                 stateOrProvince: null
295             },
296             settings: this,
297             domContainer: this.el.dom,
298             geodecoder: new google.maps.Geocoder()
299         };
300     },
301     
302     drawCircle: function(center, radius, options) 
303     {
304         if (this.gMapContext.circle != null) {
305             this.gMapContext.circle.setMap(null);
306         }
307         if (radius > 0) {
308             radius *= 1;
309             options = Roo.apply({}, options, {
310                 strokeColor: "#0000FF",
311                 strokeOpacity: .35,
312                 strokeWeight: 2,
313                 fillColor: "#0000FF",
314                 fillOpacity: .2
315             });
316             
317             options.map = this.gMapContext.map;
318             options.radius = radius;
319             options.center = center;
320             this.gMapContext.circle = new google.maps.Circle(options);
321             return this.gMapContext.circle;
322         }
323         
324         return null;
325     },
326     
327     setPosition: function(location) 
328     {
329         this.gMapContext.location = location;
330         this.gMapContext.marker.setPosition(location);
331         this.gMapContext.map.panTo(location);
332         this.drawCircle(location, this.gMapContext.radius, {});
333         
334         var _this = this;
335         
336         if (this.gMapContext.settings.enableReverseGeocode) {
337             this.gMapContext.geodecoder.geocode({
338                 latLng: this.gMapContext.location
339             }, function(results, status) {
340                 
341                 if (status == google.maps.GeocoderStatus.OK && results.length > 0) {
342                     _this.gMapContext.locationName = results[0].formatted_address;
343                     _this.gMapContext.addressComponents = _this.address_component_from_google_geocode(results[0].address_components);
344                     
345                     _this.fireEvent('positionchanged', this, location);
346                 }
347             });
348             
349             return;
350         }
351         
352         this.fireEvent('positionchanged', this, location);
353     },
354     
355     resize: function()
356     {
357         google.maps.event.trigger(this.gMapContext.map, "resize");
358         
359         this.gMapContext.map.setCenter(this.gMapContext.marker.position);
360         
361         this.fireEvent('resize', this);
362     },
363     
364     setPositionByLatLng: function(latitude, longitude)
365     {
366         this.setPosition(new google.maps.LatLng(latitude, longitude));
367     },
368     
369     getCurrentPosition: function() 
370     {
371         return {
372             latitude: this.gMapContext.location.lat(),
373             longitude: this.gMapContext.location.lng()
374         };
375     },
376     
377     getAddressName: function() 
378     {
379         return this.gMapContext.locationName;
380     },
381     
382     getAddressComponents: function() 
383     {
384         return this.gMapContext.addressComponents;
385     },
386     
387     address_component_from_google_geocode: function(address_components) 
388     {
389         var result = {};
390         
391         for (var i = 0; i < address_components.length; i++) {
392             var component = address_components[i];
393             if (component.types.indexOf("postal_code") >= 0) {
394                 result.postalCode = component.short_name;
395             } else if (component.types.indexOf("street_number") >= 0) {
396                 result.streetNumber = component.short_name;
397             } else if (component.types.indexOf("route") >= 0) {
398                 result.streetName = component.short_name;
399             } else if (component.types.indexOf("neighborhood") >= 0) {
400                 result.city = component.short_name;
401             } else if (component.types.indexOf("locality") >= 0) {
402                 result.city = component.short_name;
403             } else if (component.types.indexOf("sublocality") >= 0) {
404                 result.district = component.short_name;
405             } else if (component.types.indexOf("administrative_area_level_1") >= 0) {
406                 result.stateOrProvince = component.short_name;
407             } else if (component.types.indexOf("country") >= 0) {
408                 result.country = component.short_name;
409             }
410         }
411         
412         result.addressLine1 = [ result.streetNumber, result.streetName ].join(" ").trim();
413         result.addressLine2 = "";
414         return result;
415     },
416     
417     setZoomLevel: function(zoom)
418     {
419         this.gMapContext.map.setZoom(zoom);
420     },
421     
422     show: function()
423     {
424         if(!this.el){
425             return;
426         }
427         
428         this.el.show();
429         
430         this.resize();
431         
432         this.fireEvent('show', this);
433     },
434     
435     hide: function()
436     {
437         if(!this.el){
438             return;
439         }
440         
441         this.el.hide();
442         
443         this.fireEvent('hide', this);
444     }
445     
446 });
447
448 Roo.apply(Roo.bootstrap.LocationPicker, {
449     
450     OverlayView : function(map, options)
451     {
452         options = options || {};
453         
454         this.setMap(map);
455     }
456     
457     
458 });