React-Native-Maps在加载后就适合坐标

Ark*_*kon 3 javascript android google-maps react-native react-native-maps

GitHub上的react-native-maps回购示例中提供的示例显示了一个按钮,用于执行一项功能,以设置考虑标记列表的适当缩放:

  fitAllMarkers() {
    this.map.fitToCoordinates(MARKERS, {
      edgePadding: DEFAULT_PADDING,
      animated: true,
    });
}
Run Code Online (Sandbox Code Playgroud)

https://github.com/airbnb/react-native-maps/blob/master/docs/mapview.md

给定已经初始化的标记数组,是否可以使用合适的合适度来初始化地图?

尝试在上设置合适的位置时componentDidMount,我收到:

Error using new LatLntBounds(LatLngBounds, int): Map size can't be 0. Most likely, layout has not yet occured for the map view

调用我的fitAllMarkers()函数肯定还为时过早。onLoad映射初始化后,是否有可以立即触发的功能?

Kan*_*tic 5

fitToCoordinates does not use markers it uses an array of latitude and longitude objects (there is a specific method for that fitToSuppliedMarkers). An important thing to make it work is to give a reference to the internal MapView, to get the reference to the map.

class Map extends Component {

    constructor() {
        super()
        this.mapRef = null;
    }

    render() {
      return    <MapView style={{flex: 1}}
                    ref={(ref) => { this.mapRef = ref }}
                    onLayout = {() => this.mapRef.fitToCoordinates(this.props.myLatLongs, { edgePadding: { top: 10, right: 10, bottom: 10, left: 10 }, animated: false })}>
                    <Polyline coordinates={this.props.myLatLongs} strokeWidth={4} strokeColor="#2962FF" /> 
                </MapView>
    }
}
Run Code Online (Sandbox Code Playgroud)

I leave the code here for future people that arrive to this place. this is duplicated of my answer also in the issue of the repo https://github.com/airbnb/react-native-maps/issues/1003 .

另外到达这里的任何人,请参阅mapview文档中的更新:https : //github.com/airbnb/react-native-maps/blob/master/docs/mapview.md