0

我有一个<MapView>,当我滚动地图时,该区域永远不会更新。该地图基本上停留在一个区域上。this.props.onRegionChange当我滚动时,它似乎传递了阻止地图滚动的完全相同的区域。如何让它在滚动到区域中传递?

<MapView
          style={styles.map}
          region={this.props.region}
          onRegionChange={this.props.onRegionChange}
        >
          {
            this.props.markers.map(marker => {
              return (
                <MapView.Marker
                  coordinate={{ latitude: marker.latitude, longitude: marker.longitude }}
                  title={marker.name}
                />
              )
            })}
        </MapView>

完整代码:

地图.js

import { StyleSheet } from 'react-native'
import React, { Component } from 'react'
import MapView from 'react-native-maps'
import { connect } from 'react-redux'
import {
  Button,
  Container
} from 'native-base'

import selectMarkers from './markers.selector'

import { updateRegion } from './map.action'
import Icon from 'react-native-vector-icons/FontAwesome'
import { toggleMenu } from '../search-page/searchPage.action'
import mapStyle from './style'

const mapStateToProps = (state) => ({
  region: state.get('map').get('region'),
  markers: selectMarkers(state)
})

const mapDispatchToProps = (dispatch) => ({
  onRegionChange: (region) => {
    dispatch(updateRegion(region))
  },
  onToggleMenuClick: () => {
    dispatch(toggleMenu())
  }
})

class Map extends Component {

  componentDidMount() {
    const { store } = this.context
    this.unsubscribe = store.subscribe(() => { })
  }

  componentWillUnmount() {
    this.unsubscribe()
  }

  render() {
  console.log('map')
  console.log('markers', this.props.markers)
    return (
      <Container>
        <MapView
          style={styles.map}
          region={this.props.region}
          onRegionChange={this.props.onRegionChange}
        >
          {
            this.props.markers.map(marker => {
              return (
                <MapView.Marker
                  coordinate={{ latitude: marker.latitude, longitude: marker.longitude }}
                  title={marker.name}
                />
              )
            })}
        </MapView>
        <Button
          small
          icon
          style={mapStyle.toggleMenuButton}
          onPress={() => this.props.onToggleMenuClick()}>
          <Icon name="sliders" size={20} color="#FFFFFF" />
        </Button>
      </Container>
    )
  }
}

Map.contextTypes = {
  store: React.PropTypes.object
}

Map.propTypes = {
  region: React.PropTypes.shape({
    latitude: React.PropTypes.number,
    longitude: React.PropTypes.number,
    latitudeDelta: React.PropTypes.number,
    longitudeDelta: React.PropTypes.number
  }).isRequired,
  onRegionChange: React.PropTypes.func.isRequired,
  onToggleMenuClick: React.PropTypes.func.isRequired,
  markers: React.PropTypes.array
}

export default connect(
  mapStateToProps,
  mapDispatchToProps
)(Map)

const styles = StyleSheet.create({
  map: {
    ...StyleSheet.absoluteFillObject,
    zIndex: -1
  }
})

map.reducer

// @flow
import initialState from '../../config/config'
import { UPDATE_REGION } from './map.action'

const map = (
  initialMapState: [] = initialState.get('map'),
  action: Object): string => {
  switch (action.type) {
    case UPDATE_REGION: {
      if (action.payload) {
        return initialMapState.set('region', {
          latitudeDelta: 0.0922,
          longitudeDelta: 0.0421,
          latitude: action.payload.region.latitude,
          longitude: action.payload.region.longitude
        })
      } else {
        return initialMapState
      }
    }
    default:
      return initialMapState
  }
}

export default map
4

1 回答 1

1

Use onRegionChangeComplete

<MapView
  style={styles.map}
  region={this.props.region}
  onRegionChangeComplete={this.props.onRegionChange}>

The examples in the docs use onRegionChange. So maybe onRegionChange works for most. Maybe it works without redux. Anyway easy fix is onRegionChangeComplete if getting the same issue.

于 2017-06-04T00:03:35.797 回答