北漂IT民工 的博客

一段GDirections解析一段位置的代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112

GTFS.Data.Trip.prototype.verify = function(callback, force){

if(this.programmedShapes && !force) {

if(callback) callback(true);

return;

}

var dir = new GDirections();

if(this.programmedShapes) this.programmedShapes.hide();

this.programmedShapes = new GTFS.Data.Shape();

this.programmedShapes.addStoptimes(this.stoptimes);

var idx = 0;

var self = this;

function onDirSuccess(){

var leg = new GTFS.Data.Leg();

var start = self.stoptimes[idx].marker.getLatLng();

var end = self.stoptimes[idx + 1].marker.getLatLng();

var points = [];

var pol = this.getPolyline();

var count = pol.getVertexCount();

points.push({'lat': start.lat(), 'lng': start.lng()});

for (var i = 0; i < count - 1; i++) {

var point = pol.getVertex(i);

points.push({'lat': point.lat(), 'lng': point.lng()});

}

points.push({'lat': end.lat(), 'lng': end.lng()});

var color = GTFS.Data.Trip.colors.programmed;

leg.setData(points, color);

leg.show();

self.stoptimes[idx + 1].preVerifyLeg = leg;

self.stoptimes[idx].sufVerifyLeg = leg;

self.programmedShapes.addLeg(leg);

if(idx == self.stoptimes.length - 2) {

if(callback) callback(true, idx);

} else {

idx++;

setTimeout(startDir, 100); //发现不加上timeout很容易就出错

//startDir(); </p>

}

}

function onDirFailure(){

self.programmedShapes.hideLegs();

self.programmedShapes = null;

idx = 0;

if(callback) callback(false, idx);

}

function startDir(){

var start = self.stoptimes[idx];

var end = self.stoptimes[idx + 1];

var query = "from: " + start.getQuery() + " to: " + end.getQuery();

dir.load(query, {

getPolyline: true

});

}

GEvent.addListener(dir, 'load', onDirSuccess);

GEvent.addListener(dir, 'error', onDirFailure);

startDir();

}