Home > database >  Get coordinates of where onLongPress event took place - react-native-maps
Get coordinates of where onLongPress event took place - react-native-maps

Time:03-31

I am trying to get the lat and lng coords of where a onLongPress event took place. I have the even setup, but do not know where the coords are stored in the event. Anyone have any experience with this?

<MapView
      initialRegion={{
        latitude: props.location.coords.latitude,
        longitude: props.location.coords.longitude,
        latitudeDelta: 0.04,
        longitudeDelta: 0.05,
      }}
      onLongPress={(e) => {
        console.log(e);
      }}
      showsMyLocationButton={true}
      showsUserLocation={true}
      style={styles.map}
    >
      {markers.map((marker, index) => (
        <Marker
          key={index}
          coordinate={marker.latlng}
          title={marker.title}
          description={marker.description}
          onPress={() => navigation.navigate("Marker", { data: marker })}
        ></Marker>
      ))}
    </MapView>

console.log(e) prints out a massive object, but I do not see the coords anywhere in that object.

CodePudding user response:

It is hidden in e.nativeEvent. Consider the following code snippet in order to access the coordinates.

<MapView
      ...
      onLongPress={(e) => {
        console.log(e.nativeEvent)
      }}
      ...
    >
     ...
</MapView>

The nativeEvent contains the following object.

{
    target: 127,
    coordinate: {
        latitude: 47.66491318510563,
        longitude: 4.238049164414406
     },
     position: {
        y: 489.9999084472656,
        x: 263.99993896484375
     }
}
  • Related