79742220

Date: 2025-08-21 11:39:09
Score: 7.5
Natty:
Report link

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,
  },
});
Reasons:
  • RegEx Blacklisted phrase (3): Did you find a fix
  • RegEx Blacklisted phrase (1.5): fix for this?
  • Long answer (-1):
  • Has code block (-0.5):
  • Me too answer (2.5): I am seeing the same problem
  • Contains question mark (0.5):
  • Starts with a question (0.5): Did you find a fix for this
  • Low reputation (1):
Posted by: John C