【发布时间】:2019-02-04 02:16:01
【问题描述】:
简而言之,我正在尝试使用 react-native-maps 在地图上绘制标记。
我已经创建了一个从服务器获取坐标和相应 ID 的操作(参见下面的代码)。
export const getPlacesOnMap = () => {
return dispatch => {
dispatch(authGetToken())
.then(token => {
return fetch("myApp?auth=" + token);
})
.catch(() => {
alert("No valid token found!");
})
.then(res => {
if (res.ok) {
return res.json();
} else {
throw(new Error());
}
})
.then(parsedRes => {
const places = [];
for (let key in parsedRes) {
places.push({
// ...parsedRes[key], // this fetches all the data
latitude: parsedRes[key].location.latitude,
longitude: parsedRes[key].location.longitude,
id: key
});
} console.log(places)
dispatch(mapPlaces(places));
})
.catch(err => {
alert("Oops! Something went wrong, sorry! :/");
console.log(err);
});
};
};
export const mapPlaces = places => {
return {
type: MAP_PLACES,
places: places
};
};
我不知道我是否使用了正确的词,但我基本上已经使用 componentWillMount() 测试了代码(如上),它成功地将多个坐标作为对象数组返回。
现在,问题是我不知道下一步该做什么。据我所知,我知道最终目标是创建一个 setState()。但我不知道怎么去那里。
如果有人能指出我正确的方向,那将是一个很大的帮助。
【问题讨论】:
-
这段代码是你的 redux 操作吗?你在用redux吗?你在使用 redux-thunk 吗?
-
是的。是的。是的。对不起,我忘了提。
标签: javascript react-native react-redux redux-thunk react-native-maps