【发布时间】:2021-03-11 19:00:36
【问题描述】:
我在 react-native 中制作了一些方块,我希望它们在你按下它们时改变颜色。这是我的尝试。这是我的 app.js。附言。正方形的线是从另一个文件导入的 我尝试了很多东西,但没有奏效。所以,如果你能修改我的来源,我会很高兴 从'react'导入反应,{组件}; 从'react-native'导入按钮
import {
SafeAreaView,
StyleSheet,
ScrollView,
View,
Text,
StatusBar,
} from 'react-native';
import {
Header,
LearnMoreLinks,
Colors,
DebugInstructions,
ReloadInstructions,
} from 'react-native/Libraries/NewAppScreen';
import MapView, { PROVIDER_GOOGLE, Marker, Heatmap, Circle, Polyline, Polygon } from 'react-native-maps'
import {locations} from './Data/Data'
export default class App extends Component {
constructor(props){
super(props);
this.state = {
latitude: 0,
longitude: 0,
error: null
}
}
componentDidMount (){
navigator.geolocation.getCurrentPosition(position =>{
this.setState({
latitude:position.coords.latitude,
longitude:position.coords.longitude,
error:null
});
},error=> this.setState({error:error.message}),
{enableHighAccuracy:true, timeout : 2000, maximumAge : 2000});
}
render() {
var squarez = [];
for( let i = 0; i <2916; i +=4) {
squarez.push(
{
coordinates: [
{ latitude: locations[i].latitude, longitude: locations[i].longitude },
{ latitude: locations[i+1].latitude, longitude: locations[i+1].longitude },
{ latitude: locations[i+3].latitude, longitude: locations[i+3].longitude },
{
latitude: locations[i+2].latitude, longitude: locations[i+2].longitude }
],
open: false,
}
)
}
toggle(polygon){
polygon.open = !polygon.open;
if (polygon.open) {
fillColor= "#8f353b"
}
}
return (
<View style={styles.container}>
<MapView
provider={PROVIDER_GOOGLE}
style={styles.map}
initialRegion={{
latitude: 44.439663,
longitude: 26.096306,
latitudeDelta: 0.0922,
longitudeDelta: 0.0421,
}}
>
{
squarez.map((polygon, index) => (
<View key={index}>
<Polygon
coordinates={polygon.coordinates}
onPress={() => this.toggle(polygon)}
/>
</View>))
}
<Marker coordinate={ this.state}/>
</MapView>
</View>
)
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: 'red'
},
map: {
flex: 1
}
})`
【问题讨论】:
标签: react-native google-maps google-maps-react