Reputation: 137
When using leaflet.js to render polygon. The following examples have different results.
const map = L.map('map', {
preferCanvas: true,
zoomControl: false,
attributionControl: false
});
const latlngs = [
[
6,
5
],
[
7,
1
],
[
9,
2
]
];
const polygon = L.polygon(latlngs, {color: 'red'}).addTo(map);
map.fitBounds(polygon.getBounds());
...
const latlngs = [
[
116,
115
],
[
117,
111
],
[
119,
112
]
];
const polygon = L.polygon(latlngs, {color: 'red'}).addTo(map);
map.fitBounds(polygon.getBounds());
Upvotes: 1
Views: 2348
Reputation: 1
If you want to create a polygon based on 4 dots on latitude and longitude, here is the code
var polygons = [];
var currentPolygonCoords = [];
function onMapClick(e) {
currentPolygonCoords.push(e.latlng);
L.marker(e.latlng).addTo(map);
if (currentPolygonCoords.length === 4) {
var polygon = L.polygon(currentPolygonCoords, { color: 'purple' }).addTo(map);
polygons.push(polygon);
currentPolygonCoords = [];
}
}
map.on('click', onMapClick);
Upvotes: 0
Reputation: 2042
The reason is that Leaflet uses latitude and longitude by default and projects it on a plane using a Web Mercator projection. On your second polygon, the coordinates are out of bounds for latitude.
If you just need to draw a triangle in "pixel" coordinates, you can use CRS.Simple.
const map = L.map('map', {
preferCanvas: true,
zoomControl: false,
attributionControl: false,
// Add this line:
crs: L.CRS.Simple
});
See JSFiddle: http://jsfiddle.net/ekqvwo76/
Upvotes: 3