传单的坐标反射问题 [英] Coordinate Reflection issue with Leaflet
问题描述
祝大家节日快乐.我在尝试使用L.geoJson()绘制多边形图层时注意到一个有趣的行为怪癖.考虑以下代码:
Salutations all and happy holidays. I Noticed an interesting behavioral quirk while trying to draw polygon layers with L.geoJson(). consider the following code:
var polygonCoords = [
{"type": "Feature",
"properties": {"group": "Violations"},
"geometry": {
"type" : "Polygon",
"coordinates": [[
[-107.69348, 43.22519],
[-105.48523, 42.99259],
[-107.7594, 42.26105]
]]
}
}];
和
var polygons = L.polygon([
[43.22519, -107.69348],
[42.99259, -105.48523],
[42.26105, -107.7594]
]);
现在,两者都在各自的上下文中工作.我只是想知道为什么必须反映L.polygon()中的坐标矩阵,以便像这样传递给L.goeJson()时显示人们期望的坐标:
Now, both work in their respective contexts. I was just wondering why the coordinate matrix within L.polygon() has to be reflected in order to show up where one expects it to be when passed into L.goeJson() like so:
var jsonPoly = L.geoJson(polygonCoords, {
style: function(feature) {
if (feature.properties.group == "Violations") {
return {color: "#ff0000"};
}
}
});
这是传单内的疏忽吗?另外,有没有办法通过 toGeoJson(polygons) 来自动化这种反射?
Or is this an oversight within leaflet? Also, is there a way to automate this reflection with say toGeoJson(polygons)?
非常感谢.
推荐答案
创建geoJson图层时,坐标应符合GeoJSON标准(x,y,z或lng,lat,海拔)(
When creating a geoJson layer the coordinates are expected to match the GeoJSON standard (x,y,z or lng, lat, altitude) (GeoJSON position specs)
如果您的坐标不是这种格式的字符串是GeoJSON,则可以使用自定义的coordsToLatLng函数创建GeoJSON图层,该函数将处理此转换为标准格式(com/reference.html#geojson-coordstolatlng"rel =" nofollow>传单文档)
If you have string of GeoJSON where your coordinates are not in this format, you can create your GeoJSON layer with a custom coordsToLatLng function that will handle this conversion to the standard's format (Leaflet Doc)
如果您有一个多边形图层并将其添加到现有的GeoJSON要素组中,则可以执行以下操作:
If you have a polygon layer and want to add it to an existing GeoJSON feature group you can do something like:
var polygons = L.polygon([
[43.22519, -107.69348],
[42.99259, -105.48523],
[42.26105, -107.7594]
]);
var gg = polygons.toGeoJSON();
var jsonFeatureGroup = L.geoJson().addTo(map);
jsonFeatureGroup.addData(gg);
map.fitBounds(jsonFeatureGroup.getBounds());
这篇关于传单的坐标反射问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!