Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove marker from google map in react native

Is possible to remove marker from map view? How to remove marker from google map view. I using "react-native-maps" display google map.

Please Help.

like image 212
Nidhi Patel Avatar asked May 23 '17 06:05

Nidhi Patel


1 Answers

To be able to remove or add markers, you just need to do this

//Note : my map markers is => this.props.markers

componentDidMount() {
    this.renderMarkers(this.props.markers)
}


async renderMarkers(mapMarkers) {
    let markers = [];
    markers = mapMarkers.map(marker => (<Marker
            key={marker.code}
            coordinate={{latitude: marker.lat, longitude: marker.lng}}
            onPress={{}}
        />)
    );
    this.setState({markers});
}


refreshMarkers() {
    this.renderMarkers(this.props.markers).then(() => {
        this.forceUpdate();
    });
}


render() {
    return (
        <MapView
            ref={(ref) => this.mapView = ref}
            style={styles.map}
            region={this.props.coordinate}
        >
            {this.state.markers}
        </MapView>
    );
}
like image 179
Mahdi Bashirpour Avatar answered Nov 15 '22 09:11

Mahdi Bashirpour