2017-10-08 115 views
0

我想在组件第一次渲染后立即调用地图上的方法。在这种情况下,this.map未定义,但不应该由ref设置?如何在componentDidMount方法中获得对MapView的引用?在初始渲染后反应本地地图的动画

import React from 'react'; 
import { MapView } from 'expo'; 

export default class Map extends React.Component { 
    componentDidMount() { 
    this.map.animateToBearing(25) 
    } 

    render() { 
    return (
     <MapView 
     ref={ref => { this.map = ref }} 
     style={{ flex: 1 }} 
     mapType="satellite" 
     initialRegion={{ 
      latitude: 39.2741004, 
      longitude: -76.6502307, 
      latitudeDelta: 0.002, 
      longitudeDelta: 0.001, 
     }} 
     /> 
    ); 
    } 
} 

回答

1

望着这Github issue,你可能需要使用onLayout而不是componentDidMount

例如:

<MapView 
    ref={ref => { this.map = ref }} 
    onLayout={() => this.map.animateToBearing(25)} 
    .... 
/> 
+0

我仍然得到一个错误_this2.map.animateToBearing不是一个函数 – pjb3

+0

@ pjb3如果你从尝试'MapView' ['制作的Airbnb /反应,native- maps'](https://github.com/airbnb/react-native-maps)?也许'expo'捆绑的'MapView'是旧版本 – Bill