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
133 Roo.extend(Roo.bootstrap.LocationPicker, Roo.bootstrap.Component, {
141 mapTypeControl: false,
142 disableDoubleClickZoom: false,
144 streetViewControl: false,
148 enableAutocomplete: false,
149 enableReverseGeocode: true,
152 getAutoCreate: function()
157 cls: 'roo-location-picker'
163 initEvents: function(ct, position)
165 if(!this.el.getWidth() || this.isApplied()){
169 this.el.setVisibilityMode(Roo.Element.DISPLAY);
177 this.mapTypeId = google.maps.MapTypeId.ROADMAP;
180 this.gMapContext = this.GMapContext();
182 this.initOverlayView();
184 this.OverlayView = new Roo.bootstrap.LocationPicker.OverlayView(this.gMapContext.map);
188 google.maps.event.addListener(this.gMapContext.marker, "dragend", function(event) {
189 _this.setPosition(_this.gMapContext.marker.position);
192 google.maps.event.addListener(this.gMapContext.map, 'click', function(event){
193 _this.fireEvent('mapClick', this, event);
197 google.maps.event.addListener(this.gMapContext.map, 'rightclick', function(event){
198 _this.fireEvent('mapRightClick', this, event);
202 google.maps.event.addListener(this.gMapContext.marker, 'click', function(event){
203 _this.fireEvent('markerClick', this, event);
207 google.maps.event.addListener(this.gMapContext.marker, 'rightclick', function(event){
208 _this.fireEvent('markerRightClick', this, event);
212 this.setPosition(this.gMapContext.location);
214 this.fireEvent('initial', this, this.gMapContext.location);
217 initOverlayView: function()
221 Roo.bootstrap.LocationPicker.OverlayView.prototype = Roo.apply(new google.maps.OverlayView(), {
225 _this.fireEvent('OverlayViewDraw', _this);
230 _this.fireEvent('OverlayViewOnAdd', _this);
235 _this.fireEvent('OverlayViewOnRemove', _this);
240 _this.fireEvent('OverlayViewShow', _this, cpx);
245 _this.fireEvent('OverlayViewHide', _this);
251 fromLatLngToContainerPixel: function(event)
253 return this.OverlayView.getProjection().fromLatLngToContainerPixel(event.latLng);
256 isApplied: function()
258 return this.getGmapContext() == false ? false : true;
261 getGmapContext: function()
263 return this.gMapContext
266 GMapContext: function()
268 var _map = new google.maps.Map(this.el.dom, this);
269 Roo.log(new google.maps.LatLng(this.latitude, this.longitude));
270 var _marker = new google.maps.Marker({
271 position: new google.maps.LatLng(this.latitude, this.longitude),
273 title: this.markerTitle,
274 draggable: this.draggable
281 location: _marker.position,
283 locationName: this.locationName,
285 formatted_address: null,
293 stateOrProvince: null
296 domContainer: this.el.dom,
297 geodecoder: new google.maps.Geocoder()
301 drawCircle: function(center, radius, options)
303 if (this.gMapContext.circle != null) {
304 this.gMapContext.circle.setMap(null);
308 options = Roo.apply({}, options, {
309 strokeColor: "#0000FF",
312 fillColor: "#0000FF",
316 options.map = this.gMapContext.map;
317 options.radius = radius;
318 options.center = center;
319 this.gMapContext.circle = new google.maps.Circle(options);
320 return this.gMapContext.circle;
326 setPosition: function(location)
328 this.gMapContext.location = location;
329 this.gMapContext.marker.setPosition(location);
330 this.gMapContext.map.panTo(location);
331 this.drawCircle(location, this.gMapContext.radius, {});
335 if (this.gMapContext.settings.enableReverseGeocode) {
336 this.gMapContext.geodecoder.geocode({
337 latLng: this.gMapContext.location
338 }, function(results, status) {
340 if (status == google.maps.GeocoderStatus.OK && results.length > 0) {
341 _this.gMapContext.locationName = results[0].formatted_address;
342 _this.gMapContext.addressComponents = _this.address_component_from_google_geocode(results[0].address_components);
344 _this.fireEvent('positionchanged', this, location);
351 this.fireEvent('positionchanged', this, location);
356 google.maps.event.trigger(this.gMapContext.map, "resize");
358 this.gMapContext.map.setCenter(this.gMapContext.marker.position);
360 this.fireEvent('resize', this);
363 setPositionByLatLng: function(latitude, longitude)
365 this.setPosition(new google.maps.LatLng(latitude, longitude));
368 getCurrentPosition: function()
371 latitude: this.gMapContext.location.lat(),
372 longitude: this.gMapContext.location.lng()
376 getAddressName: function()
378 return this.gMapContext.locationName;
381 getAddressComponents: function()
383 return this.gMapContext.addressComponents;
386 address_component_from_google_geocode: function(address_components)
390 for (var i = 0; i < address_components.length; i++) {
391 var component = address_components[i];
392 if (component.types.indexOf("postal_code") >= 0) {
393 result.postalCode = component.short_name;
394 } else if (component.types.indexOf("street_number") >= 0) {
395 result.streetNumber = component.short_name;
396 } else if (component.types.indexOf("route") >= 0) {
397 result.streetName = component.short_name;
398 } else if (component.types.indexOf("neighborhood") >= 0) {
399 result.city = component.short_name;
400 } else if (component.types.indexOf("locality") >= 0) {
401 result.city = component.short_name;
402 } else if (component.types.indexOf("sublocality") >= 0) {
403 result.district = component.short_name;
404 } else if (component.types.indexOf("administrative_area_level_1") >= 0) {
405 result.stateOrProvince = component.short_name;
406 } else if (component.types.indexOf("country") >= 0) {
407 result.country = component.short_name;
411 result.addressLine1 = [ result.streetNumber, result.streetName ].join(" ").trim();
412 result.addressLine2 = "";
416 setZoomLevel: function(zoom)
418 this.gMapContext.map.setZoom(zoom);
431 this.fireEvent('show', this);
442 this.fireEvent('hide', this);
447 Roo.apply(Roo.bootstrap.LocationPicker, {
449 OverlayView : function(map, options)
451 options = options || {};