javascript - React Leaflet: How to have Multiselect on Map - Stack Overflow

I'm using React and React-Leaflet to generate a map of circleMarkers given data points and their L

I'm using React and React-Leaflet to generate a map of circleMarkers given data points and their Lat/Long coordinates. I have no issues rendering a map of data, and even being able to change the data rendered if a user filters by filter type on a sidebar.

<Map center={[this.props.data[0].Latitude, this.props.data[0].Longitude]} zoom={12}>
    <TileLayer
      attribution="&amp;copy <a href=&quot;;quot;>OpenStreetMap</a> contributors"
      url="https://{s}.tile.openstreetmap/{z}/{x}/{y}.png"
    />

    /* code for rendering circleMarkers goes here */

</Map>

When a user clicks on a marker, I have it so a popup appears on the map for the data with a little blurb about the corresponding point.

<CircleMarker center={[entry.Latitude, entry.Longitude]} color={this.determineMarkerColor(entry)} radius={thisputeMarkerSize(entry)}>
          <Popup>
              <span>Radius is for: {this.props.filterType} </span>
          </Popup>
</CircleMarker>

Is there a way to configure the Map so that it can figure out when a user is trying to, for example, shift click to select multiple points and then pass the selected points as an array? Or, even better, how can I make the Map interactive so a user can drag and drop a custom area (like drawing a square or circle) and select all rendered points WITHIN that area?

I'm planning on passing this array of selected data to another ponent that will graph it.

Any advice on where to look for this would be appreciated.

I'm using React and React-Leaflet to generate a map of circleMarkers given data points and their Lat/Long coordinates. I have no issues rendering a map of data, and even being able to change the data rendered if a user filters by filter type on a sidebar.

<Map center={[this.props.data[0].Latitude, this.props.data[0].Longitude]} zoom={12}>
    <TileLayer
      attribution="&amp;copy <a href=&quot;http://osm/copyright&quot;>OpenStreetMap</a> contributors"
      url="https://{s}.tile.openstreetmap/{z}/{x}/{y}.png"
    />

    /* code for rendering circleMarkers goes here */

</Map>

When a user clicks on a marker, I have it so a popup appears on the map for the data with a little blurb about the corresponding point.

<CircleMarker center={[entry.Latitude, entry.Longitude]} color={this.determineMarkerColor(entry)} radius={this.puteMarkerSize(entry)}>
          <Popup>
              <span>Radius is for: {this.props.filterType} </span>
          </Popup>
</CircleMarker>

Is there a way to configure the Map so that it can figure out when a user is trying to, for example, shift click to select multiple points and then pass the selected points as an array? Or, even better, how can I make the Map interactive so a user can drag and drop a custom area (like drawing a square or circle) and select all rendered points WITHIN that area?

I'm planning on passing this array of selected data to another ponent that will graph it.

Any advice on where to look for this would be appreciated.

Share Improve this question asked Jan 15, 2018 at 14:49 shishyshishy 8171 gold badge17 silver badges31 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

It's already almost 3 years passed away but here is my solution with pressing shift and selecting an area of markers, maybe it will be useful for somebody

Map has onmousedown & onmouseup methods

<Map
  ref={saveMap}
  onmousedown={onMouseDown} // set start position in state
  onmouseup={onMouseUp} // end position and logic to select markers
  className="leaflet-container"
  center={position}
  zoom={viewport.zoom}
>

With onmousedown it is possible to find the start position when the user pressed shift and started to move the mouse to select an area

onmousedown returns an event with latlng property - start position:

const onMouseDown = useCallback((e: LeafletMouseEvent) => {
  if (e.originalEvent.shiftKey) { // check if the shift key been pressed
    setStartPoint(e.latlng);
  }
}, []);

onmouseup also returns an event with latlng property - end position:

const onMouseUp = useCallback((e: LeafletMouseEvent) => {
  if (e.originalEvent.shiftKey && startPoint) {
    const bounds = new L.LatLngBounds(startPoint, e.latlng); // creates a class which will help to identify if an element is within the selection boundaries
    const selectedIds: string[] = [];

    for (let i = 0; i < orders?.length; i++) {
      const { geometry: { coordinates } } = orders[i];
      const point = new L.LatLng(coordinates[0], coordinates[1]);
      bounds.contains(point) && selectedIds.push(orders[i].properties.entry);
    }
    onSelectOrder(selectedIds);
    setStartPoint(null);
  }
}, [onSelectOrder, orders, startPoint]);

It works pretty well for my purposes to select multiple orders on the map

You could play around with leaflet-draw in order to get the selection tool that you want. With leaflet-draw you can draw polygons, circles, rectangles, etc and get the bounds of the figure you've drawn. There's also a library called point-in-polygon that can return the coordinates inside a boundaries parameter.

TLDR:

  1. Draw polygon with Leaflet Draw and get the bounds of your drawn figure.
  2. Filter your markers that have their {latitude, longitude} inside the bounds with Point-in-polygon.

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

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信