Skip to content

Commit 7cd27a3

Browse files
NathanNathan
authored andcommitted
initial commit
0 parents  commit 7cd27a3

File tree

4 files changed

+148
-0
lines changed

4 files changed

+148
-0
lines changed

LICENSE

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Copyright (c) 2013 Nathan Mahdavi
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4+
5+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6+
7+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

dist/.DS_Store

6 KB
Binary file not shown.

examples/index.html

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
5+
<script src="http://cdn.leafletjs.com/leaflet-0.6.4/leaflet.js"></script>
6+
<script src="../src/leaflet.plotter.js"></script>
7+
<link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet-0.6.4/leaflet.css" />
8+
<!--[if lte IE 8]>
9+
<link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet-0.6.4/leaflet.ie.css" />
10+
<![endif]-->
11+
<style>
12+
body { margin:0; padding:0; }
13+
#map { position:absolute; top:0; bottom:0; width:100%; }
14+
</style>
15+
</head>
16+
<body>
17+
<div id="map"></div>
18+
<script type="text/javascript">
19+
20+
var map = L.map('map').setView([51.505, -0.09], 13);
21+
22+
L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {
23+
attribution: '&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
24+
}).addTo(map);
25+
26+
var plottedPolyline = L.Polyline.Plotter([
27+
[51.53662, -0.09218],
28+
[51.54281, -0.10712],
29+
[51.5394, -0.12394],
30+
[51.53246, -0.12772],
31+
[51.50735, -0.09253],
32+
[51.53085, -0.0563],
33+
[51.53865, -0.06008],
34+
[51.54164, -0.07502],
35+
[51.53662, -0.08875]
36+
],{
37+
weight: 5
38+
}).addTo(map);
39+
</script>
40+
</body>
41+
</html>

src/leaflet.plotter.js

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
L.Polyline.plotter = L.Polyline.extend({
2+
_lineMarkers: [],
3+
_editIcon: L.divIcon({className: 'leaflet-div-icon leaflet-editing-icon'}),
4+
_halfwayPointMarkers: [],
5+
_existingLatLngs: [],
6+
options: {
7+
weight: 2,
8+
color: '#000'
9+
},
10+
initialize: function (latlngs, options){
11+
this._setExistingLatLngs(latlngs);
12+
L.Polyline.prototype.initialize.call(this, [], options);
13+
},
14+
onAdd: function (map) {
15+
L.Polyline.prototype.onAdd.call(this, map);
16+
this._map = map;
17+
this._plotExisting();
18+
this._bindMapClick();
19+
},
20+
setLatLngs: function(latlngs){
21+
L.Polyline.prototype.setLatLngs.call(this, latlngs);
22+
},
23+
_bindMapClick: function(){
24+
this._map.on('click', this._addNewMarker, this);
25+
},
26+
_setExistingLatLngs: function(latlngs){
27+
this._existingLatLngs = latlngs;
28+
},
29+
_replot: function(){
30+
this._redraw();
31+
this._redrawHalfwayPoints();
32+
},
33+
_getNewMarker: function(latlng, options){
34+
options.draggable = true;
35+
return new L.marker(latlng, options);
36+
},
37+
_addToMapAndBindMarker: function(newMarker){
38+
newMarker.addTo(this._map);
39+
newMarker.on('click', this._removePoint, this);
40+
newMarker.on('drag', this._replot, this);
41+
},
42+
_removePoint: function(e){
43+
this._map.removeLayer(this._lineMarkers[this._lineMarkers.indexOf(e.target)]);
44+
this._lineMarkers.splice(this._lineMarkers.indexOf(e.target), 1);
45+
this._replot();
46+
},
47+
_addNewMarker: function(e){
48+
var newMarker = this._getNewMarker(e.latlng, { icon: this._editIcon });
49+
this._addToMapAndBindMarker(newMarker);
50+
this._lineMarkers.push(newMarker);
51+
this._replot();
52+
},
53+
_redrawHalfwayPoints: function(){
54+
for(index in this._halfwayPointMarkers){
55+
index = parseInt(index);
56+
this._map.removeLayer(this._halfwayPointMarkers[index]);
57+
}
58+
for(index in this._lineMarkers){
59+
index = parseInt(index);
60+
if(typeof this._lineMarkers[index + 1] === 'undefined'){
61+
return;
62+
}
63+
var halfwayMarker = new L.Marker([
64+
(this._lineMarkers[index].getLatLng().lat + this._lineMarkers[index + 1].getLatLng().lat) / 2,
65+
(this._lineMarkers[index].getLatLng().lng + this._lineMarkers[index + 1].getLatLng().lng) / 2,
66+
], { icon: this._editIcon, opacity: 0.5 }).addTo(this._map);
67+
halfwayMarker.index = index;
68+
halfwayMarker.on('click', this._addHalfwayPoint, this);
69+
this._halfwayPointMarkers.push(halfwayMarker);
70+
}
71+
},
72+
_addHalfwayPoint: function(e){
73+
var newMarker = this._getNewMarker(e.latlng, { icon: this._editIcon });
74+
this._addToMapAndBindMarker(newMarker);
75+
this._lineMarkers.splice(e.target.index + 1, 0, newMarker);
76+
this._replot();
77+
},
78+
_plotExisting: function(){
79+
for(index in this._existingLatLngs){
80+
this._addNewMarker({
81+
latlng: new L.LatLng(
82+
this._existingLatLngs[index][0],
83+
this._existingLatLngs[index][1]
84+
)
85+
});
86+
}
87+
},
88+
_redraw: function(){
89+
this.setLatLngs([]);
90+
this.redraw();
91+
for(index in this._lineMarkers){
92+
this.addLatLng(this._lineMarkers[index].getLatLng());
93+
}
94+
this.redraw();
95+
}
96+
});
97+
98+
L.Polyline.Plotter = function(latlngs, options){
99+
return new L.Polyline.plotter(latlngs, options);
100+
};

0 commit comments

Comments
 (0)