【问题标题】:React Native Maps: Markers image doesn't show using Custom Marker in react-native-mapsReact Native Maps:标记图像不显示在 react-native-maps 中使用自定义标记
【发布时间】:2018-08-19 07:07:47
【问题描述】:

我正在使用react-native-maps,但我遇到了一个问题,经过大量谷歌搜索而没有答案让我在这里问它。 我正在尝试使用自定义标记作为地图中的标记,如下图所示

  • 我在搜索中发现需要使用自定义标记来完成制造商的设计,然后我创建了一个自定义标记组件

    import React, { Component } from "react";
    import { View } from "react-native";
    import {
    Text,
    Left,
    Right,
    Thumbnail,
    } from "native-base";
    const defaultEmployeeLogo = require("../../../assets/defualtEmployee.png");
    class CustomMarker extends Component {
    render() {
        return (
        <View style={{ flexDirection: 'row', width: 140, height: 60, 
          borderRadius: 70, backgroundColor: 'orange' }}>
            <Left>
                <Thumbnail source={defaultEmployeeLogo} />
            </Left>
            <Right>
                <Text style={{
                    color: '#fef',
                    fontSize: 13,
                    paddingBottom: 2,
                    fontFamily: 'Roboto',
                    alignItems: 'center',
                    paddingRight: 10
                }}>Mohammad</Text>
            </Right></View >);
       }
    }
    export default CustomMarker;
    

当我单独使用 CustomMarker.js 类时,它可以正常工作并显示图像,但是当我将其用作标记自定义视图时,它不会显示图像

我不知道为什么它不能在 android 中使用 Custom Marker 渲染图像。 这是我使用地图、标记和自定义标记类的代码

