admin管理员组文章数量:1418049
In the following link, there is online demo case showing how to user esri-leaflet-geosearch plugin,
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));
}
});
This online demo works well to support the geosearch function.
And in my React app, I plan to add such as search box for leaflet as well. But can't figure out how to do this.
As esri-leaflet-geosearch
depends on esri-leaflet
, so installed both npm packages, but can't find next step. so any help?
In the following link, there is online demo case showing how to user esri-leaflet-geosearch plugin, 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));
}
});
This online demo works well to support the geosearch function.
And in my React app, I plan to add such as search box for leaflet as well. But can't figure out how to do this.
As esri-leaflet-geosearch
depends on esri-leaflet
, so installed both npm packages, but can't find next step. so any help?
1 Answer
Reset to default 6You can achieve that using react-leaflet.
First install leaflet, react-leaflet & esri-leaflet-geocoder libraries.
After that import them in index.js
Then in your p:
import React, { Component, createRef } from 'react';
import L from 'leaflet';
import * as ELG from 'esri-leaflet-geocoder';
import { Map, TileLayer } from 'react-leaflet';
...
ponentDidMount() {
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/{z}/{x}/{y}.png'} />
<div className='pointer'></div>
</Map>
);
}
Demo
本文标签: javascriptesrileafletgeosearch how to integrate it with ReactStack Overflow
版权声明:本文标题:javascript - esri-leaflet-geosearch: how to integrate it with React - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745258200a2650229.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论