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

3

6

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.

Nora answered 28/7, 2019 at 17:8 Comment(0)
N
6

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);

Nora answered 28/7, 2019 at 17:34 Comment(0)
D
9

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.

Disulfiram answered 19/8, 2021 at 8:32 Comment(0)
N
6

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);

Nora answered 28/7, 2019 at 17:34 Comment(0)
D
1

move your function gotToMyLocation:

if Using Class - in class before render()

or

if using function - use function gotToMyLocation(){...} and don't use this

Distend answered 18/2, 2021 at 15:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.