reactjs - Openlayers GeoJSON 位于错误的位置

标签 reactjs openlayers geojson

我是 Openlayers 新手,对传单没什么经验。在传单中工作的 GeoJSON 位于完全错误的位置(小红点)

我可以理解这与投影有关,但由于我是新人,我找不到可用的投影。由于 GeoJSON 在传单中完美运行,我假设可以找到其 EPSG:3857 文件 here

enter image description here

如果你放大得很远,它就会被投影 enter image description here

import React, { useEffect, useState } from 'react'
import isDefined from '../../lib/utils/is-defined'
import OlMap from 'ol/Map'
import OlView from 'ol/View'
import OlLayerTile from 'ol/layer/Tile'
import OlSourceOSM from 'ol/source/Osm'
import GeoJSON from 'ol/format/GeoJSON'
import VectorLayer from 'ol/layer/Vector'
import VectorSource from 'ol/source/Vector'
import { Circle as CircleStyle, Fill, Stroke, Style } from 'ol/style'
import germanyFullGeoJSON from '../../assets/germany_full.json'
import 'ol/ol.css'

const styles = {
  MultiPolygon: new Style({
    stroke: new Stroke({
      color: 'red',
      width: 4
    }),
    fill: new Fill({
      color: 'red'
    })
  })
}

const styleFunction = function (feature) {
  return styles[feature.getGeometry().getType()]
}

const Map = () => {
  const [map, setMap] = useState(null)
  const [zoom, setZoom] = useState(5)
  const [center, setCenter] = useState([546000, 6868000])

  useEffect(() => {
    const format = new GeoJSON()
    const features = format.readFeatures(germanyFullGeoJSON)

    const vectorLayer = new VectorLayer({
      source: new VectorSource({
        features
      }),
      style: styleFunction
    })
    const tempMap = new OlMap({
      target: 'map',
      layers: [
        new OlLayerTile({
          source: new OlSourceOSM()
        }),
        vectorLayer
      ],
      view: new OlView({
        center,
        zoom
      })

    })
    setMap(tempMap)
  }, [])

  if (isDefined(map)) {
    map.getView().setCenter(center)
    map.getView().setZoom(zoom)
    map.render()
  }
  return (
    <div id="map" style={{ width: '100%', height: '100%' }} />
  )
}

export default Map

最佳答案

OpenLayers的默认投影是EPSG:3857 。因此,您需要转换 EPSG:4326 中给出的德国 GeoJSON 特征。尝试按照以下设置修复格式 featureProjection因此:

const format = new GeoJSON({ featureProjection: 'EPSG:3857' });

关于reactjs - Openlayers GeoJSON 位于错误的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71007336/

相关文章:

html - 文本从 div 的底部开始

javascript - 查找对象的 super 嵌套数组的位置

python - 使用 python 使用 .geojson 文件查找位置

javascript - ES6 胖箭头 react this.setState ESLint 错误

javascript - 如何访问 iframe 中 URL 的参数?

javascript - Openlayers - 在 map 上的弹出窗口中显示 geojson 文件中的数据

javascript - OpenLayers 弹出 contentDiv => 按钮中的 qx.ui.root.Inline 无法单击!

geojson - 确定某个位置的描笼涯

javascript - 如何使用d3,javascript和json文件将 map 划分为邮政编码?

android - React native-fbsdk 未知问题?