javascript - esri-leaflet-geosearch: how to integrate it with React - Stack Overflow

In the following link, there is online demo case showing how to user esri-leaflet-geosearch plugin, var

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?

Share Improve this question edited Mar 7, 2020 at 8:51 kboul 14.6k5 gold badges47 silver badges58 bronze badges asked Nov 6, 2018 at 9:04 Chris BaoChris Bao 2,8888 gold badges43 silver badges69 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

You 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="&amp;copy Google"
                url={'http://{s}.tile.openstreetmap/{z}/{x}/{y}.png'} />
            <div className='pointer'></div>
        </Map>
    );
}

Demo

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745258200a4619084.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信