MapView Callout onPress не запускается - PullRequest
0 голосов
/ 10 мая 2019

Я получил MapView от, и я пытаюсь отобразить кнопку местоположения, которая в данный момент отображается в выноске с надписью «Найди меня». Код ниже.

Кнопка отображается красиво, но я не могу заставить работать onPress: когда я нажимаю «Найти меня», ни лог-операторы «callout hello», ни «button hello» не регистрируются, поэтому ничего не нажималось ( кнопка также не отображает непрозрачность при нажатии).

Я также пытался отрисовать кнопку без выноски. Отображается, но все еще не нажата.

Помощь!


import React, { Component } from "react";
import { MapView } from "expo";
import { View, Button } from "react-native";
import { BLText } from "../components/StyledComponents";
import F8StyleSheet from "../components/F8StyleSheet";
import { connect } from "react-redux";
const { Marker, Callout } = MapView;
import { getLocationAsync } from "../redux/actions/userActions";

FindMeButton = ({ onPress }) => {
  return (
    <Callout
      style={styles.locationButtonCallout}
      onPress={() => console.log("callout hello")}
    >
      <Button
        onPress={() => console.log("button hello")}
        title={"Find Me"}
        style={styles.locationButton}
      />
    </Callout>
  );
};

class MapScreen extends Component {
  static navigationOptions = {
    title: "Nearby Stations"
  };

  renderMarkers() {
    return (markers = Object.keys(this.props.stations).map(key => {
      const station = this.props.stations[key];
      return (marker = (
        <Marker
          key={key}
          // onPress={this.onMarkerPress.bind(this, station)}
          coordinate={{
            latitude: Number(station.location.lat),
            longitude: Number(station.location.lng)
          }}
        >
          <Callout
            onPress={() => {
              this.props.navigation.navigate("ListScreen");
              // this.props.navigation.navigate("StationDetail", {
              //   title: station.title
              // });
            }}
          >
            <BLText>{station.title}</BLText>
          </Callout>
        </Marker>
      ));
    }));
  }

  render() {
    console.log("rendering at region:", this.props.userLocation);

    return (
      <View style={styles.container}>
        <MapView
          provider={MapView.PROVIDER_GOOGLE}
          style={{ flex: 1 }}
          region={this.props.userLocation}
          showsUserLocation={true}
        >
          {this.renderMarkers()}
          <FindMeButton onPress={this.props.getLocationAsync} />
        </MapView>
      </View>
    );
  }
}

const mapStateToProps = state => {
  return {
    stations: state.main.stations,
    userLocation: state.main.currentRegion
  };
};

export default connect(
  mapStateToProps,
  { getLocationAsync }
)(MapScreen);

const styles = F8StyleSheet.create({
  locationButtonCallout: {
    borderRadius: 0,
    opacity: 0.8,
    backgroundColor: "lightgrey",
  },
  locationButton: {
    backgroundColor: "lightgrey",
    borderRadius: 10,
    opacity: 0.8
  },
  container: {
    flex: 1,
    flexDirection: "row",
    justifyContent: "center"
  }
});

1 Ответ

0 голосов
/ 13 мая 2019

Решено: Callout должен быть родным братом MapView, а не ребенком.

...