Cannot get ref to MapView - react-native-maps
Asked Answered
F

4

8

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>
Filmer answered 15/7, 2019 at 20:49 Comment(2)
Facing the same issue..Brazzaville
duplicate answer enter link description herePrussiate
A
7

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>
Auric answered 20/8, 2020 at 17:29 Comment(1)
this is perfect, I couldn't figure out where everything was supposed to go and now my map is beautiful :DBalderdash
W
5

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}
    );
}
Walston answered 16/7, 2019 at 12:12 Comment(0)
S
2

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);
    }
});
Sapsucker answered 25/6, 2020 at 14:18 Comment(0)
I
0

Have you tried using

React.createRef();

with your map?

or

what I did was export it outside my class like this

export let mapRef: MapView | null;

and also created exported functions like

export function animateToRegion(region: Region, duration?: number) {
  if (mapRef) {
    mapRef.animateToRegion(region, duration);
  }
}

and my MapView look something like this

    <MapView
 ref={(c) => mapRef = c}
 showsMyLocationButton={false} />

outside my class, that way if it is a component you can easily just call it.

Insulator answered 15/7, 2019 at 23:38 Comment(1)
I tried both solutions before, I could not understand the second one. I don't understand the line export let mapRef: MapView | null; , what does it do. It looks like TypeScript, correct me if I am wrong and I am using JavascriptFilmer

© 2022 - 2024 — McMap. All rights reserved.