传单:如何交换从ajax调用接收到的坐标 [英] Leaflet: how to swap coordinates received from an ajax call

查看:109
本文介绍了传单:如何交换从ajax调用接收到的坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Leaflet 1.0.3和一些插件,包括Leaflet.ajax。我的L.geo.ajax调用正在工作并返回geojson对象,但是坐标相反。我创建了一个函数来解决此问题:

I am using Leaflet 1.0.3 and a few plugins including Leaflet.ajax. My L.geo.ajax call is working and returning geojson objects, however, the coordinates are reversed. 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')
    }

但是我的问题是我不知道该放在哪里。我可以在geoJson呼叫中运行它吗?如果是这样,在哪里?这是ajax调用的代码段:

But my problem is I don't know where to put it. Do I run it inside my geoJson call? If so, where? Here is a snippet of the ajax call:

    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.

推荐答案

欢迎使用!

首先请确保您的坐标确实是相反的。

First make sure that your coordinates are indeed reversed.

请注意,GeoJSON格式期望 [经度,纬度] ,而Leaflet通常期望 [经度,纬度] ,对于 L.geoJSON()工厂(以及插件 L.geoJson.ajax()),它会自动读取GeoJSON订单并以正确的坐标构建图层。

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.

坐标仍然颠倒,适当的纠正显然是直接纠正数据源中的顺序(或任何服务输出您的数据),以便您实际上获得兼容的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. That would solve many future headaches.

如果这不可能,那么的确可以在脚本中尝试解决方法。

If that is not possible, then indeed you could try a workaround within your script.

最合适的方法可能是使用 < L.geoJSON 工厂的code> coordsToLatLng 选项。

The most appropriate way to do so would probably be to use the coordsToLatLng option of the L.geoJSON factory.

更改其默认实现,您将得到类似的内容:

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

这篇关于传单:如何交换从ajax调用接收到的坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