root/sandbox/cmoullet/ux/RoutingPanel/ux/widgets/RoutingPanel.js

Revision 1912, 33.0 kB (checked in by cmoullet, 7 months ago)

Fix Ext 3.0 problem with ScriptTagProxy

Line 
1 /**
2  * Copyright (c) 2008-2009 The Open Source Geospatial Foundation
3  *
4  * Published under the BSD license.
5  * See http://svn.geoext.org/core/trunk/geoext/license.txt for the full text
6  * of the license.
7  */
8
9 /** api: (define)
10  *  module = GeoExt.ux
11  *  class = RoutingPanel
12  *  base_link = `Ext.Panel <http://extjs.com/deploy/dev/docs/?class=Ext.Panel>`_
13  */
14
15 Ext.namespace('GeoExt.ux');
16
17 GeoExt.ux.RoutingPanel = Ext.extend(Ext.Panel, {
18
19     /** api: config[map]
20      *  ``OpenLayers.Map``  A configured map
21      */
22     /** private: property[map]
23      *  ``OpenLayers.Map``  The map object
24      */
25     map: null,
26
27     /** api: config[startLocationCombo]
28      *  ``Ext.form.ComboBox``  The combo box used for searching the start point
29      *  In order to work, the combo needs to have a propoerty locationString: lat,lon (WGS 84, example: locationString: '47.25976,9.58423') assigned depending of the searched position
30      *  See geocoding_type for the proposed combo.
31      */
32     /** private: property[startLocationCombo]
33      *  ``Ext.form.ComboBox``  The combo box used for searching the start point
34      */
35     startLocationCombo: null,
36
37     /** api: config[endLocationCombo]
38      *  ``Ext.form.ComboBox``  The combo box used for searching the end point
39      *  In order to work, the combo needs to have a propoerty locationString: lat,lon (WGS 84, example: locationString: '47.25976,9.58423') assigned depending of the searched position
40      *  See geocoding_type for the proposed combo.
41      */
42     /** private: property[endLocationCombo]
43      *  ``Ext.form.ComboBox``  The combo box used for searching the end point
44      */
45     endLocationCombo: null,
46
47     /** private: property[proxy]
48      *  ``Ext.data.ScriptTagProxy``  Proxy to make the search request
49      */
50     proxy: null,
51
52     /** private: property[proxy]
53      *  ``Ext.data.Store``  Store for toring the routing results
54      */
55     routingStore: null,
56
57     /** api: config[lang]
58      *  Language (used for the routing)
59      */
60     /** private: property[lang]
61      *  Language (used for the routing)
62      */
63     lang: 'en',
64
65     /** api: config[routingServiceType]
66      *  Type of routing service used for the computation. Per default: 'cloudmade'
67      */
68     /** private: property[routingServiceType]
69      *  Type of routing service used for the computation
70      */
71     routingServiceType: 'cloudmade',
72
73     /** property[routingVersionCloudmade]
74      *  Routing version of the routing service, available after an itinerary computation
75      *  See http://developers.cloudmade.com/wiki/routing-http-api/Response_structure
76      *  Cloudmade specific
77      */
78     routingVersionCloudmade: null,
79
80     /** property[routingStatusCloudmade]
81      *  Routing status of the routing service, available after an itinerary computation
82      *  See http://developers.cloudmade.com/wiki/routing-http-api/Response_structure
83      *  Cloudmade specific
84      */
85     routingStatusCloudmade: null,
86
87     /** property[routingStatusMessageCloudmade]
88      *  Routing status message of the routing service, available after an itinerary computation
89      *  See http://developers.cloudmade.com/wiki/routing-http-api/Response_structure
90      *  Cloudmade specific
91      */
92     routingStatusMessageCloudmade: null,
93
94     /** property[routingRouteSummaryCloudmade]
95      *  Routing route summary of the routing service, available after an itinerary computation
96      *  See http://developers.cloudmade.com/wiki/routing-http-api/Response_structure
97      *  Cloudmade specific
98      */
99     routingRouteSummaryCloudmade: null,
100
101     /** property[routingRouteGeometryCloudmade]
102      *  Routing route geometry of the routing service, available after an itinerary computation
103      *  See http://developers.cloudmade.com/wiki/routing-http-api/Response_structure
104      *  Cloudmade specific
105      */
106     routingRouteGeometryCloudmade: null,
107
108     /** property[routingRouteInstructionsCloudmade]
109      *  Routing route instruction of the routing service, available after an itinerary computation
110      *  See http://developers.cloudmade.com/wiki/routing-http-api/Response_structure
111      *  Cloudmade specific
112      */
113     routingRouteInstructionsCloudmade: null,
114
115     /** api: config[routingLayer]
116      *  ``OpenLayers.Layer.Vector``  Layer presenting the routing result geometry. Per default, a layer named "Routing" will be created.
117      */
118     /** private: property[routingLayer]
119      *  ``OpenLayers.Layer.Vector``  Layer presenting the routing result geometry. Per default, a layer named "Routing" will be created.
120      */
121     routingLayer: null,
122
123     /** api: config[routingRecenterMap]
124      *  ``Boolean``  Defines if the map is recentered after routing computation. Per default, yes.
125      */
126     /** private: property[routingRecenterMap]
127      *  ``Boolean``  Defines if the map is recentered after routing computation. Per default, yes.
128      */
129     routingRecenterMap: true,
130
131     /** property[routingPathFeature]
132      *  ``OpenLayers.Feature.Vector`` Line feature storing the routing path.
133      */
134     routingPathFeature: null,
135
136     /** property[routingStartFeature]
137      *  ``OpenLayers.Feature.Vector`` Point feature storing the start point, if digitized by the user.
138      */
139     routingStartFeature: null,
140
141     /** property[routingEndFeature]
142      *  ``OpenLayers.Feature.Vector`` Point feature storing the end point, if digitized by the user.
143      */
144     routingEndFeature: null,
145
146     /** private: property[routingPointDrawControl]
147      *  ``OpenLayers.Control.DrawFeature`` Control to get start and end feature
148      */
149     routingPointDrawControl: null,
150
151     /** property[routingResultPanel]
152      *  ``Ext.Panel`` Panel presenting the computation result
153      */
154     routingResultPanel: null,
155
156     /** api: config[route_typeCloudmade]
157      *  Computation route type. Per default: car. Can be 'foot' or 'bicycle'.
158      */
159     /** private: property[route_typeCloudmade]
160      *  Computation route type. Per default: car.
161      */
162     route_typeCloudmade: 'car',
163
164     /** api: config[cloudmadeKey]
165      *  Cloudmade key used for the routing and geocoding services
166      *  Cloudmade specific
167      */
168     cloudmadeKey: null,
169
170     /** api: config[geocodingType]
171      *  Geocoding type. Per default: 'cloudmade'. Can be 'geonames'.
172      */
173     /** private: property[geocodingType]
174      *  Geocoding type. Per default: 'cloudmade'. Can be 'geonames'.
175      */
176     geocodingType: 'cloudmade',
177
178     /** private: property[geocodingQueryParam]
179      *  Geocoding query param used for cloudmade geocoding service. Per default: 'query'
180      */
181     geocodingQueryParam: 'query',
182
183     /** api: config[geocodingMaxRows]
184      *  Maximum number of rows returned by the cloudmade geocoding service. Per default: 20
185      */
186     /** private: property[geocodingMaxRows]
187      *  Maximum number of rows returned by the cloudmade geocoding service. Per default: 20
188      */
189     geocodingMaxRows: 20,
190
191     /** private: property[geocodingUrl]
192      *  Geocoding url of the cloudmade geocoding service.
193      */
194     geocodingUrl: null,
195
196     /** api: config[showGoogleItinerary]
197      *  Define if the google itinerary links are shown in the result panel
198      */
199     /** private: property[showGoogleItinerary]
200      *  Define if the google itinerary links are shown in the result panel
201      */
202     showGoogleItinerary: true,
203
204     /** api: config[routingStyle]
205      *  Vector style of the routing path
206      */
207     /** private: property[routingStyle]
208      *  Vector style of the routing path
209      */
210     routingStyle: {
211         strokeColor: "#0000FF",
212         strokeOpacity: 0.5,
213         strokeWidth: 5
214     },
215
216     /** api: config[vectorStyle]
217      *  ``OpenLayers.StyleMap`` Vector style of routing layer
218      */
219     /** private: property[vectorStyle]
220      *  ``OpenLayers.StyleMap`` Vector style of routing layer
221      */
222     vectorStyle: new OpenLayers.StyleMap({
223         "default": new OpenLayers.Style({
224             pointRadius: "8",
225             fillColor: "#FF0000",
226             fillOpacity: 0.5,
227             strokeColor: "#FF0000",
228             strokeOpacity: 1,
229             strokeWidth: 1
230         })
231     }),
232
233     /** private: property[routingProjection]
234      *  ``OpenLayers.Projection`` Projection of routing system.
235      */
236     routingProjection: new OpenLayers.Projection("EPSG:4326"),
237
238     /** api: config[readPermalink]
239      *  ``Boolean``  Read the permalink in the url if presents
240      */
241     /** private: property[readPermalink]
242      *  ``Boolean``  Read the permalink in the url if presents
243      */
244     readPermalink: true,
245
246     /** private: method[initComponent]
247      *  Private initComponent override.
248      *  Create two events:
249      *  - routingcomputed
250      *  - beforeroutingcomputed
251      */
252     initComponent : function() {
253         var defConfig = {
254             plain: true,
255             border: false
256         };
257
258         Ext.applyIf(this, defConfig);
259
260         // Create cloudmade geocoding serach combo
261         if (this.geocodingType == 'cloudmade') {
262             this.geocodingUrl = 'http://geocoding.cloudmade.com/' + this.cloudmadeKey + '/geocoding/v2/find.js?results=' + this.geocodingMaxRows + '&return_geometry=false';
263             this.startLocationCombo = new Ext.form.ComboBox({
264                 fieldLabel: OpenLayers.i18n('A'),
265                 name: 'startLocationCombo',
266                 emptyText: OpenLayers.i18n('Search start...'),
267                 minChars: 1,
268                 queryDelay: 50,
269                 hideTrigger: true,
270                 charset: 'UTF8',
271                 forceSelection: true,
272                 displayField: 'name',
273                 queryParam: this.geocodingQueryParam,
274                 tpl: '<tpl for="."><div class="x-combo-list-item"><h1>{name}</h1></div></tpl>',
275                 store: new Ext.data.Store({
276                     proxy: new Ext.data.ScriptTagProxy({
277                         url: this.geocodingUrl,
278                         method: 'GET'
279                     }),
280                     reader: new Ext.data.JsonReader({
281                         totalProperty: "found",
282                         root: "features",
283                         fields: [
284                             {
285                                 name: 'name',
286                                 mapping: 'properties.name'
287                             },
288                             {
289                                 name: 'centroid'
290                             }
291                         ]
292                     })
293                 }),
294                 listeners: {
295                     "select": function(combo, record, index) {
296                         if (this.routingStartFeature) {
297                             this.routingLayer.removeFeatures([this.routingStartFeature]);
298                         }
299                         this.startLocationCombo.locationString = record.data.centroid.coordinates[0] + ',' + record.data.centroid.coordinates[1];
300                     },
301                     scope: this
302                 }
303             });
304
305             this.endLocationCombo = new Ext.form.ComboBox({
306                 fieldLabel: OpenLayers.i18n('B'),
307                 name: 'endLocationCombo',
308                 emptyText: OpenLayers.i18n('Search end...'),
309                 minChars: 1,
310                 queryDelay: 50,
311                 hideTrigger: true,
312                 charset: 'UTF8',
313                 forceSelection: true,
314                 displayField: 'name',
315                 queryParam: this.geocodingQueryParam,
316                 tpl: '<tpl for="."><div class="x-combo-list-item"><h1>{name}</h1></div></tpl>',
317                 store: new Ext.data.Store({
318                     proxy: new Ext.data.ScriptTagProxy({
319                         url: this.geocodingUrl,
320                         method: 'GET'
321                     }),
322                     reader: new Ext.data.JsonReader({
323                         totalProperty: "found",
324                         root: "features",
325                         fields: [
326                             {
327                                 name: 'name',
328                                 mapping: 'properties.name'
329                             },
330                             {
331                                 name: 'centroid'
332                             }
333                         ]
334                     })
335                 }),
336                 listeners: {
337                     "select": function(combo, record, index) {
338                         if (this.routingEndFeature) {
339                             this.routingLayer.removeFeatures([this.routingEndFeature]);
340                         }
341                         this.endLocationCombo.locationString = record.data.centroid.coordinates[0] + ',' + record.data.centroid.coordinates[1];
342                     },
343                     scope: this
344                 }
345             });
346         }
347
348         // Create geonames search combo
349         if (this.geocodingType == 'geonames') {
350             this.startLocationCombo = new GeoExt.ux.geonames.GeoNamesSearchCombo({
351                 fieldLabel: OpenLayers.i18n('A'),
352                 map: this.map,
353                 zoom: 13
354             });
355             this.startLocationCombo.on(
356                     "select", function(combo, record, index) {
357                 if (this.routingStartFeature) {
358                     this.routingLayer.removeFeatures([this.routingStartFeature]);
359                 }
360                 this.startLocationCombo.locationString = record.data.lat + ',' + record.data.lng;
361             }, this
362                     );
363
364
365             this.endLocationCombo = new GeoExt.ux.geonames.GeoNamesSearchCombo({
366                 fieldLabel: OpenLayers.i18n('B'),
367                 map: this.map,
368                 zoom: 13
369             });
370             this.endLocationCombo.on("select", function(combo, record, index) {
371                 if (this.routingEndFeature) {
372                     this.routingLayer.removeFeatures([this.routingEndFeature]);
373                 }
374                 this.endLocationCombo.locationString = record.data.lat + ',' + record.data.lng;
375             }, this);
376         }
377
378         // Create routing result panel
379         this.routingResultPanel = new Ext.Panel({
380             border: false,
381             html: '',
382             bodyStyle: 'padding:5px 5px 5px 5px;'
383         });
384
385         // Set default location string
386         if (!this.startLocationCombo.locationString) {
387             this.startLocationCombo.locationString = '47.25976,9.58423';
388         }
389         if (!this.endLocationCombo.locationString) {
390             this.endLocationCombo.locationString = '47.26117,9.59882';
391         }
392
393         // Create items of routing panel
394         this.items = [
395             {
396                 layout: 'form',
397                 border:false,
398                 labelWidth: 10,
399                 items: [
400                     {
401                         layout: 'column',
402                         border: false,
403                         defaults:{
404                             layout:'form',
405                             border:false,
406                             labelWidth: 10,
407                             bodyStyle:'padding:5px 0px 0px 5px'
408                         },
409                         items:[
410                             {
411                                 columnWidth:0.75,
412                                 defaults:{
413                                     anchor:'100%'
414                                 },
415                                 items: [
416                                     this.startLocationCombo
417                                 ]
418                             },
419                             {
420                                 columnWidth:0.25,
421                                 items: [
422                                     {
423                                         xtype:'button',
424                                         text: OpenLayers.i18n('Get Point'),
425                                         handler: function(button, event) {
426                                             this.routingPointDrawControl.type = 'GetStartPoint';
427                                             this.routingPointDrawControl.activate();
428                                         },
429                                         scope: this
430                                     }
431                                 ]
432                             }
433                         ]
434                     },
435                     {
436                         layout: 'column',
437                         border: false,
438                         defaults:{
439                             layout:'form',
440                             border:false,
441                             labelWidth: 10,
442                             bodyStyle:'padding:0px 0px 0px 5px;'
443                         },
444                         items:[
445                             {
446                                 columnWidth:0.75,
447                                 defaults:{
448                                     anchor:'100%'
449                                 },
450                                 items: [
451                                     this.endLocationCombo
452                                 ]
453                             },
454                             {
455                                 columnWidth:0.25,
456                                 items: [
457                                     {
458                                         xtype:'button',
459                                         text: OpenLayers.i18n('Get Point'),
460                                         handler: function(button, event) {
461                                             this.routingPointDrawControl.type = 'GetEndPoint';
462                                             this.routingPointDrawControl.activate();
463                                         },
464                                         scope: this
465                                     }
466                                 ]
467                             }
468                         ]
469                     }
470                 ]
471             },
472             {
473                 layout: 'column',
474                 border: false,
475                 items: [
476                     {
477                         columnWidth: 1,
478                         border: false,
479                         bodyCfg: {tag:'center'},
480                         items: [
481                             {
482                                 xtype:'button',
483                                 text: OpenLayers.i18n('Compute itinerary'),
484                                 handler: function() {
485                                     this.getItinerary();
486                                 },
487                                 scope: this
488                             }
489                         ]
490                     }
491                 ]
492
493             },
494             {
495                 border: false,
496                 items: [
497                     this.routingResultPanel
498                 ]
499             }
500
501         ];
502
503         this.addEvents(
504             /** api: event[routingcomputed]
505              *  Fires when a routing has been computed
506              *
507              *  Listener arguments:
508              *  * comp - :class:`GeoExt.ux.RoutingPanel`` This component.
509              */
510                 'routingcomputed',
511
512             /** api: event[beforeroutingcomputed]
513              *  Fires when before a routing is computed
514              *
515              *  Listener arguments:
516              *  * comp - :class:`GeoExt.ux.RoutingPanel`` This component.
517              */
518                 'beforeroutingcomputed');
519
520
521         GeoExt.ux.RoutingPanel.superclass.initComponent.call(this);
522     },
523
524     /** private: method[afterRender]
525      *  Private afterRender override.
526      */
527     afterRender: function() {
528         GeoExt.ux.RoutingPanel.superclass.afterRender.call(this);
529
530         // Create routing layer
531         if (!this.routingLayer) {
532             this.routingLayer = new OpenLayers.Layer.Vector("Routing", {styleMap: this.vectorStyle});
533         }
534         this.map.addLayer(this.routingLayer);
535
536         // Create point draw control
537         this.routingPointDrawControl = new OpenLayers.Control.DrawFeature(this.routingLayer, OpenLayers.Handler.Point);
538         this.map.addControl(this.routingPointDrawControl);
539         this.routingPointDrawControl.events.on({ "featureadded": function(events) {
540             var featureLocation = null;
541             if (this.routingPointDrawControl.type == 'GetStartPoint') {
542                 if (this.routingStartFeature) {
543                     this.routingLayer.removeFeatures([this.routingStartFeature]);
544                     this.routingStartFeature = null;
545                 }
546                 this.routingStartFeature = events.feature;
547                 featureLocation = this.routingStartFeature.geometry.clone();
548                 featureLocation.transform(this.map.getProjectionObject(), this.routingProjection);
549                 this.startLocationCombo.locationString = featureLocation.y + ',' + featureLocation.x;
550                 this.startLocationCombo.emptyText = OpenLayers.i18n('Position: ') + Math.round(featureLocation.y * 100000) / 100000 + ',' + Math.round(featureLocation.x * 100000) / 100000;
551                 this.startLocationCombo.clearValue();
552             }
553             if (this.routingPointDrawControl.type == 'GetEndPoint') {
554                 if (this.routingEndFeature) {
555                     this.routingLayer.removeFeatures([this.routingEndFeature]);
556                     this.routingEndFeature = null;
557                 }
558                 this.routingEndFeature = events.feature;
559                 featureLocation = this.routingEndFeature.geometry.clone();
560                 featureLocation.transform(this.map.getProjectionObject(), this.routingProjection);
561                 this.endLocationCombo.locationString = featureLocation.y + ',' + featureLocation.x;
562                 this.endLocationCombo.emptyText = OpenLayers.i18n('Position: ') + Math.round(featureLocation.y * 100000) / 100000 + ',' + Math.round(featureLocation.x * 100000) / 100000;
563                 this.endLocationCombo.clearValue();
564             }
565             this.routingPointDrawControl.deactivate();
566         },
567             scope: this
568         });
569         // Use the permalink
570         if (this.readPermalink) {
571             var parameters = OpenLayers.Util.getParameters();
572             this.setPermalink(parameters);
573         }
574     }
575     ,
576
577     /** method[getPermalink]
578      *  Create the permalink
579      *
580      * :param complete ``Boolean`` Defines if the complete URL is generated or only the parameters KVP
581      */
582     getPermalink: function(complete) {
583         var permalink;
584         if (complete) {
585             permalink = window.location.href;
586             if (OpenLayers.String.contains(permalink, '?')) {
587                 var end = permalink.indexOf('?');
588                 permalink = permalink.substring(0, end);
589             }
590             permalink = permalink + "?";
591         } else {
592             permalink = '';
593         }
594
595         permalink = permalink + "routingReadPermalink=" + this.readPermalink;
596         permalink = permalink + "&routingRecenterMap=" + this.routingRecenterMap;
597
598         if (this.startLocationCombo.locationString && this.routingPathFeature) {
599             permalink = permalink + "&routingStartLocationString=" + this.startLocationCombo.locationString;
600         }
601         if (this.endLocationCombo.locationString && this.routingPathFeature) {
602             permalink = permalink + "&routingEndLocationString=" + this.endLocationCombo.locationString;
603         }
604         if (this.startLocationCombo.value) {
605             permalink = permalink + "&routingStartLocationValue=" + this.startLocationCombo.value;
606         }
607         if (this.endLocationCombo.value) {
608             permalink = permalink + "&routingEndLocationValue=" + this.endLocationCombo.value;
609         }
610         if (this.startLocationCombo.emptyText.indexOf(OpenLayers.i18n('Position: ')) > -1) {
611             permalink = permalink + "&routingStartLocationEmptyText=" + this.startLocationCombo.emptyText;
612         }
613         if (this.endLocationCombo.emptyText.indexOf(OpenLayers.i18n('Position: ')) > -1) {
614             permalink = permalink + "&routingEndLocationEmptyText=" + this.endLocationCombo.emptyText;
615         }
616         if (this.map && !this.routingRecenterMap) {
617             permalink = permalink + "&routingeasting=" + this.map.getCenter().lon;
618             permalink = permalink + "&routingnorthing=" + this.map.getCenter().lat;
619             permalink = permalink + "&routingzoom=" + this.map.getZoom();
620         }
621         return permalink;
622     }
623     ,
624
625     /** private: method[setPermalink]
626      *  Set the permalink according to the url parameters
627      *
628      * :param parameters URL paramaters
629      */
630     setPermalink: function(parameters) {
631         if (parameters.routingReadPermalink) {
632             this.readPermalink = this.stringToBoolean(parameters.routingReadPermalink);
633         }
634         if (parameters.routingRecenterMap) {
635             this.routingRecenterMap = this.stringToBoolean(parameters.routingRecenterMap);
636         }
637         if (parameters.routingStartLocationString) {
638             this.startLocationCombo.locationString = parameters.routingStartLocationString[0] + "," + parameters.routingStartLocationString[1];
639         }
640         if (parameters.routingEndLocationString) {
641             this.endLocationCombo.locationString = parameters.routingEndLocationString[0] + "," + parameters.routingEndLocationString[1];
642         }
643         if (parameters.routingStartLocationString && parameters.routingEndLocationString) {
644             this.getItinerary();
645         }
646         if (parameters.routingeasting && parameters.routingnorthing) {
647             var position = new OpenLayers.LonLat(parseFloat(parameters.routingeasting), parseFloat(parameters.routingnorthing));
648             if (this.map) {
649                 this.map.setCenter(position);
650             }
651         }
652         if (parameters.routingzoom) {
653             if (this.map) {
654                 this.map.zoomTo(parseInt(parameters.routingzoom, 10));
655             }
656         }
657         if (parameters.routingStartLocationValue) {
658             this.startLocationCombo.setValue(parameters.routingStartLocationValue);
659         }
660         if (parameters.routingEndLocationValue) {
661             this.endLocationCombo.setValue(parameters.routingEndLocationValue);
662         }
663         if (parameters.routingStartLocationEmptyText) {
664             this.startLocationCombo.emptyText = parameters.routingStartLocationEmptyText.toString();
665         }
666         if (parameters.routingEndLocationEmptyText) {
667             this.endLocationCombo.emptyText = parameters.routingEndLocationEmptyText.toString();
668         }
669     },
670
671     /** private: method[stringToBoolean]
672      *  Transform a string (true, false, yes, no 1 or 0) to a boolean
673      *
674      * :param string
675      */
676     stringToBoolean: function(string) {
677         switch (string.toLowerCase()) {
678             case "true": case "yes": case "1": return true;
679             case "false": case "no": case "0": case null: return false;
680             default: return Boolean(string);
681         }
682     },
683
684     /** private: method[getItinerary]
685      *  Compute the itinerary and assign the results
686      */
687     getItinerary: function() {
688         if (this.routingServiceType == 'cloudmade') {
689            
690             this.fireEvent('beforeroutingcomputed', this);
691             this.routingResultPanel.html = OpenLayers.i18n('Computation ongoing....');
692             if (this.routingResultPanel.body) {
693                 this.routingResultPanel.body.update(this.routingResultPanel.html);
694             }
695             //http://routes.cloudmade.com/BC9A493B41014CAABB98F0471D759707/api/0.3/47.25976,9.58423,47.26117,9.59882/car/shortest.js
696             var newUrl = this.startLocationCombo.locationString + ',' + this.endLocationCombo.locationString;
697             newUrl = newUrl + "/" + this.route_typeCloudmade + ".js?lang=" + this.lang;
698             this.proxy = new Ext.data.ScriptTagProxy({
699                 url: "http://routes.cloudmade.com/" + this.cloudmadeKey + "/api/0.3/" + newUrl,
700                 nocache: false
701             });
702             this.routingStore = new Ext.data.Store({
703                 proxy: this.proxy,
704                 reader: new Ext.data.JsonReader({
705                     root: 'version',
706                     fields: [
707                         {
708                             name: 'total_length'
709                         }
710                     ]
711
712                 })
713             });
714
715             this.routingStore.on(
716                     'load', function(store) {
717                 this.routingVersionCloudmade = store.reader.jsonData.version;
718                 this.routingStatusCloudmade = store.reader.jsonData.status;
719                 if (store.reader.jsonData.status_message) {
720                     this.routingStatusMessageCloudmade = store.reader.jsonData.status_message;
721                 }
722                 if (store.reader.jsonData.route_summary) {
723                     this.routingRouteSummaryCloudmade = store.reader.jsonData.route_summary;
724                 }
725                 if (store.reader.jsonData.route_geometry) {
726                     this.routingRouteGeometryCloudmade = store.reader.jsonData.route_geometry;
727                 }
728                 if (store.reader.jsonData.route_instructions) {
729                     this.routingRouteInstructionsCloudmade = store.reader.jsonData.route_instructions;
730                 }
731                 if (this.routingStatusCloudmade == '0') {
732                     this.drawRoute();
733                     var googleLinks = ''
734                     if (this.showGoogleItinerary) {
735                         var googleLinks = '<a href="http://maps.google.com/maps?source=s_d&saddr=' + this.startLocationCombo.locationString + '&daddr=' + this.endLocationCombo.locationString + '&dirflg=a" target="new">' + OpenLayers.i18n('Google Itinerary by car') + '</a><br><a href="http://maps.google.com/maps?source=s_d&saddr=' + this.startLocationCombo.locationString + '&daddr=' + this.endLocationCombo.locationString + '&dirflg=r" target="new">' + OpenLayers.i18n('Google Itinerary by public transport') + '</a><br><a href="http://maps.google.com/maps?source=s_d&saddr=' + this.startLocationCombo.locationString + '&daddr=' + this.endLocationCombo.locationString + '&dirflg=w" target="new">' + OpenLayers.i18n('Google Itinerary on foot') + '</a>';
736                     }
737                     this.routingResultPanel.html = OpenLayers.i18n('Total length: ') + Math.round(this.routingRouteSummaryCloudmade.total_distance / 1000) + ' [km]<br>' + googleLinks;
738                     this.routingResultPanel.body.update(this.routingResultPanel.html);
739                 } else {
740                     this.routingResultPanel.html = this.routingStatusMessageCloudmade;
741                     this.routingResultPanel.body.update(this.routingResultPanel.html);
742                 }
743                 this.fireEvent('routingcomputed', this);
744             }, this);
745             this.routingStore.load();
746         } else {
747             alert('Routing service: ' + this.routingServiceType + ' not supported. Patch welcome !');
748         }
749     },
750
751     /** private: method[clearItinerary]
752      *  Clear the itinerary the itinerary and assign the results
753      */
754     clearItinerary: function() {
755         if (this.routingPathFeature) {
756             this.routingLayer.removeFeatures([this.routingPathFeature]);
757         }
758         this.startLocationCombo.clearValue();
759         this.endLocationCombo.clearValue();
760         if (this.routingStartFeature) {
761             this.routingLayer.removeFeatures([this.routingStartFeature]);
762             this.routingStartFeature = null;
763         }
764         if (this.routingEndFeature) {
765             this.routingLayer.removeFeatures([this.routingEndFeature]);
766             this.routingEndFeature = null;
767         }
768         this.routingResultPanel.html = '';
769         this.routingResultPanel.body.update(this.routingResultPanel.html);
770     },
771
772     /** private: method[drawRoute]
773      *  Draw the route in the map
774      */
775     drawRoute: function() {
776         var pointList = [];
777
778         if (this.routingServiceType == 'cloudmade') {
779             var newPoint = null;
780             for (var i = 0; i < this.routingRouteGeometryCloudmade.length; i++) {
781                 newPoint = new OpenLayers.Geometry.Point(this.routingRouteGeometryCloudmade[i][1],
782                         this.routingRouteGeometryCloudmade[i][0]);
783                 newPoint.transform(this.routingProjection,
784                         this.map.getProjectionObject());
785                 pointList.push(newPoint);
786             }
787         }
788
789         if (this.routingPathFeature) {
790             this.routingLayer.removeFeatures([this.routingPathFeature]);
791         }
792         this.routingPathFeature = new OpenLayers.Feature.Vector(
793                 new OpenLayers.Geometry.LineString(pointList), null, this.routingStyle);
794
795         this.routingLayer.addFeatures([this.routingPathFeature]);
796         if (this.routingRecenterMap) {
797             this.map.zoomToExtent(this.routingPathFeature.geometry.bounds);
798         }
799     }
800 });
801
802 /** api: xtype = gxux_routingpanel */
803 Ext.reg('gxux_routingpanel', GeoExt.ux.RoutingPanel);
Note: See TracBrowser for help on using the browser.