首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从MapLayer组件访问地图(工作在以前的反应-传单版本)

从MapLayer组件访问地图(工作在以前的反应-传单版本)
EN

Stack Overflow用户
提问于 2017-11-09 22:28:10
回答 1查看 1.8K关注 0票数 1

我现正尝试在单张地图上展示两点之间的路线。为此目的,我正在使用传单路由机。但是,在将地图对象传递给传单路线组件时,我有问题。

map_container.js

代码语言:javascript
复制
...
        return (
            <Map ref='map' center={position} zoom={this.state.zoom}>
                <TileLayer
                    attribution="&amp;copy <a href=&quot;http://osm.org/copyright&quot;>OpenStreetMap</a> contributors"
                    url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
                />
                <Routing map={this.refs.map} from={[51.599684, 15.27539]} to={[51.602292, 15.295128]} />
            </Map>
        )
...

routing.js

代码语言:javascript
复制
import {MapLayer} from 'react-leaflet';
import L from 'leaflet';
import 'leaflet-routing-machine';

export default class RoutingMachine extends MapLayer {
    componentWillMount() {
        super.componentWillMount();
        this.leafletElement.addTo(this.props.map);
    }

    render() {
        return null;
    }

    createLeafletElement (props) {
        const {from, to} = this.props;
        console.log(this.props)
        var leafletElement = L.Routing.control({
            waypoints: [
                L.latLng(from[0], from[1]),
                L.latLng(to[0], to[1]),
            ],
        });
        return leafletElement;
    }
}

我发现的错误:

代码语言:javascript
复制
{map: undefined, from: Array(2), to: Array(2)}

bundle.js:69506 Uncaught TypeError: Cannot read property 'getSize' of undefined
    at NewClass.onAdd (bundle.js:69506)
    at NewClass.onAdd (bundle.js:68679)
    at NewClass.addTo (bundle.js:26718)

但是最有趣的是--所有的东西在“react传单”:"1.1.0“版本上都能很好地工作,但是在1.1.1及以上版本上就会中断.

有什么想法吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-12-23 11:22:01

好吧,过了一会儿我找到了解决办法。无法读取未定义错误的属性“getSize”,这是因为只有在加载完整个模块之后,才会将映射传递给refs。可以通过添加以下内容来修正:

代码语言:javascript
复制
return ( this.map ? 
        <Map onClick={(e) => this._mapClickEvent(e)} className="main-screen-map-container" center={position} 
             zoom={this.state.zoom} ref={map => this.map = map}> 

          <TileLayer 
              attribution="&amp;copy <a href=&quot;http://osm.org/copyright&quot;>OpenStreetMap</a> contributors" 
              url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png" 
          /> 
          <Routing map={this._getMap()} from={[11.11, 12.12]} to={[11.11, 12.12]}
                   by={this.props.busstops}/>  
        </Map> 
        : 
        <Map onClick={(e) => this._mapClickEvent(e)} className="main-screen-map-container" center={position} 
             zoom={this.state.zoom} ref={map => this.map = map}> 

          <TileLayer 
              attribution="&amp;copy <a href=&quot;http://osm.org/copyright&quot;>OpenStreetMap</a> contributors" 
              url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png" 
          /> 
        </Map> 

它检查地图是否已经定义,然后在地图上重新划分道路。根据代码的其余部分(如果您使用Redux获取某些数据并设置强制更新的状态),可能还需要添加以下代码:

代码语言:javascript
复制
 componentDidMount() { 
    console.log("map did mount " + this.map); 
    this.forceUpdate() 
  } 

这应该能解决问题。PS三元操作符也可以通过删除冗余代码和仅仅添加

代码语言:javascript
复制
 _addRouting() {
    if (this.map) {
      return (
          <div>
            <Routing map={this._getMap()} from={[11.11, 12.12]} to={[11.11, 12.12]}
                     by={this.props.busstops}/>
          </div>
      )
    }
  }

然后在渲染方法中注入。

仍然不知道为什么这个问题没有发生在较早版本的反应传单。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47212671

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档