【发布时间】:2019-12-05 14:05:17
【问题描述】:
我正在使用反应原生地图,并且我正在尝试在按下标记标注时添加事件侦听器。它适用于Android,但不适用于IOS。在这第一个 sn-p calloutPress 在 Android 上被调用,但在 IOS 上没有:
<MapView
provider={PROVIDER_GOOGLE}
style={styles.map}
rotateEnabled={false}
mapType="standard"
initialRegion={region}
>
<Marker coordinate={markerCoordinate} onCalloutPress={() => this.calloutPress()}>
<Callout>
<View>
<Text style={styles.calloutTitle}>My title</Text>
<Text style={styles.calloutDescription}>My description</Text>
</View>
</Callout>
</Marker>
</MapView>
我还在标注中尝试了可触摸的不透明度,现在 calloutPress 在 Android 或 IOS 上都没有被调用:
<MapView
provider={PROVIDER_GOOGLE}
style={styles.map}
rotateEnabled={false}
mapType="standard"
initialRegion={region}
>
<Marker coordinate={markerCoordinate}>
<Callout>
<TouchableOpacity onPress={() => this.calloutPress()}>
<Text style={styles.calloutTitle}>My title</Text>
<Text style={styles.calloutDescription}>My description</Text>
</TouchableOpacity>
</Callout>
</Marker>
</MapView>
这是完整的课程:
import React, { Component } from "react";
import { View, StyleSheet, Text, TouchableOpacity } from "react-native";
import MapView, { Marker, Callout, PROVIDER_GOOGLE } from "react-native-maps";
export default class MapTabs extends Component {
constructor(props) {
super(props);
}
render() {
return (
<View style={styles.mapContainer}>
<MapView
provider={PROVIDER_GOOGLE}
style={styles.map}
rotateEnabled={false}
mapType="standard"
initialRegion={region}
>
<Marker coordinate={markerCoordinate}>
<Callout>
<TouchableOpacity onPress={() => this.calloutPress()}>
<Text style={styles.calloutTitle}>My title</Text>
<Text style={styles.calloutDescription}>My description</Text>
</TouchableOpacity>
</Callout>
</Marker>
</MapView>
</View>
);
}
calloutPress() {
console.log("hello!");
}
}
const region = {
latitude: 54.403664,
longitude: 14.769657,
latitudeDelta: 30,
longitudeDelta: 30
};
const markerCoordinate = { latitude: 54.403664, longitude: 14.769657 };
const styles = StyleSheet.create({
mapContainer: {
width: "100%",
height: "100%",
zIndex: 0
},
map: {
flex: 1
},
calloutTitle: {
fontSize: 17,
marginBottom: 5,
fontWeight: "bold"
},
calloutDescription: {
fontSize: 14
}
});
【问题讨论】:
标签: ios react-native react-native-maps