简体   繁体   English

在新的 position 更改上更新地图上的标记 position - 反应原生

[英]Update the Marker position on Maps on a new position change - React native

I have initialized a react native map with an initial region co-ordinate.我已经使用初始区域坐标初始化了反应原生 map。

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

Now fetching a new location from the server, which has a new latitude and longitude, which I want to display as a new marker on my maps.现在从服务器获取一个新的位置,它有一个新的纬度和经度,我想在我的地图上显示为一个新的标记。 I have updated the region with new latitude and longitude on ononRegionChange.我在 ononRegionChange 上用新的纬度和经度更新了该地区。

But I'm unable to invoke the new marker position on the region change.但我无法在区域更改时调用新标记 position。 Can anyone help me out here?有谁可以帮我离开这里吗?

If you are looking for something using which you can move the marker to new position then give the below suggestion a try.如果您正在寻找可以将标记移动到新的 position 的东西,请尝试以下建议。

We are using the same approach in our project, so I hope this would help!我们在我们的项目中使用相同的方法,所以我希望这会有所帮助!

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

you have to set region prop for MapView and update it when your lat and long changed.您必须为MapView设置region属性,并在您long lat和经度发生变化时对其进行更新。 your state object should be an object like this as well:你的 state object 也应该是这样的 object :

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

and here is the MapView这是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>

Note that you have to set both mapRegion and markerCoordinate on every data fetching from the server.请注意,您必须在从服务器获取的每个数据上同时设置mapRegionmarkerCoordinate

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM