update leaflet-routing

This commit is contained in:
Norbert Renner 2014-05-14 11:40:52 +02:00
parent 7936afdd66
commit f1cae03a80
10 changed files with 245 additions and 40 deletions

View file

@ -99,7 +99,8 @@ L.Routing.Draw = L.Handler.extend({
// Visible Marker
if (!this._marker) {
this._marker = new L.Marker(this._map.getCenter(), {
icon: this.options.icons.normal
icon: (this.options.icons.draw ? this.options.icons.draw : new L.Icon.Default())
,opacity: (this.options.icons.draw ? 1.0 : 0.0)
,zIndexOffset: this.options.zIndexOffset
,clickable: false
});
@ -224,7 +225,7 @@ L.Routing.Draw = L.Handler.extend({
*/
,_show: function() {
this._hidden = false;
this._marker.setOpacity(1.0);
this._marker.setOpacity(this.options.icons.draw ? 1.0 : 0.0);
this._trailer.setStyle({opacity: 0.2});
}

View file

@ -17,7 +17,7 @@ L.Routing = L.Control.extend({
// CONSTANTS
,statics: {
VERSION: '0.0.2-dev'
VERSION: '0.1.1-dev'
}
// OPTIONS
@ -27,6 +27,7 @@ L.Routing = L.Control.extend({
start: new L.Icon.Default()
,end: new L.Icon.Default()
,normal: new L.Icon.Default()
,draw: new L.Icon.Default()
}
,zIndexOffset: 2000
,routing: {
@ -37,6 +38,12 @@ L.Routing = L.Control.extend({
,sensitivity: 10 // snapping sensitivity
,vertexonly: false // vertex only snapping
}
,shortcut: {
draw: {
enable: 68, // char code for 'd'
disable: 81 // char code for 'q'
}
}
}
/**
@ -79,7 +86,10 @@ L.Routing = L.Control.extend({
//L.DomUtil.disableTextSelection();
//this._tooltip = new L.Tooltip(this._map);
//this._tooltip.updateContent({ text: L.drawLocal.draw.marker.tooltip.start });
L.DomEvent.addListener(this._container, 'keyup', this._keyupListener, this);
if (this.options.shortcut) {
L.DomEvent.addListener(this._container, 'keyup', this._keyupListener, this);
}
this._draw = new L.Routing.Draw(this, {
icons: this.options.icons
@ -138,9 +148,16 @@ L.Routing = L.Control.extend({
delete this.options;
}
/**
* Called whenever a waypoint is clicked
*
* @access private
*
* @param <L.Event> e - click event
*/
,_waypointClickHandler: function(e) {
this.removeWaypoint(e.marker, function() {
console.log(arguments);
// console.log(arguments);
});
}
@ -152,7 +169,7 @@ L.Routing = L.Control.extend({
* @param <L.Marker> marker - new waypoint marker (can be ll)
* @param <L.Marker> prev - previous waypoint marker
* @param <L.Marker> next - next waypoint marker
* @param <Function> cb - callback method
* @param <Function> cb - callback method (err, marker)
*
* @return void
*/
@ -501,27 +518,134 @@ L.Routing = L.Control.extend({
if (current === null) { return geojson; }
geojson.properties.waypoints.push([current.getLatLng().lng, current.getLatLng().lat]);
// First waypoint marker
geojson.properties.waypoints.push({
coordinates: [current.getLatLng().lng, current.getLatLng().lat],
_index: 0
});
while (current._routing.nextMarker) {
var next = current._routing.nextMarker
geojson.properties.waypoints.push([next.getLatLng().lng, next.getLatLng().lat]);
var next = current._routing.nextMarker;
// Line segment
var tmp = current._routing.nextLine.getLatLngs();
for (var i = 0; i < tmp.length; i++) {
if (tmp[i].alt && (typeof enforce2d === 'undefined' || enforce2d === false)) {
geojson.coordinates.push([tmp[i].lat, tmp[i].lng, tmp[i].alt]);
geojson.coordinates.push([tmp[i].lng, tmp[i].lat, tmp[i].alt]);
} else {
geojson.coordinates.push([tmp[i].lat, tmp[i].lng]);
geojson.coordinates.push([tmp[i].lng, tmp[i].lat]);
}
}
// Waypoint marker
geojson.properties.waypoints.push({
coordinates: [next.getLatLng().lng, next.getLatLng().lat],
_index: geojson.coordinates.length-1
});
// Next waypoint marker
current = current._routing.nextMarker;
}
return geojson
}
/**
* Import route from GeoJSON
*
* @access public
*
* @param <object> geojson - GeoJSON object with waypoints
* @param <object> opts - parsing options
* @param <function> cb - callback method (err)
*
* @return undefined
*
*/
,loadGeoJSON: function(geojson, opts, cb) {
var $this, oldRouter, index, waypoints;
$this = this;
// Check for optional options parameter
if (typeof opts === 'function' || typeof opts === 'undefined') {
cb = opts;
opts = {}
}
// Set default options
opts.waypointDistance = opts.waypointDistance || 50;
opts.fitBounds = opts.fitBounds || true;
// Check for waypoints before processing geojson
if (!geojson.properties || !geojson.properties.waypoints) {
if (!geojson.properties) { geojson.properties = {} };
geojson.properties.waypoints = [];
for (var i = 0; i < geojson.coordinates.length; i = i + opts.waypointDistance) {
geojson.properties.waypoints.push({
_index: i,
coordinates: geojson.coordinates[i].slice(0, 2)
});
}
if (i > geojson.coordinates.length-1) {
geojson.properties.waypoints.push({
_index: geojson.coordinates.length-1,
coordinates: geojson.coordinates[geojson.coordinates.length-1].slice(0, 2)
});
}
}
index = 0;
oldRouter = $this._router;
waypoints = geojson.properties.waypoints;
// This is a fake router.
//
// It is currently not possible to add a waypoint with a known line segment
// manually. We are hijacking the router so that we can intercept the
// request and return the correct linesegment.
//
// It you want to fix this; please make a patch and submit a pull request on
// GitHub.
$this._router = function(m1, m2, cb) { var start =
waypoints[index-1]._index; var end = waypoints[index]._index+1;
return cb(null, L.GeoJSON.geometryToLayer({
type: 'LineString',
coordinates: geojson.coordinates.slice(start, end)
}));
};
// Clean up
end = function() {
$this._router = oldRouter; // Restore router
// Set map bounds based on loaded geometry
setTimeout(function() {
if (opts.fitBounds) {
$this._map.fitBounds(L.polyline(L.GeoJSON.coordsToLatLngs(geojson.coordinates)).getBounds());
}
if (typeof cb === 'function') { cb(null); }
}, 0);
}
// Add waypoints
add = function() {
if (!waypoints[index]) { return end() }
var coords = waypoints[index].coordinates;
var prev = $this._waypoints._last;
$this.addWaypoint(L.latLng(coords[1], coords[0]), prev, null, function(err, m) {
add(++index);
});
}
add();
}
/**
* Start (or continue) drawing
*
@ -583,9 +707,9 @@ L.Routing = L.Control.extend({
* @return void
*/
,_keyupListener: function (e) {
if (e.keyCode === 27) {
if (e.keyCode === this.options.shortcut.draw.disable) {
this._draw.disable();
} else if (e.keyCode === 77) {
} else if (e.keyCode === this.options.shortcut.draw.enable) {
this._draw.enable();
}
}

View file

@ -18,6 +18,11 @@ L.Util.extend(L.LineUtil, {
minDist = Infinity;
minPoint = latlng;
minPoint._feature = null; // containing layer
if (!opts || !opts.layers || !opts.layers.length) {
return minPoint;
}
map = opts.layers[0]._map; // @todo check for undef
for (i = 0; i < opts.layers.length; i++) {