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

Leaflet: how to swap coordinates received from an ajax call(传单:如何交换从 ajax 调用接收到的坐标)
本文介绍了传单:如何交换从 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 调用接收到的坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

How do I can get a text of all the cells of the table using testcafe(如何使用 testcafe 获取表格中所有单元格的文本)
node_modules is not recognized as an internal or external command(node_modules 未被识别为内部或外部命令)
How can I create conditional test cases using Protractor?(如何使用 Protractor 创建条件测试用例?)
PhantomJS and clicking a form button(PhantomJS 并单击表单按钮)
Clicking #39;OK#39; on alert or confirm dialog through jquery/javascript?(在警报上单击“确定或通过 jquery/javascript 确认对话框?)
QunitJS-Tests don#39;t start: PhantomJS timed out, possibly due to a missing QUnit start() call(QunitJS-Tests 不启动:PhantomJS 超时,可能是由于缺少 QUnit start() 调用)