-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathloadgpx.js
308 lines (266 loc) · 10.6 KB
/
loadgpx.js
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
///////////////////////////////////////////////////////////////////////////////
// loadgpx.4.js
//
// Javascript object to load GPX-format GPS data into Google Maps.
//
// Copyright (C) 2006 Kaz Okuda (http://notions.okuda.ca)
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
//
// If you use this script or have any questions please leave a comment
// at http://notions.okuda.ca/geotagging/projects-im-working-on/gpx-viewer/
// A link to the GPL license can also be found there.
//
///////////////////////////////////////////////////////////////////////////////
//
// History:
// revision 1 - Initial implementation
// revision 2 - Removed LoadGPXFileIntoGoogleMap and made it the callers
// responsibility. Added more options (colour, width, delta).
// revision 3 - Waypoint parsing now compatible with Firefox.
// revision 4 - Upgraded to Google Maps API version 2. Tried changing the way
// that the map calculated the way the center and zoom level, but
// GMAP API 2 requires that you center and zoom the map first.
// I have left the bounding box calculations commented out in case
// they might come in handy in the future.
//
// 5/28/2010 - Upgraded to Google Maps API v3 and refactored the file a bit.
// (Chris Peplin)
//
// Author: Kaz Okuda
// URI: http://notions.okuda.ca/geotagging/projects-im-working-on/gpx-viewer/
//
// Updated for Google Maps API v3 by Chris Peplin
// Fork moved to GitHub: https://github.com/peplin/gpxviewer
//
///////////////////////////////////////////////////////////////////////////////
function GPXParser(xmlDoc, map) {
this.xmlDoc = xmlDoc;
this.map = map;
this.trackcolour = "#ff00ff"; // red
this.trackwidth = 5;
this.mintrackpointdelta = 0.0001
}
// Set the colour of the track line segements.
GPXParser.prototype.setTrackColour = function(colour) {
this.trackcolour = colour;
}
// Set the width of the track line segements
GPXParser.prototype.setTrackWidth = function(width) {
this.trackwidth = width;
}
// Set the minimum distance between trackpoints.
// Used to cull unneeded trackpoints from map.
GPXParser.prototype.setMinTrackPointDelta = function(delta) {
this.mintrackpointdelta = delta;
}
GPXParser.prototype.translateName = function(name) {
if(name == "wpt") {
return "Waypoint";
}
else if(name == "trkpt") {
return "Track Point";
}
else if(name == "rtept") {
return "Route Point";
}
}
GPXParser.prototype.createMarker = function(point) {
var lon = parseFloat(point.getAttribute("lon"));
var lat = parseFloat(point.getAttribute("lat"));
var html = "";
var pointElements = point.getElementsByTagName("html");
if(pointElements.length > 0) {
for(i = 0; i < pointElements.item(0).childNodes.length; i++) {
html += pointElements.item(0).childNodes[i].nodeValue;
}
}
else {
// Create the html if it does not exist in the point.
html = "<b>" + this.translateName(point.nodeName) + "</b><br>";
var attributes = point.attributes;
var attrlen = attributes.length;
for(i = 0; i < attrlen; i++) {
html += attributes.item(i).name + " = " +
attributes.item(i).nodeValue + "<br>";
}
if(point.hasChildNodes) {
var children = point.childNodes;
var childrenlen = children.length;
for(i = 0; i < childrenlen; i++) {
// Ignore empty nodes
if(children[i].nodeType != 1) continue;
if(children[i].firstChild == null) continue;
html += children[i].nodeName + " = " +
children[i].firstChild.nodeValue + "<br>";
}
}
}
var marker = new google.maps.Marker({
position: new google.maps.LatLng(lat,lon),
map: this.map
});
var infowindow = new google.maps.InfoWindow({
content: html,
size: new google.maps.Size(50,50)
});
google.maps.event.addListener(marker, "click", function() {
infowindow.open(this.map, marker);
});
}
GPXParser.prototype.addTrackSegmentToMap = function(trackSegment, colour,
width) {
var trackpoints = trackSegment.getElementsByTagName("trkpt");
if(trackpoints.length == 0) {
return;
}
var pointarray = [];
// process first point
var lastlon = parseFloat(trackpoints[0].getAttribute("lon"));
var lastlat = parseFloat(trackpoints[0].getAttribute("lat"));
var latlng = new google.maps.LatLng(lastlat,lastlon);
pointarray.push(latlng);
for(var i = 1; i < trackpoints.length; i++) {
var lon = parseFloat(trackpoints[i].getAttribute("lon"));
var lat = parseFloat(trackpoints[i].getAttribute("lat"));
// Verify that this is far enough away from the last point to be used.
var latdiff = lat - lastlat;
var londiff = lon - lastlon;
if(Math.sqrt(latdiff*latdiff + londiff*londiff)
> this.mintrackpointdelta) {
lastlon = lon;
lastlat = lat;
latlng = new google.maps.LatLng(lat,lon);
pointarray.push(latlng);
}
}
var polyline = new google.maps.Polyline({
path: pointarray,
strokeColor: colour,
strokeWeight: width,
map: this.map
});
}
GPXParser.prototype.addTrackToMap = function(track, colour, width) {
var segments = track.getElementsByTagName("trkseg");
for(var i = 0; i < segments.length; i++) {
var segmentlatlngbounds = this.addTrackSegmentToMap(segments[i], colour,
width);
}
}
GPXParser.prototype.addRouteToMap = function(route, colour, width) {
var routepoints = route.getElementsByTagName("rtept");
if(routepoints.length == 0) {
return;
}
var pointarray = [];
// process first point
var lastlon = parseFloat(routepoints[0].getAttribute("lon"));
var lastlat = parseFloat(routepoints[0].getAttribute("lat"));
var latlng = new google.maps.LatLng(lastlat,lastlon);
pointarray.push(latlng);
for(var i = 1; i < routepoints.length; i++) {
var lon = parseFloat(routepoints[i].getAttribute("lon"));
var lat = parseFloat(routepoints[i].getAttribute("lat"));
// Verify that this is far enough away from the last point to be used.
var latdiff = lat - lastlat;
var londiff = lon - lastlon;
if(Math.sqrt(latdiff*latdiff + londiff*londiff)
> this.mintrackpointdelta) {
lastlon = lon;
lastlat = lat;
latlng = new google.maps.LatLng(lat,lon);
pointarray.push(latlng);
}
}
var polyline = new google.maps.Polyline({
path: pointarray,
strokeColor: colour,
strokeWeight: width,
map: this.map
});
}
GPXParser.prototype.centerAndZoom = function(trackSegment) {
var pointlist = new Array("trkpt", "rtept", "wpt");
var minlat = 0;
var maxlat = 0;
var minlon = 0;
var maxlon = 0;
for(var pointtype = 0; pointtype < pointlist.length; pointtype++) {
// Center the map and zoom on the given segment.
var trackpoints = trackSegment.getElementsByTagName(
pointlist[pointtype]);
// If the min and max are uninitialized then initialize them.
if((trackpoints.length > 0) && (minlat == maxlat) && (minlat == 0)) {
minlat = parseFloat(trackpoints[0].getAttribute("lat"));
maxlat = parseFloat(trackpoints[0].getAttribute("lat"));
minlon = parseFloat(trackpoints[0].getAttribute("lon"));
maxlon = parseFloat(trackpoints[0].getAttribute("lon"));
}
for(var i = 0; i < trackpoints.length; i++) {
var lon = parseFloat(trackpoints[i].getAttribute("lon"));
var lat = parseFloat(trackpoints[i].getAttribute("lat"));
if(lon < minlon) minlon = lon;
if(lon > maxlon) maxlon = lon;
if(lat < minlat) minlat = lat;
if(lat > maxlat) maxlat = lat;
}
}
if((minlat == maxlat) && (minlat == 0)) {
this.map.setCenter(new google.maps.LatLng(49.327667, -122.942333), 14);
return;
}
// Center around the middle of the points
var centerlon = (maxlon + minlon) / 2;
var centerlat = (maxlat + minlat) / 2;
var bounds = new google.maps.LatLngBounds(
new google.maps.LatLng(minlat, minlon),
new google.maps.LatLng(maxlat, maxlon));
this.map.setCenter(new google.maps.LatLng(centerlat, centerlon));
this.map.fitBounds(bounds);
}
GPXParser.prototype.centerAndZoomToLatLngBounds = function(latlngboundsarray) {
var boundingbox = new google.maps.LatLngBounds();
for(var i = 0; i < latlngboundsarray.length; i++) {
if(!latlngboundsarray[i].isEmpty()) {
boundingbox.extend(latlngboundsarray[i].getSouthWest());
boundingbox.extend(latlngboundsarray[i].getNorthEast());
}
}
var centerlat = (boundingbox.getNorthEast().lat() +
boundingbox.getSouthWest().lat()) / 2;
var centerlng = (boundingbox.getNorthEast().lng() +
boundingbox.getSouthWest().lng()) / 2;
this.map.setCenter(new google.maps.LatLng(centerlat, centerlng),
this.map.getBoundsZoomLevel(boundingbox));
}
GPXParser.prototype.addTrackpointsToMap = function() {
var tracks = this.xmlDoc.documentElement.getElementsByTagName("trk");
for(var i = 0; i < tracks.length; i++) {
this.addTrackToMap(tracks[i], this.trackcolour, this.trackwidth);
}
}
GPXParser.prototype.addWaypointsToMap = function() {
var waypoints = this.xmlDoc.documentElement.getElementsByTagName("wpt");
for(var i = 0; i < waypoints.length; i++) {
this.createMarker(waypoints[i]);
}
}
GPXParser.prototype.addRoutepointsToMap = function() {
var routes = this.xmlDoc.documentElement.getElementsByTagName("rte");
for(var i = 0; i < routes.length; i++) {
this.addRouteToMap(routes[i], this.trackcolour, this.trackwidth);
}
}