React Native Maps Callout按不在IOS上触发

时间:2019-07-27 15:08:53

标签: ios react-native react-native-maps

我正在使用react本机地图,并且尝试在按下标记标注时添加事件监听器。它适用于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>

我还尝试了标注内部的可触摸不透明度,现在在Android或IOS上均未调用 calloutPress

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

2 个答案:

答案 0 :(得分:0)

您可以使用MapView.Callout

您可以尝试吗?

export default class MapTabs extends Component {
    constructor(props) {
        super(props);
    }

    calloutPress() {
        console.log("hello!");
    }

  render() {
    return (
      <View style={styles.mapContainer}>
        <MapView
          provider={PROVIDER_GOOGLE}
          style={styles.map}
          rotateEnabled={false}
          mapType="standard"
          initialRegion={region}
        >
          <MapView.Marker
            coordinate={markerCoordinate}
          >
            <MapView.Callout>
              <TouchableOpacity
                onPress={() => this.calloutPress()}
              >
                 <Text style={styles.calloutTitle}>My title</Text>
                 <Text style={styles.calloutDescription}>My description</Text>
              </TouchableOpacity>
            </MapView.Callout>
          </MapView.Marker>
        </MapView>
      </View>
    );
  }
}

答案 1 :(得分:0)

经过一些进一步的研究,我发现了这个未解决的问题https://github.com/react-native-community/react-native-maps/issues/2223

该问题表明,使用provider = {PROVIDER_GOOGLE}和MapView上的自定义样式使onCalloutPress不会在IOS上触发。使用本机提供程序时会触发。

显然,标注上有一个onPress事件,使用该事件也可以在IOS上使用它。这是使用google provider在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平台依赖。