简体   繁体   English

React.js和Mapbox:使用react.js时,mapbox map无法正确呈现

[英]React.js and Mapbox: mapbox map not rendering properly when using react.js

I'm learning to use both react and mapbox,but am struggling with why my mapbox map is not rendering properly when loaded through React. 我正在学习使用react和mapbox,但我正在努力解决为什么我的mapbox地图在通过React加载时无法正确呈现。 When I load it normally (without React.js), there is no problem, as this following code works and the map appears normally: 当我正常加载它(没有React.js)时,没有问题,因为下面的代码工作并且地图正常显示:

<script src="https://api.tiles.mapbox.com/mapbox.js/v2.1.5/mapbox.js"></script>
<link rel="stylesheet" href="https://api.tiles.mapbox.com/mapbox.js/v2.1.5/mapbox.css"/>

<div> id="map"></div>

    <script>                
    var map = L.mapbox.map('map', 'blabla.blabla').setView([lat, long], 9);
    </script>

But when I do it the following way, the map often doesn't appear at all, or when it does, I see just a portion of it in the top left of the div. 但是当我按照以下方式进行操作时,地图通常根本不显示,或者当它出现时,我只看到div左上角的一部分。

<script src="path/to/react.js"></script>
<script src="https://api.tiles.mapbox.com/mapbox.js/v2.1.5/mapbox.js"></script>
<link rel="stylesheet" href="https://api.tiles.mapbox.com/mapbox.js/v2.1.5/mapbox.css"/>

<div id="react-content">
    <script src="path/to/react/content.js'></script>
</div>

//now inside react/content.js, there is a mapbox component inside an outer component. I'm leaving out the rest of the code to save space, and I believe that I am rendering the OuterComponent and the MapBoxMap component correctly inside that.

var MapBoxMap = React.createClass({
    componentDidMount: function() {
        L.mapbox.accessToken = this.props.accessToken;
        var map = L.mapbox.map(this.getDOMNode(), this.props.mapId)
            .setView([15, -105], 9);            
    },
    render: function() {
        return (
                <div id="map"></div>
        );
    }
});

Doing things like zooming, or opening the chrome developer tools seems to make the map appear sometimes. 像缩放或打开chrome开发工具这样的事情似乎有时会使地图出现。 I don't know what is going on. 我不知道发生了什么事。 Is the component not mounted properly when I attempt to render the map? 尝试渲染地图时,组件是否未正确安装? I thought that is what componentDidMount was supposed to take care of? 我以为这是componentDidMount应该照顾的? I would really appreciate any insight! 我真的很感激任何见解! Alos, this map is being rendered inside some Zurb Foundation elements, so I don't know if that makes a difference? Alos,这张地图正在一些Zurb Foundation元素中呈现,所以我不知道这是否有所作为?

You're missing the related CSS? 您是否缺少相关的CSS? You always need to set the height of the element: 您始终需要设置元素的高度:

html, body, #map {
    height: 100%;
}

I am unable to test because of React but it sounds like the map is unable to get the correct dimensions from the element. 由于React,我无法测试,但听起来地图无法从元素中获取正确的尺寸。 That usually happens if you don't set the correct CSS or the element has display: none; 如果你没有设置正确的CSS或元素有display: none;通常会发生这种情况display: none; If setting the correct CSS doesn't work for you can try invalidating the size so the map will reset itself. 如果设置正确的CSS不起作用,可以尝试使大小无效,以便地图自行重置。 L.mapbox.Map has a invalidateSize method you can call, see: L.mapbox.Map有一个你可以调用的invalidateSize方法,参见:

http://leafletjs.com/reference.html#map-invalidatesize http://leafletjs.com/reference.html#map-invalidatesize

在中使用.map<div> 标签-react.js</div><div id="text_translate"><p> 我真的很感激任何帮助。 所以该项目是一个反应项目。</p><p> 这是我获取<a href="https://i.stack.imgur.com/WelCP.png" rel="nofollow noreferrer">的内容:获取请求</a></p><p>这是我从服务器响应的内容: <a href="https://i.stack.imgur.com/e2QG3.png" rel="nofollow noreferrer">Response from server</a></p><p> 我试了一下 Postman,看看我收到的是什么类型的 json。 这是它返回的内容: <a href="https://i.stack.imgur.com/yg6IE.png" rel="nofollow noreferrer">Postman 测试</a></p><p>我遇到的问题是我无法 map 收到的数据。 一旦输入链接,我只想在首页上以一种很好的方式显示接收到的数据。</p><p> 是否可以将 map 放在 div 标签内? 或者还有另一种方法吗?</p><p> <em><strong>请注意该项目是作为功能组件编写的</strong></em></p></div> - Using .map in <div> tag - react.js

暂无
暂无

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

相关问题 React.js没有渲染? - React.js Not Rendering? 图像未在 react.js 上呈现 - images are not rendering on react.js 按钮未在 React.js 中呈现 - Button not rendering in React.js 实体未呈现react.js - entity is not rendering react.js 在React.js上进行条件渲染 - Conditional rendering on React.js 在React.js中渲染SVG - Rendering SVG in React.js 类型错误:map 不是 function(使用 React.js) - TypeError: map is not a function (Using React.js) 在 react.js 中使用 Array.map - Using Array.map in react.js 使用 react.js 从-到映射 - Map from-to using react.js 在中使用.map<div> 标签-react.js</div><div id="text_translate"><p> 我真的很感激任何帮助。 所以该项目是一个反应项目。</p><p> 这是我获取<a href="https://i.stack.imgur.com/WelCP.png" rel="nofollow noreferrer">的内容:获取请求</a></p><p>这是我从服务器响应的内容: <a href="https://i.stack.imgur.com/e2QG3.png" rel="nofollow noreferrer">Response from server</a></p><p> 我试了一下 Postman,看看我收到的是什么类型的 json。 这是它返回的内容: <a href="https://i.stack.imgur.com/yg6IE.png" rel="nofollow noreferrer">Postman 测试</a></p><p>我遇到的问题是我无法 map 收到的数据。 一旦输入链接,我只想在首页上以一种很好的方式显示接收到的数据。</p><p> 是否可以将 map 放在 div 标签内? 或者还有另一种方法吗?</p><p> <em><strong>请注意该项目是作为功能组件编写的</strong></em></p></div> - Using .map in <div> tag - react.js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM