Rectangle has only 3 LatLngs if first and last latlng are equal
See original GitHub issueThe logic of _convertLatLngs
in L.Polygon removes the last latlng if it is the same as the first latlng. But a rectangle calls this too and then it results in having only 3 instead of 4 latlngs. --> Is not longer a valid Rectangle.
Both times the rectangle has only 3 latlngs left:
r = L.rectangle([[0,0],[0,0]])
console.log(r.getLatLngs())
r.setLatLngs([[1,1],[0,0],[0,0],[1,1]])
console.log(r.getLatLngs())
The problem code: https://github.com/Leaflet/Leaflet/blob/620bc3e9c5d282090f429ac21d28d00fbffdb3db/src/layer/vector/Polygon.js#L99-L108
I think this can be easily fixed when this code is added to L.Rectangle:
_convertLatLngs: function (latlngs) {
var result = Polyline.prototype._convertLatLngs.call(this, latlngs),
len = result.length;
// remove last point if it equals first one
if (len >= 4 && result[0] instanceof LatLng && result[0].equals(result[len - 1])) {
result.pop();
}
return result;
},
Or we remove the check and the pop
completely.
I will create a PR when I know which solution is preferred.
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
How to convert a LatLng and a radius to a LatLngBounds in ...
The issue is that there is no way to create a LatLng and a radius into a LatLngBounds. The constructor for LatLngBounds only...
Read more >L.Rectangle - WRLD3D
Returns true if the Polyline has no LatLngs. getCenter(), LatLng. Returns the center (centroid) of the polyline. getBounds(), LatLngBounds.
Read more >Shapes | Maps JavaScript API - Google Developers
The Polygon in the example above consists of four sets of LatLng coordinates, but notice that the first and last sets define the...
Read more >Documentation - a JavaScript library for interactive maps
Check this list if you are using a different version of Leaflet. ... when the map is initialized (when its center and zoom...
Read more >v3.3.1 JavaScript Library: All | Mapbox.js
options, object, If provided, it is the same options as provided to L.Map with the ... selects the first result of a query...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Sure I understand you. You are making use of operating latlngs directly, and found some case that is not handled well in your way. While it’s very easy to solve your issue locally, I’m still not convinced that it will be useful for Leaflet in general. It’s obvious that Leaflet’s Rectangle is very limited and not even close to it’s math definition.
And we cannot just take SOME properties from that definition ignoring all others. And it’s even worse Leaflet shapes are screen only, not geodesic, so they keep their properties only in current projection.
Anyway, I am keeping this issue open, may be other maintainers have own opinions.
No I’m not, because of this the method
setLatLngs()
is needed.I don’t think that it would be the right / correct way to say that rectangles can only horizontal Bounds. What would then the differences between Bounds and Rectangles (except the visualizing). No latlngs, only bounds can be set / get and is always horizontal. In my eyes then would it better to name it BoundsRectangle or something that make it clear that it is “connected” to bounds.
Math definition of a Rectangle:
So there is no definition that it must be horizontal. Src 1 Src 2
I understand when we say that leaflet don’t support rotated rectangles but I think it should be possible that it can be set from outside for example with
setLatLngs()
. (By the way this is a nice method to create rotated rectangles with two opposite corners (like bounds) _getRotatedRectangle )Ok I don’t tried it with
getBounds()
… Then it is correct, but withgetLatLngs()
only 3 latlngs are returned.This is a result of a editing function. When the user resizes the rectangle and drag the corners on top of each other, it can result in
[[0,0],[0,0]]
I see (or it looks like) that you don’t really understand me and don’t think that it is a bug that it only hat 3 latlngs, but a rectangle is existing out of bounds, bounds are out of 4 latlngs. So why should it valid to remove the last latlng, when it is equal to the first one and then it has only 3 latlngs anymore. So I suggest to add this to L.Rectangle:
No big change and it is never possible that leaflet destroys internally the rectangle latlngs. (It is something else if the user only pass 3 latlngs to
setLatLng
)And in future maybe it has a better defenition but for now I think it is a bug …