Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Marker click event on react native maps not working in react ios

I have tried by calling onPress method in MapView.Marker Tab, but its not working.

This method for tracking marker click:

markerClick(){
      console.log("Marker was clicked");
 }

In render method, Map components are declared to display map and markers on map. In onPress method I have called my custom method markerClicked(). Still I am not getting the result.

render() {
        return (

          <View style={styles.container}>
            <MapView style={styles.map}
              initialRegion={{
                  latitude: 37.78825,
                  longitude: -122.4324,
                  latitudeDelta: 0.0,
                  longitudeDelta: 0.0,
              }}
            >
            {this.state.markers.map(marker => (
              <MapView.Marker
                coordinate={marker.coordinate}
                title={marker.title}
                description={marker.description}
                onPress={() => this.markerClick()}
              />
            ))}
          </MapView>
        </View>
      );
  }
like image 480
BK19 Avatar asked Sep 23 '16 07:09

BK19


Video Answer


3 Answers

To add to Anil's answer, you can use the 'onCalloutPress' callback on Marker to handle the callout press event, so that you don't need a TouchableHighlight in the callout:

<MapView.Marker
   coordinate={marker.coordinate}
   title={marker.title}
   description={marker.description}
   onCalloutPress={this.markerClick}>
   <MapView.Callout tooltip style={styles.customView}>
       <View style={styles.calloutText}>
          <Text>{marker.title}{"\n"}{marker.description}</Text>
       </View>
   </MapView.Callout>
</MapView.Marker>
like image 196
brickingup Avatar answered Sep 30 '22 22:09

brickingup


You just need to add <MapView.Callout> in <MapView.Marker> tag. Custom callout- you can customize marker info click window as your requirement.

I have used TouchableHighlight for marker info window click, so that you can able to redirect user to other screen on click of custom callout.

<View style={styles.mainContainer}>
                  <MapView style={styles.map}
                          initialRegion={{
                              latitude: 37.78825,
                              longitude: -122.4324,
                              latitudeDelta: 0.0,
                              longitudeDelta: 0.0,
                            }}>
                            {this.state.markers.map((marker) => (
                              <MapView.Marker
                                coordinate={marker.coordinate}
                                title={marker.title}
                                description={marker.description}>
                                  <MapView.Callout tooltip style={styles.customView}>
                                      <TouchableHighlight onPress= {()=>this.markerClick()} underlayColor='#dddddd'>
                                          <View style={styles.calloutText}>
                                              <Text>{marker.title}{"\n"}{marker.description}</Text>
                                          </View>
                                      </TouchableHighlight>
                                    </MapView.Callout>
                                </MapView.Marker>
                            ))}
                     </MapView>
                  </View>
like image 27
Anil Ravsaheb Ghodake Avatar answered Sep 30 '22 21:09

Anil Ravsaheb Ghodake


Try to add a "key" in your marker:

{this.state.markers.map((marker, i) => (
    <MapView.Marker
      key={i}
      coordinate={marker.coordinate}
      title={marker.title}
      description={marker.description}
      onPress={() => this.markerClick()}
    />
 ))}

Just use 'i' for testing, you should use an unique id

like image 29
psyzinho Avatar answered Sep 30 '22 21:09

psyzinho