react-native-maps: Map not working on android as ios, when remounted
Summary
This inssues just happens on android.
The map marker don’t shows after i unmount the component that contains a modal react with the map. I have been another problem that’s when the state of mapType changes,it’s don’t changes in according.
I’m not sure about this bug, i have tried many time and it didn’t work as i wanted, I’ll be happy for any help, thanks.
Reproducible sample code
///////modal
import React from 'react';
import { Modal, View } from 'react-native';
const ModalView = ({ visible, callbackclose, children }) => (
<Modal visible={visible} onRequestClose={callbackclose} animationType="slide">
<View>
{children}
</View>
</Modal>
);
export default ModalView;
///////app.js
import React, { useState, useEffect, useRef } from 'react';
import { SafeAreaView, TouchableOpacity, Text, Dimensions, Image } from 'react-native';
import ModalView from './ModalView';
import MapView, { Marker } from 'react-native-maps';
const MapaImovel = ({ mapaHibrido, initialRegion, coordinate, windowWidth, windowHeight }) => {
const [refreshMap, setRefreshMap] = useState(false);
const mapRef = useRef(null);
useEffect(() => { triggerRefresh(); }, [mapaHibrido]);
const triggerRefresh = () => {
setRefreshMap(true);
setTimeout(() => setRefreshMap(false), 500);
};
if (refreshMap) return null;
return (
<MapView
key={`map-${mapaHibrido}-${refreshMap}`}
ref={mapRef}
initialRegion={initialRegion}
style={{ width: windowWidth, height: windowHeight }}
mapType={mapaHibrido ? 'hybrid' : 'standard'}
showsUserLocation
>
<Marker coordinate={coordinate.location}>
<Image source={require('./images/pin.png')} style={{ width: 40, height: 38 }} resizeMode="contain" />
</Marker>
</MapView>
);
};
const App = () => {
const [activeMapa, setActiveMapa] = useState(false);
const windowWidth = Dimensions.get('window').width;
const windowHeight = Dimensions.get('window').height;
const initialRegion = { latitude: -23.55052, longitude: -46.633308, latitudeDelta: 0.0922, longitudeDelta: 0.0421 };
const coordinate = { id: 1, location: { latitude: -23.55052, longitude: -46.633308 } };
return (
<SafeAreaView style={{ flex: 1 }}>
<TouchableOpacity onPress={() => setActiveMapa(true)} style={{ padding: 20, backgroundColor: 'blue' }}>
<Text style={{ color: 'white' }}>Abrir Mapa</Text>
</TouchableOpacity>
<ModalView visible={activeMapa} titulo="Localização do Imóvel" callbackclose={() => setActiveMapa(false)}>
<MapaImovel mapaHibrido={false} initialRegion={initialRegion} coordinate={coordinate} windowWidth={windowWidth} windowHeight={windowHeight} />
</ModalView>
</SafeAreaView>
);
};
export default App;
Steps to reproduce
To reproduce, open and close the modal, after opening it again, it is expected that the map marker will not appear again.
Expected result
I expected that when I open and reopen the component whith the map, your marker shows normaly.
Actual result
Currently, when i open the modal and the component is mounted, the mapa works nomarly, but when i reopen, the map marker don’t shows. When the state of mapType changes, his don’t changes in according.
React Native Maps Version
1.20.0
What platforms are you seeing the problem on?
Android
React Native Version
0.75.2
What version of Expo are you using?
Not using Expo
Device(s)
Redmi 9 (MIUI V12.5.6.0.RJCMIXM)
Additional information
No response
About this issue
- Original URL
- State: open
- Created 2 years ago
- Comments: 15
I think I found a temporary solution. Apparently, the MapView is conflicting with React Native’s loading process during remounting (THEORY, NOTHING PROVEN -> JUST MY THOUGHTS). For example, I’m using React Navigation, and after login, the first screen to be rendered is the map, which causes the markers not to render. The solution I found was to add a setTimeout inside a useEffect with a delay of 3 to 5 seconds. I used a state like [render, setRender] = useState(false), and after 3 seconds in the setTimeout, I set it to true. In the view, I added a conditional: {render && <MapView> … </MapView>}.
I encounter the similar issue in Expo SDK 52 (react-native-maps 1.18.0) app, but in our case, there is another main tab map, so the problem seems to be more complicated.
Instead of above
[render, setRender] = useState(false)and{render && ... }with delay solution, the followingmapViewRef.current?.setState({ isReady: true })call inonLayoutevent seems to be possible, but I am not sure whether this is a valid and safe solution.For anyone stumbling on this you can solve this issue like: