How do I set a selected marker with a ref and animate using react native and react-native-maps

56 views Asked by At

I am trying to animate a selected marker using ref.

I have it so far that if I press a marker one of them jumps but not the selected one but I cannot seem to change which one is selected? Ideally I want to be able to press a marker and that selects it and when I press the button, it animates the selected marker.

I have tried adding the current marker to a state but it does not work and I have tried setting the ref when clicked but get error saying its read only? I have also tried passing the markerRef to the handlePress and tried to substitute the .current for it but no good?

The code is :

import React, { useRef, useState } from 'react';
import { View, StyleSheet, TouchableOpacity, Text } from 'react-native';
import MapView, { Marker } from 'react-native-maps';
import * as Animatable from 'react-native-animatable';

const MapWithMarkerAnimation = () => {
  const markerRef = useRef(null);


  const handleAnimateButtonPress = () => {
    if (markerRef) {
      markerRef.current.bounce(800);
    }
  };

  return (
    <View style={styles.container}>
      <MapView style={styles.map} initialRegion={{ latitude: 52.4194975, longitude: -1.5101260 }} >
        <Marker coordinate={{ latitude: 52.4194975, longitude: -1.5101260 }} onPress={handleAnimateButtonPress}>
          <Animatable.View ref={markerRef} animation="zoomIn" duration={1000} easing="ease-out">
            <View style={styles.marker} />
          </Animatable.View>
        </Marker>
        <Marker coordinate={{ latitude: 52.514825, longitude: -1.5101260 }} onPress={handleAnimateButtonPress}>
          <Animatable.View ref={markerRef} animation="zoomIn" duration={1000} easing="ease-out">
            <View style={styles.marker} />
          </Animatable.View>
        </Marker>
      </MapView>
      <TouchableOpacity style={styles.button} onPress={handleAnimateButtonPress}>
        <Text style={styles.buttonText}>Animate Marker</Text>
      </TouchableOpacity>
    </View>
  );
};

const styles = StyleSheet.create({
  container: {
    flex: 1,
    alignItems: 'center',
    justifyContent: 'center',
  },
  map: {
    ...StyleSheet.absoluteFillObject,
  },
  marker: {
    width: 20,
    height: 20,
    borderRadius: 10,
    backgroundColor: 'red',
  },
  button: {
    backgroundColor: 'blue',
    paddingVertical: 10,
    paddingHorizontal: 20,
    borderRadius: 5,
    marginTop: 10,
  },
  buttonText: {
    color: 'white',
    fontSize: 16,
    fontWeight: 'bold',
  },
});

export default MapWithMarkerAnimation;
1

There are 1 answers

0
JulesUK On

I finally got it working by adding this in the main App.js

const [selectedMarker, setSelectedMarker] = useState(null);
  const markerRefs = useRef([]);

  const handleMarkerPress = (index) => {
    setSelectedMarker(index);
    markerRefs.current[index].rubberBand(); // Animating selected marker
  };

Then changing the normal View to

<Animatable.View ref={(ref) => (markerRefs.current[index] = ref)}>

and can start the animation by calling

handleMarkerPress(0) - 0 is where position in array.

Hope this helps anyone else struggling with the same issue.