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