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

1
.gitignore vendored
View file

@ -1,4 +1,5 @@
bower_components/leaflet-routing/libs/
bower_components/leaflet-routing/examples/
bower_components/Leaflet.Elevation/*
!bower_components/Leaflet.Elevation/dist/
!bower_components/Leaflet.Elevation/src/

View file

@ -1,11 +1,11 @@
{
"name": "leaflet-routing",
"homepage": "https://github.com/Turistforeningen/leaflet-routing",
"_release": "f78c22ed00",
"_release": "2cffb221af",
"_resolution": {
"type": "branch",
"branch": "gh-pages",
"commit": "f78c22ed001788761b81172ef746a357a92e2f5f"
"commit": "2cffb221af25cf624065b9c4d4cfdd299aea5746"
},
"_source": "git://github.com/Turistforeningen/leaflet-routing.git",
"_target": "gh-pages",

View file

@ -0,0 +1,38 @@
Changelog
=========
### 0.2.0 (Unreadlesed)
**Features**
* Add option for overriding draw shortcuts (#23)
* Add option for disabling drawing marker (#18)
* Disable shortcuts by setting `options.shortcut` to `false`.
* Add support for loading GeoJSON without waypoints (#16).
* Add option to #loadGeoJSON() to disable map fit bounds.
**Breaking changes**
* Default shortcut key for draw enable is `d`
* Default shortcut key for draw disable is `q`
### 0.1.1 March 11, 2014
**Features**
* Add changelog overview (#14)
* Add Change map bounds when loading GeoJSON (#13)
**Bugfixes**
* Fix undefined evaluation when snapping layer is not avaiable (#15)
* Fail gracefully when loading invalid GeoJSON (#12)
### 0.1.0 March 10, 2014
* Implements `#loadGeoJSON()` method (#3)
#### Backwards compability note
* Format of `properties.waypoints` in `#toGeoJSON()` is changed according to #3.

View file

@ -1,22 +1,22 @@
Copyright (c) 2013, Turistforeningen, Hans Kristian Flaatten
Copyright (c) 2014, Den Norske Turistforening
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are
permitted provided that the following conditions are met:
Redistribution and use in source and binary forms, with or without modification, are
permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this list of
conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this list
1. Redistributions of source code must retain the above copyright notice, this list of
conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this list
of conditions and the following disclaimer in the documentation and/or other materials
provided with the distribution.
provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

View file

@ -1,7 +1,11 @@
Routing in Leaflet
==================
Leaflet.Routing
===============
Research and development of a Leaflet.js routing module. The module will be capable of the routing markers and returning geojson in various ways.
Leaflet.Routing is a routing controller for the popular Leaflet mapping
framework. The module provides an intuitive interface for routing paths between
waypoints using any user specified routing service. A demo using the OSM data
can be found
[here](http://turistforeningen.github.io/leaflet-routing/examples/osm.html).
![Prototype Routing using Leaflet](https://raw.github.com/Turistforeningen/leaflet-routing/gh-pages/images/promo.gif)
@ -11,13 +15,19 @@ Research and development of a Leaflet.js routing module. The module will be capa
var routing = new L.Routing({
position: 'topright'
,routing: {
router: myRouter
router: myRouterFunction
}
,snapping: {
layers: [snapping]
layers: [mySnappingLayer]
,sensitivity: 15
,vertexonly: false
}
,shortcut: {
draw: {
enable: 68 // 'd'
,disable: 81 // 'q'
}
}
});
map.addControl(routing);
```
@ -35,11 +45,13 @@ routing.routing(true);
```
### Enable Snapping `NOT IMPLEMETED`
```javascript
routing.snapping(true);
```
### Recalculate the complete route by routing each segment
```javascript
routing.rerouteAllSegments(callback);
```
@ -51,29 +63,52 @@ var first = routing.getFirst();
```
### Get last waypoint
```javascript
var last = routing.getLast();
```
### Get all waypoints
```javascript
var waypointsArray = routing.getWaypoints();
```
### Routing to Polyline
```javascript
var polyline = routing.toPolyline();
```
### To GeoJSON
```javascript
var geoJSON3D = routing.toGeoJSON();
var geoJSON2D = routing.toGeoJSON(false);
```
### Load GeoJSON
Load GeoJSON with and without `properties.waypoints`.
#### Options
* `number` waypointDistance - distance between inserted waypoints for GeoJSON without waypoints.
* `boolean` fitBounds - fit map arround loaded GeoJSON.
```javascript
routing.loadGeoJSON(geojson, [options], function(err) {
if (err) {
console.log(err);
} else {
console.log('Finished loading GeoJSON');
}
});
```
## Copyright
Copyright (c) 2013, Turistforeningen
Copyright (c) 2014, Den Norske Turistforening
All rights reserved.
@ -83,3 +118,4 @@ Redistribution and use in source and binary forms, with or without modification,
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

View file

@ -141,7 +141,7 @@ var routing, data;
var coords = routing.toGeoJSON().coordinates;
var data = [];
for (var i = 0; i < coords.length; i++) {
data.push(coords[i][1] + ' ' + coords[i][0]);
data.push(coords[i][0] + ' ' + coords[i][1]);
}
data = 'LINESTRING(' + data.join(',') + ')';
$.post('http://mintur.ut.no/lib/ajax/post_geom.php?api_key=' + apiKey + '&tp_id=' + id, {coords: data}, function(data) {

View file

@ -38,7 +38,7 @@
<script src="src/L.Routing.Storage.js"></script>
<script src="src/L.Routing.Draw.js"></script>
<script src="src/L.Routing.Edit.js"></script>
<script src="http://twitter.github.io/typeahead.js/releases/latest/typeahead.js"></script>
<script src="http://twitter.github.io/typeahead.js/releases/0.9.3/typeahead.min.js"></script>
<script src="app.js"></script>
</body>
</html>

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++) {