React 本机地图上的标记单击事件在 React 中不起作用 ios
Marker click event on react native maps not working in react ios
我试过在 MapView.Marker 选项卡中调用 onPress 方法,但它不起作用。
跟踪标记点击的方法:
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>
);
}
尝试在标记中添加 "key":
{this.state.markers.map((marker, i) => (
<MapView.Marker
key={i}
coordinate={marker.coordinate}
title={marker.title}
description={marker.description}
onPress={() => this.markerClick()}
/>
))}
只需使用'i'进行测试,你应该使用唯一的id
您只需在 <MapView.Marker>
标签中添加 <MapView.Callout>
。
自定义标注 - 您可以根据需要自定义标记信息点击 window。
我已将 TouchableHighlight
用于 marker info window click
,因此您可以在单击 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>
要添加 Anil 的答案,您可以使用标记上的 'onCalloutPress' 回调来处理标注按下事件,这样您就不需要标注中的 TouchableHighlight:
<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>
<MapView.Marker key={index} coordinate={marker.coordinate}
title={marker.title}
description={marker.description}
onPress={e => this.onPressMarker(e,index)}
>
onCalloutPress={this.markerClick()} 对我有用。希望这有帮助。
我试过在 MapView.Marker 选项卡中调用 onPress 方法,但它不起作用。
跟踪标记点击的方法:
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>
);
}
尝试在标记中添加 "key":
{this.state.markers.map((marker, i) => (
<MapView.Marker
key={i}
coordinate={marker.coordinate}
title={marker.title}
description={marker.description}
onPress={() => this.markerClick()}
/>
))}
只需使用'i'进行测试,你应该使用唯一的id
您只需在 <MapView.Marker>
标签中添加 <MapView.Callout>
。
自定义标注 - 您可以根据需要自定义标记信息点击 window。
我已将 TouchableHighlight
用于 marker info window click
,因此您可以在单击 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>
要添加 Anil 的答案,您可以使用标记上的 'onCalloutPress' 回调来处理标注按下事件,这样您就不需要标注中的 TouchableHighlight:
<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>
<MapView.Marker key={index} coordinate={marker.coordinate}
title={marker.title}
description={marker.description}
onPress={e => this.onPressMarker(e,index)}
>
onCalloutPress={this.markerClick()} 对我有用。希望这有帮助。