简体   繁体   English

传单:如何交换从ajax调用收到的坐标

[英]Leaflet: how to swap coordinates received from an ajax call

I am using Leaflet 1.0.3 and a few plugins including Leaflet.ajax. 我正在使用Leaflet 1.0.3和一些插件,包括Leaflet.ajax。 My L.geo.ajax call is working and returning geojson objects, however, the coordinates are reversed. 我的L.geo.ajax调用正在工作并返回geojson对象,但是坐标相反。 I created a function to fix this: 我创建了一个函数来解决此问题:

  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') } 

But my problem is I don't know where to put it. 但是我的问题是我不知道该放在哪里。 Do I run it inside my geoJson call? 我可以在geoJson呼叫中运行它吗? If so, where? 如果是这样,在哪里? Here is a snippet of the ajax call: 这是ajax调用的片段:

  var geojson = L.geoJson.ajax('http://www.iotwf.com/deployment_map/json', { pointToLayer: function (feature, latlng) { convertLatLng(latlng); ... }, onEachFeature: function(feature, layer) { ... } }); 

I am also open to other suggestions for what may fix it. 我也对可能解决该问题的其他建议持开放态度。

Welcome to SO! 欢迎来到SO!

First make sure that your coordinates are indeed reversed. 首先,请确保您的坐标确实相反。

Note that the GeoJSON format expects [longitude, latitude] , whereas Leaflet usually expects [latitude, longitude] , EXCEPT in the case of L.geoJSON() factory (and the plugin L.geoJson.ajax() ), where it automatically reads the GeoJSON order and builds the layers at the correct coordinates. 请注意,在L.geoJSON()工厂(和插件L.geoJson.ajax() )的情况下,GeoJSON格式期望[longitude, latitude] ,而Leaflet通常期望[latitude, longitude] ,但会自动读取GeoJSON顺序,并在正确的坐标处构建图层。

If your coordinates are still reversed, the appropriate correction would be obviously to correct the order in your data source directly (or whatever service outputs your data), so that you get actually compliant GeoJSON data. 如果您的坐标仍然反转,则显然应该进行适当的更正,以直接更正数据源中的顺序(或任何服务输出您的数据),以便您实际上获得兼容的GeoJSON数据。 That would solve many future headaches. 这将解决许多未来的麻烦。

If that is not possible, then indeed you could try a workaround within your script. 如果这不可能,那么实际上您可以在脚本中尝试解决方法。

The most appropriate way to do so would probably be to use the coordsToLatLng option of the L.geoJSON factory. 最合适的方法可能是使用L.geoJSON工厂的coordsToLatLng选项。

Changing its default implementation , you would get something like: 更改其默认实现 ,您将获得类似以下内容的信息:

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]);
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM