简体   繁体   English

使用react-native-maps在地图上绘制标记

[英]Plotting markers on a map using react-native-maps

In short, I'm trying to plot markers on a map using react-native-maps. 简而言之,我正在尝试使用react-native-maps在地图上绘制标记。

I've gone as far as creating an action to fetch the coordinates and respective ID from the server (see code below). 我所做的只是创建一个从服务器获取坐标和相应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
 };
};

I don't know if I'm using the right words, but I've essentially tested the code (above) using componentWillMount(), and it successfully returned multiple coordinates as an array of objects. 我不知道我是否使用了正确的单词,但实际上我已经使用componentWillMount()测试了代码(上面),并且它成功地将多个坐标作为对象数组返回。

Now, the problem is I don't know what to do next. 现在,问题是我不知道下一步该怎么做。 As much as I understand, I know the end goal is to create a setState(). 据我了解,我知道最终目标是创建一个setState()。 But I don't know how to get there. 但是我不知道如何到达那里。

Would be a great help if someone can point me in the right direction. 如果有人可以指出我正确的方向,那将是很大的帮助。

You need to create an async action. 您需要创建一个异步操作。 You can dispatch different actions inside an async action based on whether the async function inside it is resolved or rejected. 您可以根据解析或拒绝异步函数内部的异步功能,在异步动作内部调度不同的动作。

export function getPlacesOnMap(token) {
    return async function(dispatch) {
        dispatch({
            type: "FETCHING_PLACES_PENDING"
        });
        fetch("myApp?auth=" + token)
        .then(res => {
            dispatch({
                type: "FETCHING_PLACES_FULFILLED",
                payload: res.json()
            });
        })
        .catch(error => {
            dispatch({
                type: "FETCHING_PLACES_REJECTED",
                payload: error
            });
        });
    };
}

If your authGetToken() function is also a promise, you need to dispatch this action after authGetToken() was resolved. 如果您的authGetToken()函数也是一个承诺,则需要在authGetToken()解决之后分派此操作。 You can use the action.payload in your "FETCHING_PLACES_FULFILLED" case of your reducer(s) to be able to use the retrieved data. 您可以在减速器的“ FETCHING_PLACES_FULFILLED”情况下使用action.payload以便能够使用检索到的数据。

UPDATE UPDATE

Your reducer should be like this: 您的减速器应如下所示:

export default function reducer(
    state = {
        loadingMarkers : false,
        markers : [],
        error : null,
    },
    action
) {
    switch (action.type) {
        case "FETCHING_PLACES_PENDING":
            return { ...state, loadingMarkers: true };
        case "FETCHING_PLACES_FULFILLED":
            return { ...state, loadingMarkers: false, markers: action.payload};
        case "FETCHING_PLACES_REJECTED":
            return { ...state, loadingMarkers: false, error: action.payload };
        default:
            return state;
    }
}

Now you can connect your component to redux and use your markers when they are fetched. 现在,您可以将组件连接到redux并在获取标记时使用它们。

have a look at this example and connect docs 看一下这个例子连接文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM