Javascript 如何添加经度和纬度的值
我试图在react native中显示项目的详细信息 首先,我使用api来使用json的不同数据:Javascript 如何添加经度和纬度的值,javascript,reactjs,react-navigation,Javascript,Reactjs,React Navigation,我试图在react native中显示项目的详细信息 首先,我使用api来使用json的不同数据: class ListScreen extends React.Component { constructor() { super(); this.state = { dataSource: [], }; this.getRemoteData(); } static navigationOptions = { title: 'Home', }
class ListScreen extends React.Component {
constructor() {
super();
this.state = {
dataSource: [],
};
this.getRemoteData();
}
static navigationOptions = {
title: 'Home',
};
getRemoteData = () => {
const url = "https://***";
fetch(url)
.then(res => res.json())
.then(res => {
this.setState({
data: res.records
});
})
.catch(error => {
console.log("get data error from:" + url + " error:" + error);
});
};
然后我使用导航功能来显示每个项目的特定细节
renderNativeItem = (item) => {
return <ListItem
roundAvatar
subtitle={item.fields.station_name}
onPress={() => this.onPressItem(item)}
/>;
}
onPressItem = (item) => {
this.props.navigation.navigate('Detail', {item: item})
}
renderActivieItem=(项)=>{
返回此项。onPressItem(item)}
/>;
}
onPressItem=(项目)=>{
this.props.navigation.navigate('Detail',{item:item})
}
最后,我尝试显示不同的数据
但对于经度和纬度,我遇到了一个问题,不知道如何解决
(“未定义”不是对象)
render(){
返回(
this.renderActivItem(项)}
/>
);
}
}
类DetailScreen扩展了React.Component{
render(){
const item=this.props.navigation.state.params.item;
返回(
名称:{item.fields.nbebike}//work
);
}
感谢您的帮助:)您正在
数据上设置状态,但在您定义的构造函数数据集上设置状态
你不应该做:
getRemoteData=()=>{
const url=“https://***”;
获取(url)
.then(res=>res.json())
。然后(res=>{
这是我的国家({
数据集:res.records
});
})
.catch(错误=>{
log(“从:+url+获取数据错误”错误:+error);
});
};
然后在调用时,您将得到未定义的
this.renderActivItem(项)}
/>
因为这个.state.data
它从来没有被声明过。好的,但我的问题是我把坐标放在没有's'的地方
render() {
return (
<View>
<FlatList
data={this.state.data}
renderItem={({item}) => this.renderNativeItem(item)}
/>
</View>
);
}
}
class DetailScreen extends React.Component {
render() {
const item = this.props.navigation.state.params.item;
return (
<View style={styles.container}>
<MapView style={styles.map}
initialRegion={{
latitude:item.geometry.coordinate[0] , // don't work
longitude: item.geometry.coordiante[1] , // don't work
latitudeDelta: 0.0,
longitudeDelta: 0.0,
}}
>
<MapView.Marker
coordinate={{latitude: ,
longitude: }}
title={"title"}
description={"description"}
/>
</MapView>
<Text style={styles.text}>name: {item.fields.nbebike}</Text> //work
<Button title="Home" onPress={this._goHome} />
</View>
);
}