gpt4 book ai didi

ios - React Native map 标注按下在 IOS 上不触发

转载 作者:行者123 更新时间:2023-11-29 05:34:04 24 4
gpt4 key购买 nike

我正在使用 react native map ,并且尝试在按下标记标注时添加事件监听器。它适用于 Android,但不适用于 IOS。在第一个代码段中,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>

这是完整的类(class):

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
}
});

最佳答案

经过一些额外的研究,我发现了这个开放问题 https://github.com/react-native-community/react-native-maps/issues/2223

该问题指出,在 MapView 上使用provider={PROVIDER_GOOGLE} 和自定义样式会导致 onCalloutPress 在 IOS 上无法触发。使用 native 提供程序时它确实会触发。

显然,标注上有一个 onPress 事件,使用该事件也可以在 IOS 上运行。这是与 google 提供商一起在 android 和 ios 上运行的最终解决方案,它仅在每个平台上触发一次:

import React, { Component } from "react";
import { View, StyleSheet, Text } 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}
onCalloutPress={() => this.calloutPress()}
>
<Callout onPress={() => this.calloutPress()}>
<View>
<Text style={styles.calloutTitle}>My title</Text>
<Text style={styles.calloutDescription}>My description</Text>
</View>
</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
}
});

如果您不想添加未触发的事件监听器,请使 onPress 和 onCalloutPress 平台相关。

关于ios - React Native map 标注按下在 IOS 上不触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57233394/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com