地图始终位于搜索到的位置标记的中心

问题描述 投票:1回答:1

[当我输入代码:region={this.props.region}时,它可以工作并将地图居中放置在搜索位置,但不允许在地图上选择其他标记...如果我取下此部分,它总是返回到搜索位置代码:region={this.props.region}中,我可以选择其他标记,但是当我搜索其他位置时,相机不会移动到所选位置。在这种情况下如何进行?

这里是一些代码:

<MapView
        provider="google"
        style={styles.map}
        //region={this.props.region}
        initialRegion={this.state.focusedlocation}
        ref={ref => (this.map = ref)}>
        {this.renderMarkers()}
        <MapView.Marker
          onPress={this.pickLocationHandler}
          coordinate={this.props.region}>
          <Image source={markerImage} style={styles.icon} />
        </MapView.Marker>
      </MapView>

以下是标记动画的代码:

 pickLocationHandler = event => {
const coords = event.nativeEvent.coordinate;
console.log('Location picker Marker', coords);
this.map.animateToRegion({
  ...this.state.focusedlocation,
  latitude: coords.latitude,
  longitude: coords.longitude,
  latitudeDelta: LATITUDE_DELTA,
  longitudeDelta: LONGITUDE_DELTA,
});

Please open this snack to entire code

react-native location google-places
1个回答
1
投票

region当您要控制地图的视口时使用。您可以使用animateCamera将地图移至您搜索的位置。

this.map.animateCamera({
  center: {latitude, longitude}
})

更新

map-view.js

export default class MapView extends Component {
  ...
  animateToLocation = (location) => {
    this.map.animateToRegion({
      latitude: location.latitude,
      longitude: location.longitude,
      latitudeDelta: LATITUDE_DELTA,
      longitudeDelta: LONGITUDE_DELTA,
    });
  }
  ...
  render() {
    return (
      <View style={styles.container} {...this.props}>
      ...
    )
  }
}

map-container.js

class MapContainer extends React.Component {
  ...
  getCoordsFromName(loc) { 
    this.map.animateToLocation({
      latitude: loc.lat,
      longitude: loc.lng,
    })
  }

  render() {
    return (
      <View style={{ flex: 1}}>
          <MyMapView ref={ref => this.map = ref} region={this.state.region}/>
          <MapInput style = {{flex: 1, position : 'absolute'}} notifyChange={loc => this.getCoordsFromName(loc)} />
      </View>
    );
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.