React Native Maps: Markers image doesn't show using Custom Marker in react-native-maps

18,255

Solution 1

Use SVG for this https://github.com/react-native-community/react-native-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>

Solution 2

My problem was solved right now.

I hope your problem will be solved.

This is my clean code:

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>
        );
    }
}

Solution 3

I had the same problem.

When you first load an application, the image does not show, but for later loading, this problem is resolved and show image.

Just enough after the image is loaded call this.forceUpdate()

const defaultEmployeeLogo = require("../../../assets/defualtEmployee.png");

<Image source={defaultEmployeeLogo} onLoad={() => this.forceUpdate()}>
    <Text style={{width:0, height:0}}>{Math.random()}</Text>
</Image>

You can track this:

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

Solution 4

@Mahdi Bashirpour solution works for me. just upgrading above answer.

Other Images stop working if we import 'Image' from 'react-native-svg'

My solution is below.

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>

Use 'Imagesvg' for marker image. It's working for me on android 7 and 8. React native version '0.55.3'

Solution 5

This is another example

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>
    )
  }
}
Share:
18,255
Abdu4
Author by

Abdu4

Updated on June 14, 2022

Comments

  • Abdu4
    Abdu4 almost 2 years

    I'm using react-native-maps but I faced a problem that after a lot of googling without answer makes me ask it here. I'm trying to use Custom Marker for the marker in the map as the following picture

    enter image description here

    • as I searched I found out that needed to use Custom Marker to accomplish the maker's design, then I created a Custom Marker component

      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;
      

    when I use CustomMarker.js class solely it works fine and it shows the image but when I use it as the marker custom view it doesn't show the image

    enter image description here

    I don't know why it can't render the image with Custom Marker in android. and here is my code where I'm using map, markers and custom marker class

    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>
    

    any kind of help would be appreciated.