Ask Your Question
0

What is the procedure for removing lines from react-native-maps?

asked 2021-11-07 11:00:00 +0000

djk gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

answered 2021-08-03 16:00:00 +0000

devzero gravatar image

To remove lines from react-native-maps, you need to follow these steps:

  1. First, you will need to create a reference to the line object that you want to remove. You can do this using the ref prop when you create the line object.

  2. Once you have a reference to the line object, you can call the setMap(null) method on the object to remove it from the map.

  3. Alternatively, you can also remove all lines from the map by calling the setMap(null) method on the map's LineCollection object.

  4. If you want to remove only a specific line, you can store the line objects in an array, and then use the splice method to remove the desired line from the array.

  5. Finally, if you want to remove all lines from the map and start over, you can call the clear method on the map's LineCollection object.

Here is an example of how you can remove a line object from react-native-maps using a reference:

import React, { Component } from 'react';
import { View } from 'react-native';
import MapView, { Polyline } from 'react-native-maps';

export default class MapScreen extends Component {

  constructor(props) {
    super(props);

    this.state = {
      lines: [
        {
          id: 1,
          coordinates: [
            { latitude: 37.78825, longitude: -122.4324 },
            { latitude: 37.75825, longitude: -122.4624 },
          ],
        },
        {
          id: 2,
          coordinates: [
            { latitude: 37.78825, longitude: -122.4324 },
            { latitude: 37.78825, longitude: -122.4024 },
          ],
        },
      ],
    };

    this.polyLines = [];
  }

  onMapReady = () => {
    this.map.fitToElements(true);
  };

  removeLine = (lineId) => {
    const lineIndex = this.state.lines.findIndex((line) => line.id === lineId);
    if (lineIndex !== -1) {
      const lineObject = this.polyLines[lineIndex];
      lineObject.setMap(null);               // remove line from map
      this.polyLines.splice(lineIndex, 1);   // remove line from array
      const newLines = [...this.state.lines];
      newLines.splice(lineIndex, 1);
      this.setState({ lines: newLines });
    }
  };

  render() {
    return (
      <View style={{ flex: 1 }}>
        <MapView
          ref={(map) => (this.map = map)}
          style={{ flex: 1 }}
          initialRegion={{
            latitude: 37.78825,
            longitude: -122.4324,
            latitudeDelta: 0.0922,
            longitudeDelta: 0.0421,
          }}
          onMapReady={this.onMapReady}
        >
          {this.state.lines.map((line, index) => (
            <Polyline
              ref={(polyline) => (this.polyLines[index] = polyline)}
              key={`line-${index}`}
              coordinates={line.coordinates}
              strokeColor={'red'}
              strokeWidth={2}
            />
          ))}
        </MapView>
      </View>
    );
  }
}
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2021-11-07 11:00:00 +0000

Seen: 14 times

Last updated: Aug 03 '21