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

时间:2023-03-18
本文介绍了传单:如何交换从 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.

推荐答案

欢迎来到 SO!

首先确保你的坐标确实是颠倒的.

First make sure that your coordinates are indeed reversed.

请注意,GeoJSON 格式需要 [longitude, latitude],而 Leaflet 通常需要 [latitude, longitude],除了 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 工厂的>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 调用接收到的坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:沿两点之间的直线获取纬度和经度点,按百分比 下一篇:Leaflet:当边界改变时触发一个事件

相关文章