4

I am trying to make a form that a user can search their location or pin his location. I am using react-leaflet for loading a map and react-leaflet-search for adding the search functionality. The search functionality is working fine. Below you can see the code.

<Map center={position} zoom={zoom}  onDragEnd = {function(e){ console.log(e);}} >
  <TileLayer
    attribution='&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
    url='https://{s}.tile.osm.org/{z}/{x}/{y}.png'/>
  <Search 
    position="topright" 
    showPopup={false} 
    provider="OpenStreetMap" 
    showMarker={true} 
    openSearchOnLoad={true} 
    closeResultsOnClick={true} 
    providerOptions={{ region: "np" }}/>
</Map>

What I want to do is to access user's entered location or latitude longitude of the marker that is displayed after user selects the location. I tried to search for event listeners, but could not find any. Currently I am trying to use onDragEnd event but I have not succeeded yet. Could anyone tell me how to achieve what I am trying to do?

Christos Lytras
  • 36,310
  • 4
  • 80
  • 113
Ritesh Jung Thapa
  • 1,177
  • 2
  • 13
  • 20

1 Answers1

1

Unfortunately react-leaflet-search has not a proper way to retrieve search results. There is mapStateModifier callback that we can use to get the search result coordinates LatLng object, but we'll have to setup the map flyTo call as well:

render() {
  const position = [51.505, -0.09];
  const zoom = 13;

  return (
    <div>
      <Map 
        ref={ref => this.mapRef = ref}
        center={position} 
        zoom={zoom}
      >
        <TileLayer
          attribution='&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
          url='https://{s}.tile.osm.org/{z}/{x}/{y}.png' />

        <ReactLeafletSearch 
          ref={ref => this.mapSearchRef = ref}
          mapStateModifier={(latLng) => {

            // Do work with result latitude, longitude
            console.log('Search Latitude:', latLng.lat);
            console.log('Search Longitude:', latLng.lng);

            if (this.mapRef) {
              // Because we have a custom mapStateModifier callback,
              // search component won't flyTo coordinates
              // so we need to do it using our refs
              this.mapRef.contextValue.map.flyTo(
                latLng,
                this.mapSearchRef.props.zoom,
                this.mapSearchRef.props.zoomPanOptions
              );
            }
          }}
          position="topright" 
          showPopup={false} 
          provider="OpenStreetMap" 
          showMarker={true} 
          openSearchOnLoad={true} 
          closeResultsOnClick={true} 
          providerOptions={{ region: "np" }}
        />
      </Map>
    </div>
  );
}

You can check this example Stackblitz to see it working.

Christos Lytras
  • 36,310
  • 4
  • 80
  • 113
  • thanks for the answer. I did a wrap around to solve the problem. But I will check if this is a proper way. – Ritesh Jung Thapa Mar 16 '20 at 06:58
  • Hi there I am trying to understand the solution and apply it. Could you explain some more? And also do you know any way of making the search marker draggable. – Ritesh Jung Thapa Mar 17 '20 at 10:53
  • Why you can't apply it? Did you open the Stackblitz example [here](https://stackblitz.com/edit/react-leaflet-search-callback)? It has this whole code working. – Christos Lytras Mar 17 '20 at 10:55
  • Yeah I can apply it. But I am asking if there is a way to make the marker that appears after the search draggable? – Ritesh Jung Thapa Mar 17 '20 at 11:03
  • You can apply a drag functionality to `leaflet-pane leaflet-marker-pane` but that only make the marker to move, you won't get any latitude and longitude. That's hard to implement. You can search in https://github.com/Leaflet/Leaflet/issues for this but I don't think it's implemented. – Christos Lytras Mar 17 '20 at 12:23