【问题标题】:Cannot get ref to MapView - react-native-maps无法获得对 MapView 的引用 - react-native-maps
【发布时间】:2019-07-15 20:49:08
【问题描述】:

我一直在尝试使用 MapView 上的方法为区域设置动画,为此我需要访问 ref 但它未定义。其他一切工作正常,我只是无法获得 ref 并且任何时候我尝试调用一个方法,例如`

this.map.animateToRegion({
  latitude: 0,
  longitude: 0,
  latitudeDelta: 1,
  longitudeDelta: 1
});

我收到一条错误消息:

无法读取未定义的属性“animateToRegion”

<MapView
  ref={r => this.map = r}
  mapPadding={{ top: 0, left: 0, right: 0, bottom: 400 }}
  provider='google'
  style={{ flex: 1 }}
  region={this.region}
>
  {this.renderBarberMarkers()}
</MapView>

【问题讨论】:

标签: reactjs react-native react-native-maps


【解决方案1】:

如果你使用钩子,这里是你的方式:

在你的功能之上:

const mapView = React.createRef();
const animateMap = () => {
    mapView.current.animateToRegion({ // Takes a region object as parameter
        longitude: 28.97916756570339,
        latitude: 41,
        latitudeDelta: 0.4,
        longitudeDelta: 0.4,
    },1000);
}

给你的 MapView 一个这样的参考:

<MapView provider={PROVIDER_GOOGLE} region={{
       latitude: 37.78825,
       longitude: -122.4324,
       latitudeDelta: 0.015,
       longitudeDelta: 0.0121,
   }}
   ref={mapView}
   style={{flex:1}}>
</MapView>

最后要做的是触发动画,你可以这样做:

<TouchableOpacity onPress={animateMap}><Text>Start</Text></TouchableOpacity>

【讨论】:

  • 这是完美的,我无法弄清楚一切应该去哪里,现在我的地图很漂亮:D
【解决方案2】:

这里是地图视图

<MapView
  ref={(map) => { this.map = map; }}
  showsUserLocation={true}
  showsMyLocationButton={true}
  initialRegion={this.state.region}
/>

那么,你需要调用

//animate to user current location
this.map.animateToRegion(yourRegionObject,1000)

我是这样用的;

//to get user location
getLocation(){

    navigator.geolocation.getCurrentPosition(
        (position) => {
            let latitude = parseFloat(position.coords.latitude);
            let longitude = parseFloat(position.coords.longitude);

            console.log('location position: ', position);

            let region = {
                latitude: latitude,
                longitude: longitude,
                latitudeDelta: 0.0522,
                longitudeDelta: 0.0321,
            };

            this.setState({region: region});

            console.log('position: ', position);
            console.log('this.map: ', this.map);

            //animate to user current location
            this.map.animateToRegion(region,1000)


        },
        (error) => console.log('position error!!!', error),
        {enableHighAccuracy: false, timeout: 3000}
    );
}

【讨论】:

    【解决方案3】:

    你可以使用钩子

    首先导入useRef, useEffect

    import React, {useRef, useEffect} from 'react';
    

    然后

    const mapRef = useRef(null);
    

    添加 MapView 元素:

    <MapView
     provider={PROVIDER_GOOGLE}
     ref={mapRef} />
    

    您可以找到正在使用的 MapRef

    useEffect(() => {
        if (mapRef) {
          console.log(mapRef);
        }
    });
    

    【讨论】:

      【解决方案4】:

      您是否尝试过使用

      React.createRef();
      

      用你的地图?

      我所做的就是像这样将它导出到我的班级之外

      export let mapRef: MapView | null;
      

      还创建了类似的导出函数

      export function animateToRegion(region: Region, duration?: number) {
        if (mapRef) {
          mapRef.animateToRegion(region, duration);
        }
      }
      

      我的 MapView 看起来像这样

          <MapView
       ref={(c) => mapRef = c}
       showsMyLocationButton={false} />
      

      在我的课堂之外,如果它是一个组件,你可以轻松地调用它。

      【讨论】:

      • 我之前尝试过两种解决方案,我无法理解第二种解决方案。我不明白 export let mapRef: MapView | null; 这条线,它是做什么的。它看起来像 TypeScript,如果我错了,请纠正我并且我正在使用 Javascript
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-09-06
      • 2017-10-12
      • 1970-01-01
      • 2017-09-28
      • 2019-09-17
      • 2016-09-14
      • 2022-01-24
      相关资源
      最近更新 更多