简体   繁体   English

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

[英]How to use OpenLayers with react-starter-kit?

I have a problem with running OpenLayers3 Map in a project based on react-starter-kit . 我在基于react-starter-kit的项目中运行OpenLayers3 Map时遇到问题。 I'm trying to add simple map to react component. 我正在尝试添加简单的地图来反应组件。 What I did was to: 我做的是:

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

     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. Add a div to jsx part of render function <div id="map" className="map"></div> 将div添加到渲染函数的<div id="map" className="map"></div> jsx部分<div id="map" className="map"></div>

  5. Run npm start. 运行npm start。

I get an error: 我收到一个错误:

...\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)

This looks like a webpack connected issue in OL3 which was discussed and fixed. 这看起来像OL3中的webpack连接问题 ,已经讨论和修复。 However it still doesn't work for me. 但它仍然不适合我。

I also tried to add script tag with OL3 directly to index.html and removed import of ol in components. 我还尝试将带有OL3的脚本标记直接添加到index.html并删除了组件中ol的导入。 This approach works, although I'd like to use Webpack to take care of dependencies. 虽然我想使用Webpack来处理依赖关系,但这种方法很有效。

Thanks in advance for any help! 在此先感谢您的帮助!

One year later, I created this minimal wrapper react-openlayers . 一年后,我创建了这个最小的包装反应开放层

It is a minimal React wrapper of OpenLayer 3+ written in TypeScript 它是用TypeScript编写的OpenLayer 3+的最小React包装器

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM