【问题标题】:Update the Marker position on Maps on a new position change - React native在新的位置更改时更新地图上的标记位置 - 反应原生
【发布时间】:2020-05-05 11:18:35
【问题描述】:

我已经使用初始区域坐标初始化了一个反应原生地图。

 <MapView
     style={{ ...styles.map, marginTop: this.state.marginTop }}
     initialRegion={this.state.region}
     showsUserLocation={true}
     followsUserLocation={true}
     onMapReady={this.onMapReady}
     onRegionChangeComplete={this.onRegionChange}         
     >
     <MapView.Marker
     coordinate={this.state.region}
     title={"Your location"}
     draggable                  
     />
 </MapView>

现在从服务器获取一个新位置,它有一个新的纬度和经度,我想在我的地图上显示为一个新的标记。我在 ononRegionChange 上用新的纬度和经度更新了该地区。

但我无法在区域更改时调用新的标记位置。有谁能帮帮我吗?

【问题讨论】:

  • 你能分享你更新新区域的代码吗?
  • this.setState({ region: { latitude: myArray[0], longitude: myArray[1] },这就是我更新状态的方式

标签: javascript react-native react-native-android react-native-maps


【解决方案1】:

如果您正在寻找可以将标记移动到新位置的东西,请尝试以下建议。

我们在我们的项目中使用相同的方法,所以我希望这会有所帮助!

// When updating region
const coordinate = new MapView.AnimatedRegion({
  latitude: newRegion.latitude,
  longitude: newRegion.longitude,
  latitudeDelta: 0,
  longitudeDelta: 0,
});

this.setState({ region: newRegion, coordinate }, () => {
  if (!!this._markerRef) {
    coordinate.timing({
      latitude,
      longitude,
      duration: MARKER_ANIMATION_DURATION,
    }).start();
  }
})

// rendering MapView
<MapView
  style={[styles.map, { marginTop: this.state.marginTop }]}
  initialRegion={this.state.region}
  showsUserLocation={true}
  followsUserLocation={true}
  onMapReady={this.onMapReady}
  onRegionChangeComplete={this.onRegionChange}
>
  <MapView.Marker.Animated
    ref={ref => {this._markerRef = ref;}}
    coordinate={this.state.coordinate}
    title="Your location"
    draggable
  />
</MapView>;

【讨论】:

    【解决方案2】:

    您必须为MapView 设置region 属性,并在您的latlong 更改时更新它。你的状态对象也应该是这样的对象:

    this.state = { 
       mapRegion: {
         latitude: 14.343,
         longitude: 1600,
         latitudeDelta: 0.002,
         longitudeDelta: 0.0002,
       },
       markerCoordinate: {
         latitude: 14.343,
         longitude: 1600,
        }
    }
    

    这里是MapView

     <MapView
         style={{ ...styles.map, marginTop: this.state.marginTop }}
         initialRegion={this.state.mapRegion}
         region={this.state.mapRegion}
         showsUserLocation
         followsUserLocation
         onMapReady={this.onMapReady}
         onRegionChangeComplete={this.onRegionChange}         
       >
         <MapView.Marker
           coordinate={this.state.markerCoordinate}
           title={"Your location"}
           draggable                  
         />
     </MapView>
    

    请注意,您必须在从服务器获取的每个数据上同时设置 mapRegionmarkerCoordinate

    【讨论】:

    • 您好,谢谢您的回复,但是如果我需要添加多个标记,如何使用相同的代码?
    • 嗨。您只需将markerCoordinate 状态更改为数组并使用该数组生成MapView.Marker 并将每个坐标传递给每个标记。 (使用map循环生成标记)
    猜你喜欢
    • 2020-02-24
    • 1970-01-01
    • 1970-01-01
    • 2018-03-13
    • 2014-09-19
    • 1970-01-01
    • 2020-05-30
    • 1970-01-01
    • 2017-03-17
    相关资源
    最近更新 更多