我可以自动对焦一个mapbox-GL反应地理编码

问题描述 投票:1回答:1

我们正在使用的React's wrapper to Mapbox geocoder component。我们的代码基本上是一个自述,但我们只显示要求的地理编码输入字段(点击一些“编辑地址”按钮时)。

是否有可能自动对焦“搜索...” <input/>场分量(由<Geocoder />创建),以便用户可以开始输入后立即地理编码显示出来?

reactjs mapbox-gl-js mapbox-gl
1个回答
3
投票

您应该使用地理编码器onInit道具,这将传递一个gecoder实例,并在地理编码器已初始化将被调用。

class App extends Component {
  state = {
    viewport: {
      width: 400,
      height: 400,
      latitude: 37.7577,
      longitude: -122.4376,
      zoom: 8,
    },
    searchResultLayer: null,
  };

  // rest of the code

  handleGeocoderInit = (geocoderInstance) => {
    const inputEl = geocoderInstance._inputEl;
    inputEl.focus();
  };

  render() {
    const { viewport, searchResultLayer } = this.state;

    return (
      <MapGL
        ref={this.mapRef}
        {...viewport}
        onViewportChange={this.handleViewportChange}
        mapboxApiAccessToken={MAPBOX_TOKEN}>
        <Geocoder
          mapRef={this.mapRef}
          onResult={this.handleOnResult}
          onViewportChange={this.handleGeocoderViewportChange}
          mapboxApiAccessToken={MAPBOX_TOKEN}
          position="top-left"
          onInit={this.handleGeocoderInit}
        />
        <DeckGL {...viewport} layers={[searchResultLayer]} />
      </MapGL>
    );
  }
}

render(<App />, document.getElementById('root'));
© www.soinside.com 2019 - 2024. All rights reserved.