本文介绍了如何在JavaScript Google Map API上用仪表显示geojson数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

GeoJson文件的坐标以[X,Y]米为单位,而不是[lng,lat].如何在Google地图上显示它?

GeoJson file coordinates are in [X, Y] meters not in [lng, lat]. How to display it on google map?

{
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "geometry": {
        "type": "MultiPolygon",
        "coordinates": [
          [
            [
              [
                360590,
                555610
              ],
              [
                360590,
                555555.0128
              ],
              [
                360590,
                555540
              ],
              [
                360592.4439,
                555540
              ],
              [
                360600,
                555540
              ],
              [
                360600,
                555518.8277
              ]
            ]
          ]
        ]
      }
    }
  ]
}

在这里[360590,555610]-[X,Y]坐标以米为单位,现在我们需要在Google地图上显示此坐标,对此有什么解决方案吗?

here, [360590, 555610] - [X, Y] coordinates is in meters, Now we need to display this coordinates on google map, Is there any solution for this?

我们还必须使用addGeoJson或loadGeoJson方法,因为GeoJson文件中有200MB的数据.

also we must have to use addGeoJson or loadGeoJson method because we have 200MB data in GeoJson file.

推荐答案

在后端进行转换会更好,但也有一种JS方式-使用 proj4js epsg .io 查找相应的基准.

Converting in the backend would be better but there's a JS way too -- using proj4js and epsg.io to find the corresponding datums.

重要点:线性环的第一个和最后一个位置必须相同.否则,您的GeoJSON 无效.所以请注意我的points数组从同一点开始和结束.

Important point: the first and last positions of the linear ring need to be identical. Otherwise your GeoJSON is not valid. So notice how my points array starts and ends w/ the same point.

安装/导入proj4js之后:

// from https://epsg.io/27700
const EPSG_27700_DATUM = "+proj=tmerc +lat_0=49 +lon_0=-2 +k=0.9996012717 +x_0=400000 +y_0=-100000 +ellps=airy +towgs84=446.448,-125.157,542.06,0.15,0.247,0.842,-20.489 +units=m +no_defs";

// from https://epsg.io/4326
const WGS84_DATUM = "+proj=longlat +datum=WGS84 +no_defs";

const points = [[360590,555610],[360590,555555.0128],[360590,555540],[360592.4439,555540],[360600,555540],[360600,555518.8277],[360590,555610]];

const converted_points = points.map(([lon, lat]) => {
    return proj4(EPSG_27700_DATUM, WGS84_DATUM, [lon, lat]);
});

console.info({ converted_points });

验证您的多边形确实在GB内:

Validating that your polygon is indeed inside of GB:

这篇关于如何在JavaScript Google Map API上用仪表显示geojson数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 15:20