【问题标题】:Map markers don't load on componentDidMount地图标记不会加载到 componentDidMount
【发布时间】:2019-10-01 04:53:20
【问题描述】:

我有一张从视口坐标获取用户位置的地图,然后它使用这些坐标从 API 获取标记。问题是我的标记只有在我移动地图后才会出现。我对自己缺少什么感到困惑:

componentDidMount(){
        this.getInitialPosition();
        this.fetchLocations();
        this.getMarkers();
    }

然后我的函数判断用户的位置:

getInitialPosition = () => {
        navigator.geolocation.getCurrentPosition(position => {
            let newViewport = {
                height: "100vh",
                width: "100vw",
                latitude: position.coords.latitude,
                longitude: position.coords.longitude,
                zoom: 15
            }
            this.setState({
                viewport: newViewport
            });
        });
    }

然后我从我的 API 中获取标记:

fetchLocations = () => {
        fetch(`http://localhost:3000/locations/${this.state.viewport.latitude}/${this.state.viewport.longitude}`)
        .then(resp => resp.json())
        .then(locations => this.setState({locations}))
    }

最后是标记:

getMarkers = () => {
        return this.state.locations.map(location => {
            return (
                <Marker key={location.id} offsetLeft={-25} offsetTop={-70} latitude={parseFloat(location.lat)} longitude={parseFloat(location.lng)}>
                    <div className="marker-styles" onClick={() => {this.handleLocationClick(location.id)}} >
                        <p>{location.reviews}</p>
                    </div>
                </Marker>
            );
        });
    }

然后当我调用地图时:

                <MapGL
                    {...this.state.viewport}

                    onClick={this.mapClick}
                    attributionControl={false}
                    onViewportChange={this.onViewportChange}
                    mapboxApiAccessToken={TOKEN}
                    mapStyle="mapbox://styles/mapbox/streets-v10">
                    {this.getMarkers()}                        
                </MapGL>

我得到了地图,但没有得到标记。有人可以指出我正确的方向吗?我认为问题在于我对 Promise 和组件生命周期的把握不稳,但老实说,我一直无法弄清楚这一点。我在这里粘贴了完整的(凌乱的)代码:https://pastebin.com/j8dzYAsk

【问题讨论】:

    标签: javascript reactjs mapbox-gl-js react-map-gl


    【解决方案1】:

    问题在于getInitialPositionfetchLocations异步的 - 当您调用getMarkers 时,它们获得的数据还没有被提取

    首先,我让 getInitialPosition 返回一个 Promise,这样一旦它也返回了由 fetch 返回的 Promise,就很容易使用 fetchLocations

    然后,只需使用 Promise.all 等待这两个 Promise 解决,一旦完成,您调用 this.getMarkers

    componentDidMount(){
        Promise.all([
            this.getInitialPosition(),
            this.fetchLocations()
        ]).then(this.getMarkers);
    }
    getInitialPosition = () => {
        return new Promise((resolve, reject) => {
            navigator.geolocation.getCurrentPosition(position => {
                let newViewport = {
                    height: "100vh",
                    width: "100vw",
                    latitude: position.coords.latitude,
                    longitude: position.coords.longitude,
                    zoom: 15
                }
                this.setState({
                    viewport: newViewport
                });
                resolve();
            });
        });
    }
    fetchLocations = () => {
        return fetch(`http://localhost:3000/locations/${this.state.viewport.latitude}/${this.state.viewport.longitude}`)
        .then(resp => resp.json())
        .then(locations => this.setState({locations}));
    }
    

    【讨论】:

    • 非常感谢您的回答。由于某种原因,它仍然没有工作。我只得到标记之后我拖动地图。有什么线索吗?
    猜你喜欢
    • 2012-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-28
    相关资源
    最近更新 更多