在下面的链接中,有一个在线演示案例展示了如何使用esri-传单-geosearch插件,https://codepen.io/exomark/pen/dafBD。
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包,但找不到下一步。有什么帮助吗?
发布于 2018-11-06 14:18:53
你可以通过使用反应传单来实现这一点。
首先安装传单,反应传单和埃斯里-传单-地理编码器库.
之后,在index.js中导入它们
那么在你的节目里:
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="&copy Google"
url={'http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png'} />
<div className='pointer'></div>
</Map>
);
}https://stackoverflow.com/questions/53168692
复制相似问题