Search code examples
react-nativemapsreact-native-maps

React Native Maps - takeSnapshot not capturing markers


react-native: https://github.com/expo/react-native/archive/sdk-42.0.0.tar.gz react: 16.13.1 react-native-maps: 0.28.0

I want to get markers as a part of the snapshot. When we use takeSnapshot method all markers are ignored.

const snapshot = this.viewRefTest.takeSnapshot({
  format: 'png', // image formats: 'png', 'jpg' (default: 'png')
  quality: 0.5, // image quality: 0..1 (only relevant for jpg, default: 1)
  result: 'file', // result types: 'file', 'base64' (default: 'file')
});

<MapView
  ref={(viewRefTest) => {
    this.viewRefTest = viewRefTest;
  }}
  showsUserLocation={true}
  followUserLocation={true}>
  <MapView.Marker coordinate={item.location}>
    <Image
      style={{ width: 30, height: 30 }}
      source={require('../../assets/images/trophy.png')}
    />
    <Callout style={{ width: 250, flexDirection: 'row', alignItems: 'center' }}>
      <Text>$23</Text>
      <View>
        <Text style={{ fontSize: 12 }}>Custom Text!</Text>
      </View>
    </Callout>
  </MapView.Marker>
</MapView>;

Please let me know the possibility of this.


Solution

  • After too many tries & combinations of adding delays, height/width, I was not able to capture custom markers using takeSnapshot method.

    As a workaround, I have used captureRef method of react-native-view-shot

    https://github.com/gre/react-native-view-shot

    const uri = await captureRef(this.viewRefTest, {
                format: "png",
                quality: 1
            })
    
    <MapView
      ref={(viewRefTest) => {
        this.viewRefTest = viewRefTest;
      }}
      showsUserLocation={true}
      followUserLocation={true}>
      <MapView.Marker coordinate={item.location}>
        <Image
          style={{ width: 30, height: 30 }}
          source={require('../../assets/images/trophy.png')}
        />
        <Callout style={{ width: 250, flexDirection: 'row', alignItems: 'center' }}>
          <Text>$23</Text>
          <View>
            <Text style={{ fontSize: 12 }}>Custom Text!</Text>
          </View>
        </Callout>
      </MapView.Marker>
    </MapView>
    

    CaptureRef Returns a Promise of the image URI. It helps capture a React Native view to an image. We can mention height, width, quality & format for the captured image.