如何使用react-native-maps中的自定义按钮前往我当前的位置?

new*_*per 6 react-native react-native-maps

我在android上使用react-native-maps。我放置了一个自定义按钮,单击该按钮应转到提供的坐标(我当前的位置)

<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}
        >
Run Code Online (Sandbox Code Playgroud)

我的按钮 -

<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>
Run Code Online (Sandbox Code Playgroud)

获取到我的位置方法 -

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 }
    )
  }
Run Code Online (Sandbox Code Playgroud)

console.log(this.map) 始终显示未定义。

我想在单击按钮时将地图移动到当前位置。

ill*_*ist 9

默认my location按钮

      showsUserLocation={true}
      showsMyLocationButton={true}
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

例子:

        <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,
          }}>
Run Code Online (Sandbox Code Playgroud)

自定义按钮

在此输入图像描述

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

为此,您需要定义mapRef

export const mapRef = React.createRef();

return(
    <MapView
      ref={mapRef}
Run Code Online (Sandbox Code Playgroud)

动画片

mapRef.current.animateToRegion({当您从一个位置切换到另一个位置时,它将处理动画。


new*_*per 6

实际上我发现了一个愚蠢的错误,绑定该方法可以使其工作。可能对某人有用。this.gotToMyLocation = this.gotToMyLocation.bind(this);