我有一个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"
}
});
答案 0 :(得分:0)
已解决:Callout
必须是MapView
的兄弟姐妹,而不是孩子。
答案 1 :(得分:0)
您确定要将其作为 MapView 的同级,而不是 Marker? Loop url from dataframe and download pdf files in Python 明确提到示例代码库
import { Callout } from 'react-native-maps';
<Marker coordinate={marker.latlng}>
<MyCustomMarkerView {...marker} />
<Callout>
<MyCustomCalloutView {...marker} />
</Callout>
</Marker>
此外,我已经尝试了您的“解决方案”,但它只是在地图中间呈现一个按钮。相反,我想我们大多数人都希望在用户单击标记后呈现它。两种我都试过了,第二种肯定是大多数用户所期望的,那么代码片段很简单:
<Marker
coordinate={{ latitude: 51.23123, longitude: 4.921321 }}
description="Description"
title="Title"
>
<Callout style={styles.locationButtonCallout}>
<Button
onPress={() => alert("button hello")}
title={"Callout Button"}
/>
</Callout>
</Marker>
...
const styles = StyleSheet.create({
locationButtonCallout: {
borderRadius: 0,
opacity: 0.8,
backgroundColor: "lightgrey",
},
});