Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ANDROID DEVICE: addViewAt: failed to insert view [664] into parent [646] at index 0 The specified child already has a parent. You must call removeView() on the child's parent first. #5043

Closed
kuldipopenxcell opened this issue May 1, 2024 · 4 comments
Labels
bug Something isn't working invalid This doesn't seem right

Comments

@kuldipopenxcell
Copy link

Summary

When i add Polyline inside MapView then it give this error:

addViewAt: failed to insert view [664] into parent [646] at index 0

The specified child already has a parent. You must call removeView() on the child's parent first.

Reproducible sample code

import {Alert, Dimensions, Image, PanResponder, View} from 'react-native';
import React, {useRef, useState} from 'react';
import {styles} from './style';
import MapView, {PROVIDER_GOOGLE, Polyline} from 'react-native-maps';

const MapPage = () => {
  const mapRef = useRef(null);
  const [lines, setLines] = useState([]);
  const [isDrawing, setIsDrawing] = useState(false);
  const [prevCoordinate, setPrevCoordinate] = useState(null);

  const handleTouchStart = () => {
    setIsDrawing(true);
  };

  const handlePanDrag = event => {
    const {coordinate} = event.nativeEvent;
    if (isDrawing && prevCoordinate) {
      setLines([...lines, [prevCoordinate, coordinate]]);
      console.log('lines', [...lines, [prevCoordinate, coordinate]]);
    }
    setPrevCoordinate(coordinate);
  };

  const handleTouchEnd = () => {
    setIsDrawing(false);
    setPrevCoordinate(null);
  };
  return (
    <View style={styles.container}>
      <MapView
        ref={mapRef}
        style={{flex: 1}}
        scrollEnabled={false}
        provider={PROVIDER_GOOGLE}
        initialRegion={{
          latitude: 37.78825,
          longitude: -122.4324,
          latitudeDelta: 0.0922,
          longitudeDelta: 0.0421,
        }}
        onTouchStart={handleTouchStart}
        onPanDrag={handlePanDrag}
        onTouchEnd={handleTouchEnd}>
        {lines.map((line, index) => (
          <Polyline
            key={index}
            coordinates={line}
            strokeColor="#FF0000"
            strokeWidth={3}
            lineDashPattern={[1]}
          />
        ))}
      </MapView>
    </View>
  );
};

export default MapPage;

Steps to reproduce

  1. Install react-native-maps
  2. Add MapView
  3. Add Polyline inside MapView
  4. In android start by dragging in the map
  5. App crashed (Attached screenshot)

Expected result

It should draw a Polylines in MapView

Actual result

It crashes with the android app

React Native Maps Version

1.14.0

What platforms are you seeing the problem on?

Android

React Native Version

0.73.6

What version of Expo are you using?

Not using Expo

Device(s)

Pixel_3a_API_34 Emulator

Additional information

Screenshot 2024-05-01 at 6 20 21 PM

@kuldipopenxcell kuldipopenxcell added the bug Something isn't working label May 1, 2024
@ashrock
Copy link

ashrock commented May 6, 2024

had the same issue. issue happens when Polyline is used on Expo Go for Android simulator. No issue experienced on iOS simulator

@kuldipopenxcell
Copy link
Author

kuldipopenxcell commented May 8, 2024

I found a solution for this for android, here are the steps to follow:

  1. Create a file named react-native.config.js in the root dir. of the project,
  2. Paste below code in the file and build again.
module.exports = {
  project: {
    android: {
      unstable_reactLegacyComponentNames: [
        'AIRGoogleMap',
        'AIRMap',
        'AIRMapMarker',
        'PanoramaView',
        'AIRMapPolyline',
      ],
    },
    ios: { },
  }
};

@kuldipopenxcell
Copy link
Author

@react-native-maps-bot
This is temporary solution, we are looking for the permanent solution in the next version.
Thanks!

@salah-ghanim
Copy link
Collaborator

@kuldipopenxcell this is rather the way to enable react-native-maps with Fabric enabled, please checkout the documentations on the main page or the example file:
https://github.com/react-native-maps/react-native-maps/?tab=readme-ov-file#react-native-configuration-for-fabric--new-architecture

@salah-ghanim salah-ghanim added the invalid This doesn't seem right label May 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working invalid This doesn't seem right
Projects
None yet
Development

No branches or pull requests

3 participants