如何将OpenLayers与react-starter-kit一起使用?

问题描述 投票:3回答:1

我在基于react-starter-kit的项目中运行OpenLayers3 Map时遇到问题。 我正在尝试添加简单的地图来反应组件。 我做的是:

  1. 运行npm install openlayers
  2. import ol from 'openlayers'添加到ContactPage.js
  3. 创建简单的地图

     componentDidMount(){ var map = new ol.Map({ target: 'map', layers: [ new ol.layer.Tile({ source: new ol.source.MapQuest({layer: 'sat'}) }) ], view: new ol.View({ center: ol.proj.transform([37.41, 8.82], 'EPSG:4326', 'EPSG:3857'), zoom: 4 }) }); } 
  4. 将div添加到渲染函数的<div id="map" className="map"></div> jsx部分<div id="map" className="map"></div>

  5. 运行npm start。

我收到一个错误:

...\node_modules\openlayers\dist\ol.js:7
    module.exports = factory();
                     ^
TypeError: Cannot use 'in' operator to search for 'geolocation' in undefined
    at C:\Git Repos\traces\node_modules\openlayers\dist\ol.js:160:823
    at OPENLAYERS (C:\Git Repos\traces\node_modules\openlayers\dist\ol.js:7:22)
    at Object.<anonymous> (C:\Git Repos\traces\node_modules\openlayers\dist\ol.js:13:2)
    at Module._compile (module.js:460:26)
    at Object.Module._extensions..js (module.js:478:10)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)
    at Module.require (module.js:365:17)
    at require (module.js:384:17)
    at Object.<anonymous> (C:\Git Repos\traces\build\webpack:\external "openlayers":1:1)

这看起来像OL3中的webpack连接问题 ,已经讨论和修复。 但它仍然不适合我。

我还尝试将带有OL3的脚本标记直接添加到index.html并删除了组件中ol的导入。 虽然我想使用Webpack来处理依赖关系,但这种方法很有效。

在此先感谢您的帮助!

javascript node.js reactjs openlayers-3 webpack
1个回答
2
投票

一年后,我创建了这个最小的包装反应开放层

它是用TypeScript编写的OpenLayer 3+的最小React包装器

© www.soinside.com 2019 - 2024. All rights reserved.