首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >esri-传单-地理搜索:如何将其与React集成

esri-传单-地理搜索:如何将其与React集成
EN

Stack Overflow用户
提问于 2018-11-06 09:04:01
回答 1查看 3.8K关注 0票数 3

在下面的链接中,有一个在线演示案例展示了如何使用esri-传单-geosearch插件,https://codepen.io/exomark/pen/dafBD

代码语言:javascript
复制
var searchControl = new L.esri.Controls.Geosearch().addTo(map);

var results = new L.LayerGroup().addTo(map);

searchControl.on('results', function(data){
    results.clearLayers();
    for (var i = data.results.length - 1; i >= 0; i--) {
      results.addLayer(L.marker(data.results[i].latlng));
    }
});

这个在线演示很好地支持地理搜索功能。

在我的React应用程序中,我也计划添加诸如传单搜索框之类的内容。但不知道该怎么做。

由于esri-leaflet-geosearch依赖于esri-leaflet,所以安装了两个npm包,但找不到下一步。有什么帮助吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-11-06 14:18:53

你可以通过使用反应传单来实现这一点。

首先安装传单,反应传单和埃斯里-传单-地理编码器库.

之后,在index.js中导入它们

那么在你的节目里:

代码语言:javascript
复制
import React, { Component, createRef } from 'react';
import L from 'leaflet';
import * as ELG from 'esri-leaflet-geocoder';
import { Map, TileLayer } from 'react-leaflet';
...
componentDidMount() {
    const map = this.mapRef.current.leafletElement;
    const searchControl = new ELG.Geosearch().addTo(map);
    const results = new L.LayerGroup().addTo(map);

    searchControl.on('results', function(data){
        results.clearLayers();
        for (let i = data.results.length - 1; i >= 0; i--) {
            results.addLayer(L.marker(data.results[i].latlng));
        }
    });
}

render() {
    const center = [37.7833, -122.4167]
    return (
        <Map 
            style={{height: '800px'}}
            center={center} 
            zoom="10"
            ref={this.mapRef}>
            <TileLayer
                attribution="&amp;copy Google"
                url={'http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png'} />
            <div className='pointer'></div>
        </Map>
    );
}

演示

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

https://stackoverflow.com/questions/53168692

复制
相关文章

相似问题

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