【问题标题】:react-native-maps markers not displayed on mapreact-native-maps 标记未显示在地图上
【发布时间】:2017-06-02 04:25:47
【问题描述】:

我的标记不会在地图上呈现。我最初有一个无状态的功能组件,但改为使用this.forceUpdate() 的类,它仍然无法正常工作。我究竟做错了什么?我已经在 Android 和 iOS 真实设备上进行了测试。地图显示但不显示标记。会不会是地图出现在标记之上,比如zIndex 问题?我在那里硬编码了一个marker,以确保问题不在于我的props.markers

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(() => { })
    this.forceUpdate()
  }

  componentWillUnmount() {
    this.unsubscribe()
  }

  render() {
  console.log('map')
  console.log('markers', this.props.markers)
    return (
      <Container>
        <MapView
          style={styles.map}
          region={{
            latitude: this.props.region.latitude,
            longitude: this.props.region.longitude,
            latitudeDelta: this.props.region.latitudeDelta,
            longitudeDelta: this.props.region.longitudeDelta,
          }}
        >
          {
            this.props.markers.map(marker => {
              return (
                <MapView.Marker
                  onLoad={() => this.forceUpdate()}
                  coordinate={{ latitude: marker.latitude, longitude: marker.longitude }}
                  title={marker.name}
                />
              )
            })}

          <MapView.Marker
            coordinate={{ latitude: 174.7666099, longitude: -36.8457991 }}
            title={"title"}
            description={"description"}
            onLoad={() => this.forceUpdate()}
            key={1}
          />
        </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
  }
})

【问题讨论】:

    标签: javascript react-native redux react-native-ios react-native-maps


    【解决方案1】:

    您更改了纬度和经度。

    latitude: 174.7666099, longitude: -36.8457991 不是地图上的一个点。

    latitude: -36.8457991, longitude: 174.7666099 是地图上的一个点。

    【讨论】:

    • 谢谢。我也犯了同样的错误,没有注意到。
    猜你喜欢
    • 1970-01-01
    • 2020-02-22
    • 1970-01-01
    • 2019-08-22
    • 2021-11-25
    • 2019-02-04
    • 2019-04-27
    • 1970-01-01
    • 2021-11-19
    相关资源
    最近更新 更多