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
28 * Create a new LocationPicker
29 * @param {Object} config The config object
33 Roo.bootstrap.LocationPicker = function(config){
35 Roo.bootstrap.LocationPicker.superclass.constructor.call(this, config);
40 * Fires when the picker initialized.
41 * @param {Roo.bootstrap.LocationPicker} this
42 * @param {Google Location} location
46 * @event positionchanged
47 * Fires when the picker position changed.
48 * @param {Roo.bootstrap.LocationPicker} this
49 * @param {Google Location} location
51 positionchanged : true,
54 * Fires when the map resize.
55 * @param {Roo.bootstrap.LocationPicker} this
60 * Fires when the map show.
61 * @param {Roo.bootstrap.LocationPicker} this
66 * Fires when the map hide.
67 * @param {Roo.bootstrap.LocationPicker} this
72 * Fires when click the map.
73 * @param {Roo.bootstrap.LocationPicker} this
74 * @param {Map event} e
78 * @event mapRightClick
79 * Fires when right click the map.
80 * @param {Roo.bootstrap.LocationPicker} this
81 * @param {Map event} e
86 * Fires when click the marker.
87 * @param {Roo.bootstrap.LocationPicker} this
88 * @param {Map event} e
92 * @event markerRightClick
93 * Fires when right click the marker.
94 * @param {Roo.bootstrap.LocationPicker} this
95 * @param {Map event} e
97 markerRightClick : true,
99 * @event OverlayViewDraw
100 * Fires when OverlayView Draw
101 * @param {Roo.bootstrap.LocationPicker} this
103 OverlayViewDraw : true,
105 * @event OverlayViewOnAdd
106 * Fires when OverlayView Draw
107 * @param {Roo.bootstrap.LocationPicker} this
109 OverlayViewOnAdd : true,
111 * @event OverlayViewOnRemove
112 * Fires when OverlayView Draw
113 * @param {Roo.bootstrap.LocationPicker} this
115 OverlayViewOnRemove : true,
117 * @event OverlayViewShow
118 * Fires when OverlayView Draw
119 * @param {Roo.bootstrap.LocationPicker} this
122 OverlayViewShow : true,
124 * @event OverlayViewHide
125 * Fires when OverlayView Draw
126 * @param {Roo.bootstrap.LocationPicker} this
128 OverlayViewHide : true,
130 * @event loadexception
131 * Fires when load google lib failed.
132 * @param {Roo.bootstrap.LocationPicker} this
139 Roo.extend(Roo.bootstrap.LocationPicker, Roo.bootstrap.Component, {
147 mapTypeControl: false,
148 disableDoubleClickZoom: false,
150 streetViewControl: false,
154 enableAutocomplete: false,
155 enableReverseGeocode: true,
158 getAutoCreate: function()
163 cls: 'roo-location-picker'
169 initEvents: function(ct, position)
171 Roo.log('initEvent');
172 if(!this.el.getWidth() || this.isApplied()){
176 Roo.log('start init');
177 this.el.setVisibilityMode(Roo.Element.DISPLAY);
184 if(typeof(google) == 'undefined' || typeof(google.maps) == 'undefined'){
185 this.fireEvent('loadexception', this);
190 this.mapTypeId = google.maps.MapTypeId.ROADMAP;
193 this.gMapContext = this.GMapContext();
195 this.initOverlayView();
197 this.OverlayView = new Roo.bootstrap.LocationPicker.OverlayView(this.gMapContext.map);
201 google.maps.event.addListener(this.gMapContext.marker, "dragend", function(event) {
202 _this.setPosition(_this.gMapContext.marker.position);
205 google.maps.event.addListener(this.gMapContext.map, 'click', function(event){
206 _this.fireEvent('mapClick', this, event);
210 google.maps.event.addListener(this.gMapContext.map, 'rightclick', function(event){
211 _this.fireEvent('mapRightClick', this, event);
215 google.maps.event.addListener(this.gMapContext.marker, 'click', function(event){
216 _this.fireEvent('markerClick', this, event);
220 google.maps.event.addListener(this.gMapContext.marker, 'rightclick', function(event){
221 _this.fireEvent('markerRightClick', this, event);
225 this.setPosition(this.gMapContext.location);
227 this.fireEvent('initial', this, this.gMapContext.location);
230 initOverlayView: function()
234 Roo.bootstrap.LocationPicker.OverlayView.prototype = Roo.apply(new google.maps.OverlayView(), {
238 _this.fireEvent('OverlayViewDraw', _this);
243 _this.fireEvent('OverlayViewOnAdd', _this);
248 _this.fireEvent('OverlayViewOnRemove', _this);
253 _this.fireEvent('OverlayViewShow', _this, cpx);
258 _this.fireEvent('OverlayViewHide', _this);
264 fromLatLngToContainerPixel: function(event)
266 return this.OverlayView.getProjection().fromLatLngToContainerPixel(event.latLng);
269 isApplied: function()
271 return this.getGmapContext() == false ? false : true;
274 getGmapContext: function()
276 return (typeof(this.gMapContext) == 'undefined') ? false : this.gMapContext;
279 GMapContext: function()
281 var position = new google.maps.LatLng(this.latitude, this.longitude);
283 var _map = new google.maps.Map(this.el.dom, {
286 mapTypeId: this.mapTypeId,
287 mapTypeControl: this.mapTypeControl,
288 disableDoubleClickZoom: this.disableDoubleClickZoom,
289 scrollwheel: this.scrollwheel,
290 streetViewControl: this.streetViewControl,
291 locationName: this.locationName,
292 draggable: this.draggable,
293 enableAutocomplete: this.enableAutocomplete,
294 enableReverseGeocode: this.enableReverseGeocode
297 var _marker = new google.maps.Marker({
300 title: this.markerTitle,
301 draggable: this.draggable
310 locationName: this.locationName,
312 formatted_address: null,
320 stateOrProvince: null
323 domContainer: this.el.dom,
324 geodecoder: new google.maps.Geocoder()
328 drawCircle: function(center, radius, options)
330 if (this.gMapContext.circle != null) {
331 this.gMapContext.circle.setMap(null);
335 options = Roo.apply({}, options, {
336 strokeColor: "#0000FF",
339 fillColor: "#0000FF",
343 options.map = this.gMapContext.map;
344 options.radius = radius;
345 options.center = center;
346 this.gMapContext.circle = new google.maps.Circle(options);
347 return this.gMapContext.circle;
353 setPosition: function(location)
355 this.gMapContext.location = location;
356 this.gMapContext.marker.setPosition(location);
357 this.gMapContext.map.panTo(location);
358 this.drawCircle(location, this.gMapContext.radius, {});
362 if (this.gMapContext.settings.enableReverseGeocode) {
363 this.gMapContext.geodecoder.geocode({
364 latLng: this.gMapContext.location
365 }, function(results, status) {
367 if (status == google.maps.GeocoderStatus.OK && results.length > 0) {
368 _this.gMapContext.locationName = results[0].formatted_address;
369 _this.gMapContext.addressComponents = _this.address_component_from_google_geocode(results[0].address_components);
371 _this.fireEvent('positionchanged', this, location);
378 this.fireEvent('positionchanged', this, location);
383 google.maps.event.trigger(this.gMapContext.map, "resize");
385 this.gMapContext.map.setCenter(this.gMapContext.marker.position);
387 this.fireEvent('resize', this);
390 setPositionByLatLng: function(latitude, longitude)
392 this.setPosition(new google.maps.LatLng(latitude, longitude));
395 getCurrentPosition: function()
398 latitude: this.gMapContext.location.lat(),
399 longitude: this.gMapContext.location.lng()
403 getAddressName: function()
405 return this.gMapContext.locationName;
408 getAddressComponents: function()
410 return this.gMapContext.addressComponents;
413 address_component_from_google_geocode: function(address_components)
417 for (var i = 0; i < address_components.length; i++) {
418 var component = address_components[i];
419 if (component.types.indexOf("postal_code") >= 0) {
420 result.postalCode = component.short_name;
421 } else if (component.types.indexOf("street_number") >= 0) {
422 result.streetNumber = component.short_name;
423 } else if (component.types.indexOf("route") >= 0) {
424 result.streetName = component.short_name;
425 } else if (component.types.indexOf("neighborhood") >= 0) {
426 result.city = component.short_name;
427 } else if (component.types.indexOf("locality") >= 0) {
428 result.city = component.short_name;
429 } else if (component.types.indexOf("sublocality") >= 0) {
430 result.district = component.short_name;
431 } else if (component.types.indexOf("administrative_area_level_1") >= 0) {
432 result.stateOrProvince = component.short_name;
433 } else if (component.types.indexOf("country") >= 0) {
434 result.country = component.short_name;
438 result.addressLine1 = [ result.streetNumber, result.streetName ].join(" ").trim();
439 result.addressLine2 = "";
443 setZoomLevel: function(zoom)
445 this.gMapContext.map.setZoom(zoom);
458 this.fireEvent('show', this);
469 this.fireEvent('hide', this);
474 Roo.apply(Roo.bootstrap.LocationPicker, {
476 OverlayView : function(map, options)
478 options = options || {};