Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot get ref to MapView - react-native-maps

I have been trying to use the methods on MapView to animate to a region, to do that I need access to the ref but it is undefined. Everything else is working properly, I just can't get the ref and anytime I try to call a method e.g `

this.map.animateToRegion({
  latitude: 0,
  longitude: 0,
  latitudeDelta: 1,
  longitudeDelta: 1
});

I get an error saying:

Cannot read property 'animateToRegion' of undefined

<MapView
  ref={r => this.map = r}
  mapPadding={{ top: 0, left: 0, right: 0, bottom: 400 }}
  provider='google'
  style={{ flex: 1 }}
  region={this.region}
>
  {this.renderBarberMarkers()}
</MapView>
like image 330
Craques Avatar asked Jul 15 '19 20:07

Craques


3 Answers

If you are using hooks, here is your way:

On top of your function:

const mapView = React.createRef();
const animateMap = () => {
    mapView.current.animateToRegion({ // Takes a region object as parameter
        longitude: 28.97916756570339,
        latitude: 41,
        latitudeDelta: 0.4,
        longitudeDelta: 0.4,
    },1000);
}

Give your MapView a ref like this:

<MapView provider={PROVIDER_GOOGLE} region={{
       latitude: 37.78825,
       longitude: -122.4324,
       latitudeDelta: 0.015,
       longitudeDelta: 0.0121,
   }}
   ref={mapView}
   style={{flex:1}}>
</MapView>

Last thing to do is triggering the animation, you can do something like this:

<TouchableOpacity onPress={animateMap}><Text>Start</Text></TouchableOpacity>
like image 164
Halil Şağan Avatar answered Oct 30 '22 00:10

Halil Şağan


here is the mapView

<MapView
  ref={(map) => { this.map = map; }}
  showsUserLocation={true}
  showsMyLocationButton={true}
  initialRegion={this.state.region}
/>

then, you need to call

//animate to user current location
this.map.animateToRegion(yourRegionObject,1000)

I am using like this;

//to get user location
getLocation(){

    navigator.geolocation.getCurrentPosition(
        (position) => {
            let latitude = parseFloat(position.coords.latitude);
            let longitude = parseFloat(position.coords.longitude);

            console.log('location position: ', position);

            let region = {
                latitude: latitude,
                longitude: longitude,
                latitudeDelta: 0.0522,
                longitudeDelta: 0.0321,
            };

            this.setState({region: region});

            console.log('position: ', position);
            console.log('this.map: ', this.map);

            //animate to user current location
            this.map.animateToRegion(region,1000)


        },
        (error) => console.log('position error!!!', error),
        {enableHighAccuracy: false, timeout: 3000}
    );
}
like image 4
Orhan Avatar answered Oct 30 '22 01:10

Orhan


with the hook you can use

first import useRef, useEffect

import React, {useRef, useEffect} from 'react';

then

const mapRef = useRef(null);

add in you MapView element:

<MapView
 provider={PROVIDER_GOOGLE}
 ref={mapRef} />

you can find MapRef in use

useEffect(() => {
    if (mapRef) {
      console.log(mapRef);
    }
});
like image 2
Amir Ardalan Avatar answered Oct 30 '22 01:10

Amir Ardalan