如何在 React Native 中适应特定标记的缩放,并且可以 select 地图中的任何其他标记?

How to fit a zoom in the specific marker in react native and can select any other marker in the map?

在这种情况下,当应用程序初始化时,Pin 标记设置在当前位置,但在同一张地图中我有其他标记,当我尝试 select 其他标记时它再次适合目前的情况,如何解决这个问题?我需要 select 任何标记......并且......当我找到某个地方(有 google 个地方)时,它会设置一个新标记并设置一个新位置并将相机设置为当前搜索放大此 selected 标记的标记,我的另一个问题是制作一个通过按钮获取当前位置(设备用户)的函数...有人可以帮助我吗???

<View style={styles.container}>
    <StatusBar hidden />
    <MapView
      onPress={this.handleMapPress}
      style={StyleSheet.absoluteFill}
      ref={map => (this.mapView = map)}
      rotateEnabled={true}
      scrollEnabled={true}
      showsMyLocationButton={true}
      followsUserLocation={true}
      showsUserLocation={true}
      zoomEnabled={true}
      showsPointsOfInterest={true}
      showBuildings={false}
      //region={this.props.region}
      initialRegion={region}
      provider="google">
      {!!location && (
        <MapView.Marker
          coordinate={location}
          onPress={this.handleMarkerPress}>
          <Image
            source={isAddressVisible ? placholder2 : placholder}
            style={styles.icon}
          />
        </MapView.Marker>
      )}
      {this.state.coordinates.map(
        (coordinates, index, title, description, location) => (
          <MapView.Marker
            onPress={this.handleMapPress}
            ref={mark => (coordinates.mark = mark)}
            key={`coordinate_${index}`}
            title={coordinates.title}
            description={coordinates.description}
            coordinate={{
              latitude: coordinates.latitude,
              longitude: coordinates.longitude,
            }}>
            <Image
              source={isAddressVisible ? placholder2 : placholder}
              style={styles.icon}
            />
          </MapView.Marker>
        )
      )}
      <MapView.Marker coordinate={this.props.region} >
          <Image
            source={ markerImage}
            style={styles.icon}
          />
      </MapView.Marker>
    </MapView>

这里是调用当前位置的按钮:

        <TouchableOpacity
       onPress={this.getlocation}
      style={styles.fab2}>
            <Image
              source={require('../assets/gps.png')}
              style={{
                width: 35,
                height: 35,
                margin: 10,
                tintColor: 'white',
              }}
            />
    </TouchableOpacity>

我找到了这个问题的解决方案,这里是您阅读的点心...

下面是简单的代码:

  pickLocationHandler = event => {
const coords = event.nativeEvent.coordinate;

this.map.animateToRegion({
  ...this.state.focusedlocation,
  latitude: coords.latitude,
  longitude: coords.longitude,
});

this.setState(prevState => {
  return {
    focusedlocation: {
      ...prevState.focusedlocation,

      latitude: coords.latitude,

      longitude: coords.longitude,
    },

    locationChosen: true,
  };
});
};

getLocationHandler = () => {
navigator.geolocation.getCurrentPosition(
  pos => {
    const coordsEvent = {
      nativeEvent: {
        coordinate: {
          latitude: pos.coords.latitude,

          longitude: pos.coords.longitude,
        },
      },
    };

    this.pickLocationHandler(coordsEvent);
  },

  err => {
    console.log(err);

    alert(
      'Ocorreu um erro ao carregar sua localização, por favor ligue o GPS!'
    );
  }
 );
};

我在我创建的按钮上调用了这个函数:

        <TouchableOpacity
     onPress={this.getLocationHandler}
      style={styles.fab2}>
            <Image
              source={require('../assets/gps.png')}
              style={{
                width: 35,
                height: 35,
                margin: 10,
                tintColor: '#FF3D00',
              }}
            />
    </TouchableOpacity>

这里是 link 的完整代码:

https://snack.expo.io/@matheus_cbrl/react-native-maps-autocomplete---address---current-location-button

如果这解决了您可能遇到的问题,请点赞!! :)