Upgrade to ExtJS 4.0.7 - Released 10/19/2011
[extjs.git] / examples / ux / GMapPanel.js
1 /*
2
3 This file is part of Ext JS 4
4
5 Copyright (c) 2011 Sencha Inc
6
7 Contact:  http://www.sencha.com/contact
8
9 GNU General Public License Usage
10 This file may be used under the terms of the GNU General Public License version 3.0 as published by the Free Software Foundation and appearing in the file LICENSE included in the packaging of this file.  Please review the following information to ensure the GNU General Public License version 3.0 requirements will be met: http://www.gnu.org/copyleft/gpl.html.
11
12 If you are unsure which license is appropriate for your use, please contact the sales department at http://www.sencha.com/contact.
13
14 */
15 /**
16  * @class Ext.ux.GMapPanel
17  * @extends Ext.Panel
18  * @author Shea Frederick
19  */
20 Ext.define('Ext.ux.GMapPanel', {
21     extend: 'Ext.Panel',
22     
23     alias: 'widget.gmappanel',
24     
25     requires: ['Ext.window.MessageBox'],
26     
27     initComponent : function(){
28         
29         var defConfig = {
30             plain: true,
31             zoomLevel: 3,
32             yaw: 180,
33             pitch: 0,
34             zoom: 0,
35             gmapType: 'map',
36             border: false
37         };
38         
39         Ext.applyIf(this,defConfig);
40         
41         this.callParent();        
42     },
43     
44     afterRender : function(){
45         
46         var wh = this.ownerCt.getSize(),
47             point;
48             
49         Ext.applyIf(this, wh);
50         
51         this.callParent();     
52         
53         if (this.gmapType === 'map'){
54             this.gmap = new GMap2(this.body.dom);
55         }
56         
57         if (this.gmapType === 'panorama'){
58             this.gmap = new GStreetviewPanorama(this.body.dom);
59         }
60         
61         if (typeof this.addControl == 'object' && this.gmapType === 'map') {
62             this.gmap.addControl(this.addControl);
63         }
64         
65         if (typeof this.setCenter === 'object') {
66             if (typeof this.setCenter.geoCodeAddr === 'string'){
67                 this.geoCodeLookup(this.setCenter.geoCodeAddr);
68             }else{
69                 if (this.gmapType === 'map'){
70                     point = new GLatLng(this.setCenter.lat,this.setCenter.lng);
71                     this.gmap.setCenter(point, this.zoomLevel);    
72                 }
73                 if (typeof this.setCenter.marker === 'object' && typeof point === 'object'){
74                     this.addMarker(point,this.setCenter.marker,this.setCenter.marker.clear);
75                 }
76             }
77             if (this.gmapType === 'panorama'){
78                 this.gmap.setLocationAndPOV(new GLatLng(this.setCenter.lat,this.setCenter.lng), {yaw: this.yaw, pitch: this.pitch, zoom: this.zoom});
79             }
80         }
81
82         GEvent.bind(this.gmap, 'load', this, function(){
83             this.onMapReady();
84         });
85
86     },
87     onMapReady : function(){
88         this.addMarkers(this.markers);
89         this.addMapControls();
90         this.addOptions();  
91     },
92     afterComponentLayout : function(w, h){
93
94         if (typeof this.getMap() == 'object') {
95             this.gmap.checkResize();
96         }
97         
98         this.callParent(arguments);
99
100     },
101     setSize : function(width, height, animate){
102         
103         if (typeof this.getMap() == 'object') {
104             this.gmap.checkResize();
105         }
106         
107         this.callParent(arguments);
108         
109     },
110     getMap : function(){
111         
112         return this.gmap;
113         
114     },
115     getCenter : function(){
116         
117         return this.getMap().getCenter();
118         
119     },
120     getCenterLatLng : function(){
121         
122         var ll = this.getCenter();
123         return {lat: ll.lat(), lng: ll.lng()};
124         
125     },
126     addMarkers : function(markers) {
127         
128         if (Ext.isArray(markers)){
129             for (var i = 0; i < markers.length; i++) {
130                 var mkr_point = new GLatLng(markers[i].lat,markers[i].lng);
131                 this.addMarker(mkr_point,markers[i].marker,false,markers[i].setCenter, markers[i].listeners);
132             }
133         }
134         
135     },
136     addMarker : function(point, marker, clear, center, listeners){
137         var evt;
138         Ext.applyIf(marker,G_DEFAULT_ICON);
139
140         if (clear === true){
141             this.getMap().clearOverlays();
142         }
143         if (center === true) {
144             this.getMap().setCenter(point, this.zoomLevel);
145         }
146
147         var mark = new GMarker(point,marker);
148         if (typeof listeners === 'object'){
149             for (evt in listeners) {
150                 if (!listeners.hasOwnProperty(evt)) {
151                     continue;
152                 }
153                 GEvent.bind(mark, evt, this, listeners[evt]);
154             }
155         }
156         this.getMap().addOverlay(mark);
157
158     },
159     addMapControls : function(){
160         
161         if (this.gmapType === 'map') {
162             if (Ext.isArray(this.mapControls)) {
163                 for(var i=0;i<this.mapControls.length;i++){
164                     this.addMapControl(this.mapControls[i]);
165                 }
166             }else if(typeof this.mapControls === 'string'){
167                 this.addMapControl(this.mapControls);
168             }else if(typeof this.mapControls === 'object'){
169                 this.getMap().addControl(this.mapControls);
170             }
171         }
172         
173     },
174     addMapControl : function(mc){
175         
176         var mcf = window[mc];
177         if (typeof mcf === 'function') {
178             this.getMap().addControl(new mcf());
179         }    
180         
181     },
182     addOptions : function(){
183         
184         if (Ext.isArray(this.mapConfOpts)) {
185             for(var i=0;i<this.mapConfOpts.length;i++){
186                 this.addOption(this.mapConfOpts[i]);
187             }
188         }else if(typeof this.mapConfOpts === 'string'){
189             this.addOption(this.mapConfOpts);
190         }        
191         
192     },
193     addOption : function(mc){
194         
195         var mcf = this.getMap()[mc];
196         if (typeof mcf === 'function') {
197             this.getMap()[mc]();
198         }    
199         
200     },
201     geoCodeLookup : function(addr) {
202         
203         this.geocoder = new GClientGeocoder();
204         this.geocoder.getLocations(addr, Ext.Function.bind(this.addAddressToMap, this));
205         
206     },
207     addAddressToMap : function(response) {
208         var place, addressinfo, accuracy, point;
209         if (!response || response.Status.code != 200) {
210             Ext.MessageBox.alert('Error', 'Code '+response.Status.code+' Error Returned');
211         }else{
212             place = response.Placemark[0];
213             addressinfo = place.AddressDetails;
214             accuracy = addressinfo.Accuracy;
215             if (accuracy === 0) {
216                 Ext.MessageBox.alert('Unable to Locate Address', 'Unable to Locate the Address you provided');
217             }else{
218                 if (accuracy < 7) {
219                     Ext.MessageBox.alert('Address Accuracy', 'The address provided has a low accuracy.<br><br>Level '+accuracy+' Accuracy (8 = Exact Match, 1 = Vague Match)');
220                 }else{
221                     point = new GLatLng(place.Point.coordinates[1], place.Point.coordinates[0]);
222                     if (typeof this.setCenter.marker === 'object' && typeof point === 'object'){
223                         this.addMarker(point,this.setCenter.marker,this.setCenter.marker.clear,true, this.setCenter.listeners);
224                     }
225                 }
226             }
227         }
228         
229     }
230  
231 });
232