googlemaps / react-native-navigation-sdk

Apache License 2.0
25 stars 4 forks source link

NavigationController affects all instances of the map #234

Closed nexttrack07 closed 1 month ago

nexttrack07 commented 2 months ago

SDK version: 0.4.3

Using the navigationController to set destinations sets the destinations on all the instances of the map. This can easily be reproduces in the example app as well.

const addMarkersToMap = useCallback(() => {
    mapViewController
      ?.isMyLocationEnabled()
      .then(() => {
        console.log('My Location Enabled');
      })
      .catch((e: any) => {
        console.log('My Location Disabled', e);
      });
    const addMarkerCalls = LatLngs.map(latLng => {
      return mapViewController?.addMarker({
        position: latLng,
        visible: true,
      });
    });

    Promise.all(addMarkerCalls).then(markers => {
      console.log('All markers added');
      navigationController.setDestinations(
        LatLngs.map(data => ({ position: data }))
      );

      const nonEmptyMarkers = markers.filter(Boolean) as Marker[];

      setAppointmentMarkers(nonEmptyMarkers);
    });
  }, [mapViewController, navigationController]);

  const onNavigationReady = useCallback(() => {
    console.log('onNavigationReady');

    addMarkersToMap();
  }, [addMarkersToMap]);

This above code is in the NavigationScreen component in the example app. Our production app has something similar. Now when you navigate to the MultipleMaps screen you can see the destinations set in those maps too however you don't see the markers (because that is using mapViewController and not navigationController).

You can also reproduce this by setting a destination in map1 in the MultipleMaps screen and then noticing that the same destination is set in map2 as well.

We have two separate instances of the map in our application and we need them to work independently of each other without any interference between the two.

Screenshot 2024-08-23 at 12 25 23 PM Screenshot 2024-08-23 at 12 26 00 PM
nexttrack07 commented 2 months ago

I tried using navigationController.cleanup() to see if that would help. But that made all the maps get stuck in Europe again. Using the cleanup method on any map affected all the other maps. Any guidance on how to use it correctly would be appreciated.

akilakumarasamy commented 2 months ago

This is blocking us from upgrade because we have 2 map instances one to show route overview and another for navigation. With this issue, the Navigation screen is still showing the route overview. Is there a way to make the instances independent of each other.

jokerttu commented 2 months ago

This is blocking us from upgrade because we have 2 map instances one to show route overview and another for navigation. With this issue, the Navigation screen is still showing the route overview. Is there a way to make the instances independent of each other.

@akilakumarasamy

If I understood your problem right, when using NavigationView, the active navigation session is inherently bound to all views that are using the NavigationView. This is expected behaviour of the NavigationView. This means that if you have multiple map instances, such as one for route overview and another for active navigation, they will both reflect the state of the active navigation session.

However, you can manage these views independently by controlling their navigation UI behavior. After initializing navigation, you can call NavigationViewController's setNavigationUIEnabled(false) method on the view that should not show the active navigation UI. This will disable the automatic updates related to the navigation session for that particular view, allowing it to remain as a route overview while the other instance handles active navigation.

nexttrack07 commented 1 month ago

Hey @jokerttu - In our case, we have the Route overview page which has the map and then a navigation page which has another map instance.

In the scenario that you're describing, if we disabled the auto updates on the Route overview page then the Route overview page will not reflect any (navigationController) actions taking place in the navigation page.

However, the navigation page will still receive (navigationController) actions that take place in the Route overview page.

We want the pages to be completely independent of each other. Is this possible?

Note: I did set the navigationViewController.setNavigationUIEnabled(false) on the Route overview page and it still caused the above issue. Once we visited the Route page and destinations are set on the Route page, the same destinations appeared on the navigation page.

jokerttu commented 1 month ago

I've created a new feature issue (#254) that should address and resolve this problem. Please follow the new issue for updates, and we’ll close this once the new feature is implemented.