basic error handling for route request (needs more work)
This commit is contained in:
parent
f8768c5f8e
commit
db63c0dd80
1 changed files with 37 additions and 21 deletions
|
|
@ -60,32 +60,48 @@ L.BRouter = L.Class.extend({
|
||||||
},
|
},
|
||||||
|
|
||||||
getRoute: function(latLngs, cb) {
|
getRoute: function(latLngs, cb) {
|
||||||
var url = this.getUrl(latLngs);
|
var url = this.getUrl(latLngs),
|
||||||
|
xhr = new XMLHttpRequest(),
|
||||||
|
gpx;
|
||||||
|
|
||||||
if (!url) {
|
if (!url) {
|
||||||
return cb(new Error('Error getting route URL'));
|
return cb(new Error('Error getting route URL'));
|
||||||
}
|
}
|
||||||
|
|
||||||
var gpxLayer = new L.GPX(url, {
|
xhr.open('GET', url, true);
|
||||||
async: true,
|
xhr.onload = L.bind(this._handleRouteResponse, this, xhr, cb);
|
||||||
polyline_options: {
|
xhr.onerror = function(evt) {
|
||||||
opacity: 0.6
|
// TODO L.Routing._routeSegment doesn't forward err to cb
|
||||||
},
|
//cb('Server error');
|
||||||
marker_options: {
|
throw 'Server error';
|
||||||
startIconUrl: null,
|
};
|
||||||
endIconUrl: null
|
xhr.send();
|
||||||
}
|
},
|
||||||
}).on('loaded', function(e) {
|
|
||||||
|
_handleRouteResponse: function(xhr, cb) {
|
||||||
|
var gpx = xhr.responseXML;
|
||||||
|
|
||||||
|
if (xhr.status === 200 && gpx) {
|
||||||
|
// L.GPX has no XHR error handling, and expects either URL or text (not document),
|
||||||
|
// so bypass by passing null and call internal _parse_gpx_data directly
|
||||||
|
var gpxLayer = new L.GPX(null, {
|
||||||
|
polyline_options: {
|
||||||
|
opacity: 0.6
|
||||||
|
},
|
||||||
|
marker_options: {
|
||||||
|
startIconUrl: null,
|
||||||
|
endIconUrl: null
|
||||||
|
}
|
||||||
|
});
|
||||||
|
var layer = gpxLayer._parse_gpx_data(gpx, gpxLayer.options);
|
||||||
// leaflet.spin
|
// leaflet.spin
|
||||||
gpxLayer.fire('data:loaded');
|
//gpxLayer.fire('data:loaded');
|
||||||
var gpx = e.target;
|
return cb(null, layer);
|
||||||
|
} else {
|
||||||
return cb(null, gpx.getLayers()[0]);
|
// TODO L.Routing._routeSegment doesn't forward err to cb
|
||||||
})/* TODO no error handling in leaflet-gpx
|
//cb('Server error');
|
||||||
.on('error', function(e){
|
throw 'Server error ' + xhr.responseText || xhr.statusText;
|
||||||
console.error('error');
|
}
|
||||||
gpxLayer.fire('data:loaded');
|
|
||||||
return cb(new Error('Routing failed'));
|
|
||||||
})*/;
|
|
||||||
},
|
},
|
||||||
|
|
||||||
getRouteSegment: function(l1, l2, cb) {
|
getRouteSegment: function(l1, l2, cb) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue