React Native Maps: Markers image doesn't show using Custom Marker in react-native-maps

后端 未结 10 1838
别那么骄傲
别那么骄傲 2021-02-05 16:52

I\'m using react-native-maps but I faced a problem that after a lot of googling without answer makes me ask it here. I\'m trying to use Custom Marker for the marker

10条回答
  •  鱼传尺愫
    2021-02-05 17:14

    I Had same problem , from github post

    using below(MapView , Image) pattern first load problem will still occour

    
        
    
    

    so adopted below solution : trick is to save current selected marker id in redux

    class Map extends React.Component {
    render() {
        let {region, markers , markerClick} = this.props;
        const normalMarkerImage = require('../../images/normal_marker.png');
        const selectedMarkerImage = require('../../images/selected_marker.png');
        return !isObjectEmpty(region) && !isObjectEmpty(markers) ? (
            
                {markers && markers.length > 0
                    ? markers.map((marker, i) => (
                             markerClick(e, marker)}
                                image={
                                    this.props.selectedMarker === marker._id
                                        ? selectedMarkerImage
                                        : normalMarkerImage
                                }
                            />
                      ))
                    : null}
            
        ) : null;
      }
    }
    

    component Function

    markerClick = async (event, data) => {
        this.props.dispatch(
            setMarkerIconInRedux(this.state.popover.currentData._id)
        );
    };
    

    Action

    export const setMarkerIconInRedux = where => {
        return {
            type: 'SET_MARKER_ICON',
            _id: where,
        };
    };
    

    Redux

    export const currentSelectedMarker = (state = {selectedMarker: ''}, action) => 
    {
        if (action.type === 'SET_MARKER_ICON') {
            return {selectedMarker: action._id};
        } else if (action.type === 'REMOVE_MARKER_ICON') {
            return {selectedMarker: ''};
        } else {
            return state;
        }
    };
    

提交回复
热议问题