我正在使用传单1.0.3和一些插件,包括Leaflet.ajax。我的L.geo.ajax调用正在工作,并返回geojson对象,但是,坐标是相反的。我创建了一个函数来修复这个问题:
var convertLatLng = function (latlng) {
var temp = latlng[y];
latlng[y] = latlng[x];
latlng[x] = temp;
convertedLatLng = latlng;
return convertedLatLng;
console.log('this function is running')
}
但我的问题是我不知道该把它放在哪里。我在我的geoJson调用中运行它吗?如果是,在哪里?下面是ajax调用的一个片段:
var geojson = L.geoJson.ajax('http://www.iotwf.com/deployment_map/json', {
pointToLayer: function (feature, latlng) {
convertLatLng(latlng);
...
},
onEachFeature: function(feature, layer) {
...
}
});
对于如何解决这个问题,我也愿意听取其他建议。
发布于 2017-04-21 10:37:38
欢迎来到这里!
首先,确保你的坐标确实是颠倒的。
注意,GeoJSON格式需要[longitude, latitude]
,而传单通常期望[latitude, longitude]
,但L.geoJSON()
工厂(和插件L.geoJson.ajax()
)除外,它会自动读取GeoJSON顺序,并在正确的坐标下构建层。
如果您的坐标仍然相反,则适当的更正显然是直接更正数据源中的顺序(或任何服务输出您的数据),从而得到实际兼容的GeoJSON数据。这将解决许多未来的头痛。
如果这是不可能的,那么您确实可以在脚本中尝试一个解决方案。
最合适的方法可能是使用coordsToLatLng
工厂的L.geoJSON
选项。
更改其默认实现,您将得到如下内容:
L.geoJson.ajax(url, {
coordsToLatLng: function (coords) {
// latitude , longitude, altitude
//return new L.LatLng(coords[1], coords[0], coords[2]); //Normal behavior
return new L.LatLng(coords[0], coords[1], coords[2]);
}
});
https://stackoverflow.com/questions/43549199
复制相似问题