return (
  <View style={styles.map_container}>
    <MapView
      style={styles.map}
      customMapStyle={customrMapStyle}
      region={{
        latitude: this.state.region.latitude,
        longitude: this.state.region.longitude,
        latitudeDelta: 0.4,
        longitudeDelta: 0.41,
      }} >
      {
        coordinationData.map(function (marker, i) {

          let lat = marker.latLang.latitude;
          let lang = marker.latLang.longitude;
           <MapView.Marker
            key={i}
            coordinate={
              {
                latitude: lat,
                longitude: lang,
                latitudeDelta: 0.4,
                longitudeDelta: 0.41
              }
            }
            title={marker.title}
            description={marker.description}

          >
            <CustomMarker />
          </MapView.Marker>
        })}
    </MapView>
  </View>

我们将不胜感激。

【问题讨论】:

    标签: android google-maps react-native react-native-maps


    【解决方案1】:

    将此https://github.com/react-native-community/react-native-svg 使用SVG

    <Marker
        coordinate={{
            longitude: lang,
            latitude: lat,
        }}
    >
        <View style={{
            flexDirection: 'row', width: 100, height: 30,
            backgroundColor: 'orange'
        }}>
            <Svg
                width={40} height={30}>
                <Image
                    href={url}
                    width={40}
                    height={30}
                />
            </Svg>
            <View
                style={{
                    flexDirection: 'column'
    
                }}
            >
                <Text
                    style={{
                        marginLeft: 2,
                        fontSize: 9,
                        color: '#ffffff',
                        fontWeight: 'bold',
                        textDecorationLine: 'underline'
                    }}
                >{marker.title}</Text>
                <Text
                    style={{
                        marginLeft: 2,
                        fontSize: 9,
                        color: '#ffffff',
                        fontWeight: 'bold',
                        textDecorationLine: 'underline'
                    }}
                >{marker.description}</Text>
            </View>
        </View>
    </Marker>
    

    【讨论】:

      【解决方案2】:

      我的问题现在已经解决了。

      希望你的问题能得到解决。

      这是我的干净代码:

      import React, {Component} from 'react';
      import {ImageBackground, Text} from 'react-native';
      import {Marker} from 'react-native-maps';
      
      export default class CustomMarker extends Component {
          state = {
              initialRender: true
          }
      
          render() {
              return (
                  <Marker
                    //...
                  >
                      <ImageBackground
                          source={require('../assets/cluster3_mobile.png')}>
      
                          // *** These lines are very important ***
                          onLoad={() => this.forceUpdate()}
                          onLayout={() => this.setState({initialRender: false})}
                          key={`${this.state.initialRender}`}
                          >
                          
      
                          // **** This line is very very important ****
                          <Text style={{width: 0, height: 0}}>{Math.random()}</Text>
      
                      </ImageBackground>
                  </Marker>
              );
          }
      }
      

      【讨论】:

      • 我认为您必须为 ImageBackgrund 添​​加样式,至少在 android 中运行会显示此错误。 @Mahdi Bashirpour
      • 不使用native-base,而是直接测试我的代码。也许这是个问题。或者在地图以外的地方测试组件代码,看看它是否在那里工作@Abdu4
      • 如果整个事情都没有地图,那么很可能带有组件native-base的地图有问题@Abdu4
      • 对不起,这不是问题,我认为问题出在 Android 上。@Mahdi Bashirpour
      • 所以请完全使用我的代码。请放心,这将是真的,因为我已经尝试过了@Abdu4
      【解决方案3】:

      我遇到了同样的问题。

      第一次加载应用程序时,图像不显示,但稍后加载时,此问题已解决并显示图像。

      图片加载完毕后调用this.forceUpdate()

      const defaultEmployeeLogo = require("../../../assets/defualtEmployee.png");
      
      <Image source={defaultEmployeeLogo} onLoad={() => this.forceUpdate()}>
          <Text style={{width:0, height:0}}>{Math.random()}</Text>
      </Image>
      

      你可以跟踪这个:

      https://github.com/react-community/react-native-maps/issues/924

      【讨论】:

      • 我既不是从 Web 服务器也不是从 AsynStoage 加载图像,它只是在我的本地项目中。@Mahdi Bashirpour
      • 没有。您不会从网络上获取图像。您的图像是本地的。 const defaultEmployeeLogo = require("../../../assets/defualtEmployee.png");
      • 一点都不重要。请在加载图像或屏幕时尝试此this.forceUpdate()
      • 除此之外,如果我在 Marker 中使用图像作为标签,它可以正常工作,但我需要在我的自定义视图中使用它作为标记,因为我需要使用带有自定义视图的文本和 iamge 的长边.
      【解决方案4】:

      @Mahdi Bashirpour 解决方案对我有用。只是升级上面的答案。

      如果我们从 'react-native-svg' 导入 'Image',其他图像将停止工作

      我的解决方案如下。

      import {Image} from 'react-native';   // for other images in our render method.
      import { Image as Imagesvg } from 'react-native-svg';
      
      <Marker
         coordinate={marker.latlng}
         title={marker.title}
       >
      
      <View style={{ height: 90, width: 90, backgroundColor: 'orange' }}>
            <Svg width={90} height={90}}>
              <Imagesvg href={marker_g} width={90} height={90} />  // Local Images
             <Imagesvg href={{uri:url}} width={90} height={90} />   //Server images
          </Svg>
      </View>
      </Marker>
      

      使用“Imagesvg”作为标记图像。 它适用于我在 android 7 和 8 上。React 本机版本 '0.55.3'

      【讨论】:

        【解决方案5】:

        这是另一个例子

        class PinMarker extends Component {
          state = {
            initialRender: true
          }
          render() {
            return (
              <MapView.Marker coordinate={coordinate}>
                <Image
                  source={...}
                  onLayout={() => this.setState({ initialRender: false })}
                  key={`${this.state.initialRender}`}
                />
              </MapView.Marker>
            )
          }
        }
        

        【讨论】:

        • 非常感谢您的回复,但同样的结果,地图加载但图像未显示。
        • 还有其他想法吗? @Mahdi Bashirpour
        • 因为我自己也有同样的问题,所以我现在正在努力解决。我想出了很好的结果@abdu4
        【解决方案6】:

        我也有同样的问题,from github post

        使用 below(MapView , Image) 模式首次加载问题仍然会发生

        <MapView.Marker>
            <Image source={img_marker} />
        </MapView.Marker>
        

        所以采用以下解决方案:技巧是将当前选定的标记ID保存在redux中

        class Map extends React.Component {
        render() {
            let {region, markers , markerClick} = this.props;
            const normalMarkerImage = require('../../images/normal_marker.png');
            const selectedMarkerImage = require('../../images/selected_marker.png');
            return !isObjectEmpty(region) && !isObjectEmpty(markers) ? (
                <MapView
                    style={styles.map}
                    showsUserLocation={true}
                    followUserLocation={true}
                    zoomEnabled={true}
                    region={region}
                    paddingAdjustmentBehavior={'automatic'}
                    showsIndoors={true}
                    showsIndoorLevelPicker={false}
                    showsTraffic={false}
                    toolbarEnabled={false}
                    loadingEnabled={true}
                    showsMyLocationButton={true}>
                    {markers && markers.length > 0
                        ? markers.map((marker, i) => (
                                <MapView.Marker
                                    coordinate={{
                                        longitude: marker.loc.coordinates[0],
                                        latitude: marker.loc.coordinates[1],
                                    }}
                                    key={`marker-${i}`}
                                    style={{height: 20, width: 20}}
                                    onPress={e => markerClick(e, marker)}
                                    image={
                                        this.props.selectedMarker === marker._id
                                            ? selectedMarkerImage
                                            : normalMarkerImage
                                    }
                                />
                          ))
                        : null}
                </MapView>
            ) : null;
          }
        }
        

        组件功能

        markerClick = async (event, data) => {
            this.props.dispatch(
                setMarkerIconInRedux(this.state.popover.currentData._id)
            );
        };
        

        动作

        export const setMarkerIconInRedux = where => {
            return {
                type: 'SET_MARKER_ICON',
                _id: where,
            };
        };
        

        Redux

        export const currentSelectedMarker = (state = {selectedMarker: ''}, action) => 
        {
            if (action.type === 'SET_MARKER_ICON') {
                return {selectedMarker: action._id};
            } else if (action.type === 'REMOVE_MARKER_ICON') {
                return {selectedMarker: ''};
            } else {
                return state;
            }
        };
        

        【讨论】:

          【解决方案7】:

          我通过用 8 位 gamma 整数 Image 替换 16 位 gamma 整数 Image 解决了这个问题。它可以在 Gimp 中完成,一旦导出图像选择 8bpc RGBA。

          【讨论】:

          • 你也可以添加一个例子吗?
          • 这不是关于编码,而是关于替换图像。您的图像应为 8 位 gamma 整数格式。
          【解决方案8】:

          自 2019 年 10 月起,只要将 trackViewChanges 设置为 true,图像就可以显示在标记中。我使用 FastImage,但总体思路是在加载图像后立即将 trackingViewChanges 设置为 true,然后设置为 false,这样就不会出现任何性能问题。

          export const UserMapPin = props => {
            const [trackView, setTrackView] = useState(true);
          
            function changeTrackView() {
              setTrackView(false);
            }
          
            return (
              <Marker
                tracksViewChanges={trackView}
                coordinate={props.coordinates}>
                <View style={styles.imageIconContainer}>
                    <FastImage
                      onLoadEnd={changeTrackView}
                      style={styles.someImageStyle}
                      source={{
                        uri: props.imageURI,
                      }}
                      resizeMode={FastImage.resizeMode.cover}
                    />
                </View>
              </Marker>
            );
          };
          

          【讨论】:

            【解决方案9】:

            就我而言,问题仅在于远程图像,因此我通过 hack 解决了它。

            我只是在文本组件中放置了一个宽度为零的图像版本,在文本组件之外放置了一个版本。突然一切都解决了。

                <Image
                 source={{ uri: "https://cdn.pixabay.com/photo/2015/04/23/22/00/tree-736885__340.jpg" }}
                 style={styles.companyLogoMarker}
                 resizeMode="cover"
                 />
                 <Text style={styles.markerText}>
                    {names?.[0]?.value}
                   <Image
                       source={{ uri: "https://cdn.pixabay.com/photo/2015/04/23/22/00/tree-736885__340.jpg" }}
                       style={{width: 0, height: 0}}
                       resizeMode="cover"
                    />
                 </Text>
            

            截至撰写此答案时,这是 [react native maps][1] 中的一个错误,自 2017 年以来未解决

            【讨论】:

              【解决方案10】:

              &lt;View&gt; 中添加您的自定义标记组件 例如&lt;View&gt; your marker custom view &lt;/View&gt;。也许它会解决你的问题。

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 2020-02-22
                • 2023-03-17
                • 1970-01-01
                • 2019-09-29
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多