首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用loadModules ReactJS访问esri事件中的访问状态

使用loadModules ReactJS访问esri事件中的访问状态
EN

Stack Overflow用户
提问于 2020-12-05 19:21:10
回答 1查看 223关注 0票数 1

为了加载esri模块,我使用了从esri加载程序中延迟加载"loadModules“的方法。这里的问题是,当“搜索完成”事件触发时,我无法访问状态来存储经度和纬度值。在创建搜索小部件时,我也无法覆盖"allPlaceholder“值。

https://codesandbox.io/s/pedantic-hellman-sbcdz

知道我可能做错什么了吗?可以在componentDidMount之外访问搜索小部件吗?

谢谢!

代码语言:javascript
复制
import React , { Component, Fragment } from 'react';
import { loadModules } from 'esri-loader';
import Col from 'react-bootstrap/Col';
import Row from 'react-bootstrap/Row';

class MapSearch extends Component {
    constructor(props) {
        super(props);
        this.mapRef = React.createRef();
        this.searchRef = React.createRef();
        this.state = {
            mysearch: null,
            longitude: 0,
            latitude: 0,
            searchTerm: ""
        };
    }
    componentDidMount() {
        loadModules(['esri/Map', 'esri/views/MapView', 'esri/widgets/Search'], { css: true })
            .then(([ArcGISMap, MapView, Search]) => {
                const map = new ArcGISMap({
                    basemap: 'osm'
                });

                this.view = new MapView({
                    container: this.mapRef.current,
                    map: map,
                    center: [-85, 35],
                    zoom: 14
                });
                var mysearch = new Search({
                    view: this.view,
                    allPlaceholder: "TESTESTTEST", // this doesn't work
                    container: this.searchRef.current
                });
                mysearch.on("search-complete", function(event){
                    console.log(event);
                   console.log(this.state);
                 }) 
            }
        );
    }
    render() {
        return (
            <Fragment>
                <Row >
                    <Col lg={7}><div className="arcmap" style={{"height": "50vh"}}  ref={this.mapRef}></div></Col>
                    <Col lg={5}><div className="zobya-search" style={{ "wdith": "100%" }} ref={this.searchRef} ></div></Col>
                </Row>
            </Fragment>
        );
    }
}
export default MapSearch;

这个结果非常简单,希望它能帮助到其他人

代码语言:javascript
复制
just add a binding in the constructor such as

this.handleSearchComplete = this.handleSearchComplete.bind(this); 

and create a new function  

handleSearchComplete(event) {
        this.setState({
            longitude: event.results[0].results[0].feature.geometry.longitude , 
            latitude: event.results[0].results[0].feature.geometry.latitude
       });
    } 
then call this callback function such as
mysearch.on("search-complete", this.handleSearchComplete)
EN

回答 1

Stack Overflow用户

发布于 2020-12-07 01:34:00

事实证明,这很简单,希望它能帮助到其他人。

只需在构造函数中添加绑定,如

代码语言:javascript
复制
this.handleSearchComplete = this.handleSearchComplete.bind(this); 

并创建一个新的函数

代码语言:javascript
复制
handleSearchComplete(event) {
        this.setState({
            longitude: event.results[0].results[0].feature.geometry.longitude , 
            latitude: event.results[0].results[0].feature.geometry.latitude
       });
    } 

然后调用这个回调函数,如

代码语言:javascript
复制
mysearch.on("search-complete", this.handleSearchComplete)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65161003

复制
相关文章

相似问题

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