Did you find a fix for this? I think I am seeing the same problem. When I add a marker to my array of markers via long press, it doesn't appear until after the next marker is added....
If I add key={markers.length} to MapView this fixes the problem of the newest marker not showing, by forcing a reload of the map. But reloading the map is not ideal because it defaults back to its initial settings and disrupts the user experience.
My code:
import MapView, { Marker } from "react-native-maps";
import { StyleSheet, View } from "react-native";
import { useState } from "react";
function Map() {
const [markers, setMarkers] = useState([]);
const addMarker = (e) => {
const { latitude, longitude } = e.nativeEvent.coordinate;
setMarkers((prev) => [
...prev,
{ id: Date.now().toString() + markers.length, latitude, longitude },
]);
};
return (
<View style={styles.container}>
<MapView
style={styles.map}
initialRegion={{
latitude: 53.349063173157184,
longitude: -6.27913410975665,
latitudeDelta: 0.0922,
longitudeDelta: 0.0421,
}}
onLongPress={addMarker}
>
{markers.map((m) => {
console.log(m);
return (
<Marker
key={m.id}
identifier={m.id}
coordinate={{ latitude: m.latitude, longitude: m.longitude }}
/>
);
})}
</MapView>
</View>
);
}
export default Map;
const styles = StyleSheet.create({
container: {
//flex: 1,,
},
map: {
width: "100%",
height: "100%",
},
button: {
position: "absolute",
top: 10,
right: 10,
width: 80,
height: 80,
borderRadius: 10,
overflow: "hidden",
borderWidth: 2,
borderColor: "#fff",
backgroundColor: "#ccc",
elevation: 5,
},
previewMap: {
flex: 1,
},
});