Skip to content

Commit

Permalink
Fix bug when circle is the only layer on the map (#627) (patch)
Browse files Browse the repository at this point in the history
* fix bug when circle is the only layer on the map

* remove not necessary code

* Fix Bug after Dragging
  • Loading branch information
Falke-Design committed Sep 3, 2020
1 parent 7348490 commit 0954f68
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 14 deletions.
2 changes: 2 additions & 0 deletions src/js/Edit/L.PM.Edit.Circle.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ Edit.Circle = Edit.extend({
initialize(layer) {
this._layer = layer;
this._enabled = false;
// create polygon around the circle border
this._updateHiddenPolyCircle();
},
applyOptions() {
if (this.options.snappable) {
Expand Down
29 changes: 17 additions & 12 deletions src/js/Edit/L.PM.Edit.CircleMarker.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ Edit.CircleMarker = Edit.extend({
initialize(layer) {
this._layer = layer;
this._enabled = false;
// create polygon around the circle border
this._updateHiddenPolyCircle();
},
applyOptions() {
// Use the not editable and only draggable version
Expand Down Expand Up @@ -290,21 +292,24 @@ Edit.CircleMarker = Edit.extend({
marker.off('pm:dragstart', this._unsnap, this);
},
_updateHiddenPolyCircle() {
const pointA = this._layer._map.project(this._layer.getLatLng());
const pointB = L.point(pointA.x + this._layer.getRadius(), pointA.y);
const radius = this._layer.getLatLng().distanceTo(this._layer._map.unproject(pointB));
const map = this._layer._map || this._map;
if(map) {
const pointA = map.project(this._layer.getLatLng());
const pointB = L.point(pointA.x + this._layer.getRadius(), pointA.y);
const radius = this._layer.getLatLng().distanceTo(map.unproject(pointB));

const _layer = L.circle(this._layer.getLatLng(), this._layer.options);
_layer.setRadius(radius);
const _layer = L.circle(this._layer.getLatLng(), this._layer.options);
_layer.setRadius(radius);

if (this._hiddenPolyCircle) {
this._hiddenPolyCircle.setLatLngs(Utils.circleToPolygon(_layer, 200).getLatLngs());
} else {
this._hiddenPolyCircle = Utils.circleToPolygon(_layer, 200);
}
if (this._hiddenPolyCircle) {
this._hiddenPolyCircle.setLatLngs(Utils.circleToPolygon(_layer, 200).getLatLngs());
} else {
this._hiddenPolyCircle = Utils.circleToPolygon(_layer, 200);
}

if (!this._hiddenPolyCircle._parentCopy) {
this._hiddenPolyCircle._parentCopy = this._layer
if (!this._hiddenPolyCircle._parentCopy) {
this._hiddenPolyCircle._parentCopy = this._layer
}
}
}
});
6 changes: 4 additions & 2 deletions src/js/Mixins/Dragging.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,10 @@ const DragMixin = {
return false;
}

if(this._layer instanceof L.CircleMarker) {
this._updateHiddenPolyCircle();

// update the hidden circle border after dragging
if(this._layer instanceof L.CircleMarker){
this._layer.pm._updateHiddenPolyCircle();
}

// timeout to prevent click event after drag :-/
Expand Down
5 changes: 5 additions & 0 deletions src/js/Mixins/Snapping.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@ const SnapMixin = {
this._snapList
);

// if no layers found. Can happen when circle is the only visible layer on the map and the hidden snapping-border circle layer is also on the map
if(Object.keys(closestLayer).length === 0){
return false;
}

const isMarker =
closestLayer.layer instanceof L.Marker ||
closestLayer.layer instanceof L.CircleMarker;
Expand Down

0 comments on commit 0954f68

Please sign in to comment.