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.mapTypeId){
166             this.mapTypeId = google.maps.MapTypeId.ROADMAP;
167         }
168             
169         if(!this.el.getWidth() || this.isApplied()){
170             return;
171         }
172         
173         this.el.setVisibilityMode(Roo.Element.DISPLAY);
174         
175         this.initial();
176     },
177     
178     initial: function()
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             Roo.log('mapClick');
194             _this.fireEvent('mapClick', this, event);
195             
196         });
197
198         google.maps.event.addListener(this.gMapContext.map, 'rightclick', function(event){
199             Roo.log('mapRightClick');
200             _this.fireEvent('mapRightClick', this, event);
201             
202         });
203         
204         google.maps.event.addListener(this.gMapContext.marker, 'click', function(event){
205             Roo.log('markerClick');
206             _this.fireEvent('markerClick', this, event);
207             
208         });
209
210         google.maps.event.addListener(this.gMapContext.marker, 'rightclick', function(event){
211             Roo.log('markerRightClick');
212             _this.fireEvent('markerRightClick', this, event);
213             
214         });
215         
216         this.setPosition(this.gMapContext.location);
217         
218         this.fireEvent('initial', this, this.gMapContext.location);
219     },
220     
221     initOverlayView: function()
222     {
223         var _this = this;
224         
225         Roo.bootstrap.LocationPicker.OverlayView.prototype = Roo.apply(new google.maps.OverlayView(), {
226             
227             draw: function()
228             {
229                 Roo.log('OverlayView draw');
230                 _this.fireEvent('OverlayViewDraw', _this);
231             },
232             
233             onAdd: function()
234             {
235                 Roo.log('OverlayView onAdd');
236                 _this.fireEvent('OverlayViewOnAdd', _this);
237             },
238             
239             onRemove: function()
240             {
241                 Roo.log('OverlayView onRemove');
242                 _this.fireEvent('OverlayViewOnRemove', _this);
243             },
244             
245             show: function(cpx)
246             {
247                 Roo.log('OverlayView show');
248                 _this.fireEvent('OverlayViewShow', _this, cpx);
249             },
250             
251             hide: function()
252             {
253                 Roo.log('OverlayView hide');
254                 _this.fireEvent('OverlayViewHide', _this);
255             }
256             
257         });
258     },
259     
260     fromLatLngToContainerPixel: function(event)
261     {
262         return this.OverlayView.getProjection().fromLatLngToContainerPixel(event.latLng);
263     },
264     
265     isApplied: function() 
266     {
267         return this.getGmapContext() == false ? false : true;
268     },
269     
270     getGmapContext: function() 
271     {
272         return this.gMapContext
273     },
274     
275     GMapContext: function() 
276     {
277         var _map = new google.maps.Map(this.el.dom, this);
278         var _marker = new google.maps.Marker({
279             position: new google.maps.LatLng(this.latitude, this.longitude),
280             map: _map,
281             title: this.markerTitle,
282             draggable: this.draggable
283         });
284         
285         return {
286             map: _map,
287             marker: _marker,
288             circle: null,
289             location: _marker.position,
290             radius: this.radius,
291             locationName: this.locationName,
292             addressComponents: {
293                 formatted_address: null,
294                 addressLine1: null,
295                 addressLine2: null,
296                 streetName: null,
297                 streetNumber: null,
298                 city: null,
299                 district: null,
300                 state: null,
301                 stateOrProvince: null
302             },
303             settings: this,
304             domContainer: this.el.dom,
305             geodecoder: new google.maps.Geocoder()
306         };
307     },
308     
309     drawCircle: function(center, radius, options) 
310     {
311         if (this.gMapContext.circle != null) {
312             this.gMapContext.circle.setMap(null);
313         }
314         if (radius > 0) {
315             radius *= 1;
316             options = Roo.apply({}, options, {
317                 strokeColor: "#0000FF",
318                 strokeOpacity: .35,
319                 strokeWeight: 2,
320                 fillColor: "#0000FF",
321                 fillOpacity: .2
322             });
323             
324             options.map = this.gMapContext.map;
325             options.radius = radius;
326             options.center = center;
327             this.gMapContext.circle = new google.maps.Circle(options);
328             return this.gMapContext.circle;
329         }
330         
331         return null;
332     },
333     
334     setPosition: function(location) 
335     {
336         this.gMapContext.location = location;
337         this.gMapContext.marker.setPosition(location);
338         this.gMapContext.map.panTo(location);
339         this.drawCircle(location, this.gMapContext.radius, {});
340         
341         var _this = this;
342         
343         if (this.gMapContext.settings.enableReverseGeocode) {
344             this.gMapContext.geodecoder.geocode({
345                 latLng: this.gMapContext.location
346             }, function(results, status) {
347                 
348                 if (status == google.maps.GeocoderStatus.OK && results.length > 0) {
349                     _this.gMapContext.locationName = results[0].formatted_address;
350                     _this.gMapContext.addressComponents = _this.address_component_from_google_geocode(results[0].address_components);
351                     
352                     _this.fireEvent('positionchanged', this, location);
353                 }
354             });
355             
356             return;
357         }
358         
359         this.fireEvent('positionchanged', this, location);
360     },
361     
362     resize: function()
363     {
364         google.maps.event.trigger(this.gMapContext.map, "resize");
365         
366         this.gMapContext.map.setCenter(this.gMapContext.marker.position);
367         
368         this.fireEvent('resize', this);
369     },
370     
371     setPositionByLatLng: function(latitude, longitude)
372     {
373         this.setPosition(new google.maps.LatLng(latitude, longitude));
374     },
375     
376     getCurrentPosition: function() 
377     {
378         return {
379             latitude: this.gMapContext.location.lat(),
380             longitude: this.gMapContext.location.lng()
381         };
382     },
383     
384     getAddressName: function() 
385     {
386         return this.gMapContext.locationName;
387     },
388     
389     getAddressComponents: function() 
390     {
391         return this.gMapContext.addressComponents;
392     },
393     
394     address_component_from_google_geocode: function(address_components) 
395     {
396         var result = {};
397         
398         for (var i = 0; i < address_components.length; i++) {
399             var component = address_components[i];
400             if (component.types.indexOf("postal_code") >= 0) {
401                 result.postalCode = component.short_name;
402             } else if (component.types.indexOf("street_number") >= 0) {
403                 result.streetNumber = component.short_name;
404             } else if (component.types.indexOf("route") >= 0) {
405                 result.streetName = component.short_name;
406             } else if (component.types.indexOf("neighborhood") >= 0) {
407                 result.city = component.short_name;
408             } else if (component.types.indexOf("locality") >= 0) {
409                 result.city = component.short_name;
410             } else if (component.types.indexOf("sublocality") >= 0) {
411                 result.district = component.short_name;
412             } else if (component.types.indexOf("administrative_area_level_1") >= 0) {
413                 result.stateOrProvince = component.short_name;
414             } else if (component.types.indexOf("country") >= 0) {
415                 result.country = component.short_name;
416             }
417         }
418         
419         result.addressLine1 = [ result.streetNumber, result.streetName ].join(" ").trim();
420         result.addressLine2 = "";
421         return result;
422     },
423     
424     setZoomLevel: function(zoom)
425     {
426         this.gMapContext.map.setZoom(zoom);
427     },
428     
429     show: function()
430     {
431         if(!this.el){
432             return;
433         }
434         
435         this.el.show();
436         
437         this.resize();
438         
439         this.fireEvent('show', this);
440     },
441     
442     hide: function()
443     {
444         if(!this.el){
445             return;
446         }
447         
448         this.el.hide();
449         
450         this.fireEvent('hide', this);
451     }
452     
453 });
454
455 Roo.apply(Roo.bootstrap.LocationPicker, {
456     
457     OverlayView : function(map, options)
458     {
459         options = options || {};
460         
461         this.setMap(map);
462     }
463     
464     
465 });