Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to go to my current location using custom button in react-native-maps?

I am using react-native-maps on android. I put a custom button clicking on which should go to supplied coordinates (my current location)

<MapView
          ref={(map) => { this.map = map; }}
          provider={PROVIDER_GOOGLE}
          style={{ height: '100%', width: '100%' }}
          annotations={markers} 
          showsUserLocation={true}
          showsMyLocationButton={true}
          initialRegion={this.state.region}
          onRegionChangeComplete={this.onRegionChange}
        >

my button -

<TouchableOpacity onPress={this.gotToMyLocation} style={[Style.alignJustifyCenter, {
          width: 60, height: 60,
          position: "absolute", bottom: 20, right: 20, borderRadius: 30, backgroundColor: "#d2d2d2"
        }]}>
          <Image
            style={{ width: 40, height: 40 }}
            source={Images.myLocation}
          />
        </TouchableOpacity>

gotToMyLocation method -

gotToMyLocation(){
    console.log('gotToMyLocation is called')
    navigator.geolocation.getCurrentPosition(
      ({ coords }) => {
        console.log("curent location: ", coords)
        console.log(this.map);
        if (this.map) {
          console.log("curent location: ", coords)
          this.map.animateToRegion({
            latitude: coords.latitude,
            longitude: coords.longitude,
            latitudeDelta: 0.005,
            longitudeDelta: 0.005
          })
        }
      },
      (error) => alert('Error: Are location services on?'),
      { enableHighAccuracy: true }
    )
  }

console.log(this.map) always shows undefined.

I want to move map to my current location when clicking the button.

like image 609
newdeveloper Avatar asked Jul 28 '19 17:07

newdeveloper


Video Answer


2 Answers

Actually i found out a silly mistake where binding the method would make it work. may be useful to someone. this.gotToMyLocation = this.gotToMyLocation.bind(this);

like image 70
newdeveloper Avatar answered Sep 19 '22 16:09

newdeveloper


Default my location button

      showsUserLocation={true}
      showsMyLocationButton={true}

enter image description here

Example:

        <MapView
          provider={PROVIDER_GOOGLE}
          showsUserLocation={true}
          showsMyLocationButton={true}
          style={{
            ...StyleSheet.absoluteFillObject,
            width: '100%',
          }}
          initialRegion={{
            latitude: targetLocation.latitude,
            longitude: targetLocation.longitude,
            latitudeDelta: 0.0922,
            longitudeDelta: 0.0421,
          }}>

A custom button

enter image description here

    <MaterialIcons
      style={style.myLocationIcon}
      name="my-location"
      onPress={() => {
        dispatch(settingActions.getLocation());
        mapRef.current.animateToRegion({
          latitude: myLatitude,
          longitude: myLongitude,
          latitudeDelta: 0.0922,
          longitudeDelta: 0.0421,
        });
      }}
    />

For this you need to define mapRef

export const mapRef = React.createRef();

return(
    <MapView
      ref={mapRef}

Animation

mapRef.current.animateToRegion({ will take care of animation while you switch from one location to another.

like image 38
illusionist Avatar answered Sep 20 '22 16:09

illusionist