最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - onRegionChangeComplete function calling multiple times using react native maps - Stack Overflow

programmeradmin0浏览0评论

I want to changes my initial region when move some position in map, I used onRegionChangeComplete functionality for that but it is calling twice or trice at a time.

Here is my code:

onRegionChangeComplete(region) {
  if (!this.state.initialRegionChange) {
    console.log("changeRegion:"+JSON.stringify(region))
    var initialRegion = {
      latitude: region.latitude,
      longitude :region.longitude,
      latitudeDelta: LATITUDE_DELTA,
      longitudeDelta: LONGITUDE_DELTA
    }
    var lat = parseFloat(region.latitude)
    var lang = parseFloat(region.longitude)
  } else {
    this.setState({
      initialRegionChange:false
    })
  }
}

render() {
  return (
    <MapView
      ref="map"
      style={styles.map}
      initialRegion={this.state.region}
      provider={MapView.PROVIDER_DEFAULT}
      zoomEnabled={true}
      onRegionChangeComplete={this.onRegionChangeComplete.bind(this)}
      pitchEnabled={true}
      showsCompass={true}
      showsBuildings={true}
      showsUserLocation={true}
      showsTraffic={true}
      showsIndoors={true}
    />
  )
}

Here is the module I am following.

I want to changes my initial region when move some position in map, I used onRegionChangeComplete functionality for that but it is calling twice or trice at a time.

Here is my code:

onRegionChangeComplete(region) {
  if (!this.state.initialRegionChange) {
    console.log("changeRegion:"+JSON.stringify(region))
    var initialRegion = {
      latitude: region.latitude,
      longitude :region.longitude,
      latitudeDelta: LATITUDE_DELTA,
      longitudeDelta: LONGITUDE_DELTA
    }
    var lat = parseFloat(region.latitude)
    var lang = parseFloat(region.longitude)
  } else {
    this.setState({
      initialRegionChange:false
    })
  }
}

render() {
  return (
    <MapView
      ref="map"
      style={styles.map}
      initialRegion={this.state.region}
      provider={MapView.PROVIDER_DEFAULT}
      zoomEnabled={true}
      onRegionChangeComplete={this.onRegionChangeComplete.bind(this)}
      pitchEnabled={true}
      showsCompass={true}
      showsBuildings={true}
      showsUserLocation={true}
      showsTraffic={true}
      showsIndoors={true}
    />
  )
}

Here is the module I am following.

Share Improve this question edited Mar 23, 2024 at 4:47 CPlus 4,77644 gold badges30 silver badges73 bronze badges asked Dec 15, 2017 at 12:15 Hussian ShaikHussian Shaik 2,6859 gold badges41 silver badges59 bronze badges 1
  • You can Handle it through Logic . Define some Particular Radius or distance . So your onRegionChangeComplete will not working twice or thrice – Syed Zain Ali Commented Dec 27, 2017 at 18:17
Add a comment  | 

3 Answers 3

Reset to default 11

This may be an old question but I'll provide what worked for me for reference. In my case it was just that the precision of the region changed onRegionChangeComplete, which made the map think the region had been changed. To fix this I simply compared the input region to the previously set region, but limiting the decimals of the coordinates to 6 in the comparison. Something like this:

onRegionChange = (region) => {
    if(region.latitude.toFixed(6) === this.state.region.latitude.toFixed(6)
      && region.longitude.toFixed(6) === this.state.region.longitude.toFixed(6)){
        return;
    }

    this.setState({region});
}

Use react-native-maps version => v0.28.0, this issue not there in iOS,

For android :-

onRegionChangeComplete is called once when the region changes, such as when the user is done moving the map. The second parameter is an object containing more details about the move. isGesture property indicates if the move was from the user (true) or an animation (false). Note: isGesture is supported by Google Maps only.

onRegionChangeComplete={(region, gesture) => {
              if (Platform.OS === 'android') {
                if (gesture.isGesture) {
                  onRegionChange(region);
                }
              } else {
                onRegionChange(region);
              }
            
          }}

This is a documented issue with react-native-maps and there is even a pull request submitted to fix it: https://github.com/react-community/react-native-maps/pull/1597. You could either merge the pull request into your local copy of react-native-maps right now, or wait for it to be released. I would recommend adding your support to the PR to bring more attention to it and get it merged faster. I know it's not a solution to your question, but there's nothing you can do in your source code at the moment to fix it - you need the library itself to change. Hope this helps!

发布评论

评论列表(0)

  1. 暂无评论