我正在使用 React Native 并尝试理解一些示例。
我遇到一个问题
我试图在我的获取中传递纬度,但我遇到了一个问题,有人说我“纬度”不存在
class App extends React.Component {
constructor() {
super();
this.state = {
region: {
latitude: LATITUDE,
longitude: LONGITUDE,
latitudeDelta: LATITUDE_DELTA,
longitudeDelta: LONGITUDE_DELTA,
},
markers: [],
loaded: false
}
}
componentDidMount() {
navigator.geolocation.getCurrentPosition(
(position) => {
console.log(position);
this.setState({
region: {
latitude: position.coords.latitude,
longitude: position.coords.longitude,
latitudeDelta: LATITUDE_DELTA,
longitudeDelta: LONGITUDE_DELTA,
}
});
},
(error) => this.setState({ error: error.message }),
{ enableHighAccuracy: false, timeout: 200000, maximumAge: 1000 },
);
this.getLocations()
}
getLocations(){
return fetch('https://***&geofilter.distance='+latitude+'%2C2.3883402698750875%2C2000') //here
.then(response => response.json())
.then(responseData =>{
var markers = [];
你能帮我吗,谢谢:)
最佳答案
在 getLocations
中,您尝试使用 latitude
变量,但范围内没有变量。您可能想要this.state.region.latitude
。
如果您尝试使用 this.state.region.latitude
,则需要将调用移至 componentDidMount
中的 getLocations
> 因此它位于状态更改完成处理程序内:
componentDidMount() {
navigator.geolocation.getCurrentPosition(
(position) => {
console.log(position);
this.setState({
region: {
latitude: position.coords.latitude,
longitude: position.coords.longitude,
latitudeDelta: LATITUDE_DELTA,
longitudeDelta: LONGITUDE_DELTA,
}
},
() => { // ***
this.getLocations(); // ***
}); // ***
},
(error) => this.setState({ error: error.message }),
{ enableHighAccuracy: false, timeout: 200000, maximumAge: 1000 },
);
// *** Not here
}
关于javascript - 如何在获取请求中传递变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59090440/