【问题标题】:React Native Maps - Callout don't show image insideReact Native Maps - 标注不显示内部图像
【发布时间】:2019-06-28 14:31:42
【问题描述】:

当我尝试在 React Native Maps Callout 中渲染图像时遇到问题。

图像不渲染。

{this.state.database.map((route, idx) => {
        if (route.Image) {
          return (
            <MapView.Marker
              coordinate={{ latitude: Number.parseFloat(route.Latitude), longitude: Number.parseFloat(route.Longitude) }}
              key={idx}
              image={img_pin}
              ref={(ref) => this.marker = ref}
            >
              <MapView.Callout tooltip style={{ flex: 1, backgroundColor: 'white' }}>
                <View style={{ flexDirection: 'row' }}>
                  <View style={{ flex: 1 }}>
                    <Text style={styles.textCallout}>Latitude: {route.Latitude}</Text>
                    <Text style={styles.textCallout}>Longitude: {route.Longitude}</Text>
                    <Text style={styles.textCallout}>Status: <Icon name={route.Status === 1 ? 'checkmark-circle' : 'close-circle'} style={{ color: route.Status === 1 ? 'green' : 'red', fontSize: 16 }} /> </Text>
                    <Text style={styles.textCallout}>Velocidade: {route.Speed} km/h</Text>
                  </View>
                  <View style={{ margin: 5 }}>
                    <Image source={{ uri: `data:image/gif;base64,${route.Image}` }} style={{ width: 150, height: 100 }} />
                  </View>
                </View>
              </MapView.Callout>
            </MapView.Marker>
          );
        }
      })}

Image error don't render inside Callout

"react-native": "^0.57.8",
"react-native-maps": "^0.24.2",
 

【问题讨论】:

  • 你是否在 build.gradle 中添加了支持?
  • 支持什么?你能更好地解释一下吗?

标签: react-native react-native-maps


【解决方案1】:

在文本组件中使用图像

<Text> <Image style={{ height: 100, width:100 }} source={{ ... }} resizeMode="cover" /> </Text>

使用 WebView 的另一种解决方法。我认为现在是解决这个问题的正确方法。

<View>
  <WebView style={{ height: 100 , width: 230, }} source={{ uri: ... }} />
</View>

【讨论】:

    【解决方案2】:

    确保您的route.image 是 url 架构的形式,

    示例数据

     source={{uri: 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADMAAAAzCAYAAAA6oTAqAAAAEXRFWHRTb2Z0d2FyZQBwbmdjcnVzaEB1SfMAAABQSURBVGje7dSxCQBACARB+2/ab8BEeQNhFi6WSYzYLYudDQYGBgYGBgYGBgYGBgYGBgZmcvDqYGBgmhivGQYGBgYGBgYGBgYGBgYGBgbmQw+P/eMrC5UTVAAAAABJRU5ErkJggg=='}}
    

    如果正确,

    Android 上的 GIF 和 WebP 支持

    在构建您自己的原生代码时,Android 默认不支持 GIFWebP

    您需要在android/app/build.gradle 中添加一些可选模块,具体取决于您的应用程序的需要。

    build.gradle:

    dependencies {
      // If your app supports Android versions before Ice Cream Sandwich (API level 14)
      implementation 'com.facebook.fresco:animated-base-support:1.10.0'
    
      // For animated GIF support
      implementation 'com.facebook.fresco:animated-gif:1.10.0'
    
      // For WebP support, including animated WebP
      implementation 'com.facebook.fresco:animated-webp:1.10.0'
      implementation 'com.facebook.fresco:webpsupport:1.10.0'
    
      // For WebP support, without animations
      implementation 'com.facebook.fresco:webpsupport:1.10.0'
    }
    

    用法

      <MapView.Callout tooltip={true} style={{ backgroundColor: 'white', height: contentHeight, width: contentWidth }}>
                    <View style={{ flexDirection: 'column' }}>
                      <View style={{ flex: 1 }}>
                        <Text style={styles.textCallout}>Latitude: {route.Latitude}</Text>
                        <Text style={styles.textCallout}>Longitude: {route.Longitude}</Text>
                        <Text style={styles.textCallout}>Status: <Icon name={route.Status === 1 ? 'checkmark-circle' : 'close-circle'} style={{ color: route.Status === 1 ? 'green' : 'red', fontSize: 16 }} /> </Text>
                        <Text style={styles.textCallout}>Velocidade: {route.Speed} km/h</Text>
                      </View>
                     <Image source={{ uri: `data:image/gif;base64,${route.Image}` }} style={{ width: 150, height: 100, resizeMode="cover" }} />
                    </View>
                  </MapView.Callout>
    
    

    【讨论】:

    • 是的...按照您的更改后
    • 这可能很难,但我再次更新了我的答案。你想运行它吗?
    猜你喜欢
    • 2020-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多