简体   繁体   English

一些自定义标记不会出现在 Android react-native-maps 上

[英]Some custom markers do not appear on Android react-native-maps

In our app, we are obtaining the user location when he logs in (click login -> get location -> save location in redux -> navigate to home screen).在我们的应用程序中,我们在用户登录时获取用户位置(单击登录 -> 获取位置 -> 在 redux 中保存位置 -> 导航到主屏幕)。 When the Home component mounts, it should use the location to get data from the area around the user.当 Home 组件挂载时,它应该使用位置从用户周围的区域获取数据。

In iOS, all markers render correctly but in Android devices, not all markers are rendered, only some of them (and oddly enough all of them are image1 or image2).在 iOS 中,所有标记都能正确渲染,但在 Android 设备中,并非所有标记都被渲染,只有其中一些(奇怪的是,所有标记都是 image1 或 image2)。

It is only when we call the getDataFromRegion function again that all markers render correctly.只有当我们再次调用getDataFromRegion函数时,所有标记才能正确呈现。 Any idea of what we are doing wrong?知道我们做错了什么吗?

class Login extends Component {

   handleLogin = (u, p) => {

      getLocation(location => {

         let region = {
            latitude: location.coords.latitude,
            longitude: location.coords.longitude,
            latitudeDelta: 0.06,
            longitudeDelta: 0.06
         }

         /* save location in redux */
         this.props.setLocation(region)
         login(u, p).then(() => _gotoHome())
      })
   }
}

class Home extends Component {

    componentWillMount() {

       if(this.props.location !== undefined) {

           initialRegion = {
               latitude: this.props.location.latitude,
               longitude: this.props.location.longitude,
               latitudeDelta: 0.06,
               longitudeDelta: 0.06
            }
        }
     }

     componentDidMount() {
        /* set data in redux */   
           this.getDataFromRegion(initialRegion)
     }

     render() {
        this.props.data.map((data, index) => data.visible ? <CustomMarker key={index} data={data}/> : null)
        }
     }


class CustomMarker extends Component {
     render() {
         const {data} = this.props
         const coords = { latitude: data.lat, longitude: data.lng }

         return (

             <MapView.Marker
                 coordinate={coords}
                 onLoad={() => this.forceUpdate()}
                 tracksViewChanges={Platform.OS === 'android' ? false : true}
             >
                 <Image 
                    onLoad={() => this.forceUpdate()} 
                    source={data.a === '0' ? image1 : image2}
                    style={{width: 60, height: 60}}
                 />
             </MapView.Marker>
         )
     }
}

It seems like removing the Image component and use the MapView.Marker prop image got it working.似乎删除 Image 组件并使用 MapView.Marker 道具图像让它工作。 Also, rendering a dummy MapView.Marker with opacity: 0 inside the MapView solved the problem of appearing the default markers at the first render call.此外,在 MapView 中渲染一个带有opacity: 0的虚拟 MapView.Marker 解决了在第一次渲染调用时出现默认标记的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM