How to fix my linking of Waze App ? on Android

62 views Asked by At

I'm developing an application and there's a button called "Directions" on it. Button opens a navigation app (Maps, Google Maps or Waze). This works on IOS and opens Maps, but on Android it's not the same, when I press the button, a popup is displayed and ask to choose between Google Maps or Waze., If I click on Google Maps, that works, but on Waze nothing is done except to launch the app started.

I'd like to know if it's possible to know after the pop-up which app I've chosen to put a correct URL. Or just know the best way to link to the Waze App.

My app: React Native 0.71.7

It's my code :

import { useCallback } from 'react';
import { Linking } from 'react-native';

import { isIos } from '@core/constants';
import { useLocationTools } from '@hooks/location';

type LanLon = number | string;

interface SitePoint {
  site_latitude?: LanLon;
  site_longitude?: LanLon;
}

export default function useDirectionsRedirect({ data }: { data?: SitePoint }) {
  const { userPosition } = useLocationTools(true);

  return useCallback(async () => {
    const startPoint = {
      latitude: userPosition?.coords?.latitude,
      longitude: userPosition?.coords?.longitude,
    };
    const finishPoint = { latitude: data?.site_latitude, longitude: data?.site_longitude };

    if (isIos) {
      const start = userPosition ? `saddr=${startPoint.latitude}+${startPoint.longitude}&` : '';
      const finish = `daddr=${finishPoint.latitude}+${finishPoint.longitude}`;
      const link = `maps://app?${start}${finish}`;
      const isValid = await Linking.canOpenURL(link);
      if (isValid) {
        await Linking.openURL(link).catch(console.error);
      }
    } else {
      const navLink = `google.navigation:q=${finishPoint.latitude}+${finishPoint.longitude}`;

      const isValidNav = await Linking.canOpenURL(navLink);
      if (isValidNav) {
        await Linking.openURL(navLink).catch(console.error);
      }
    }
  }, [data, userPosition]);
}

Below the picture of the pop-up

enter image description here

Thanks in advance !

0

There are 0 answers