5

I am trying to launch maps using the following code.

public static void navigate(Context context, double lat, double lon) {
        String locationQuery = lat + "," + lon;
        Uri gmmIntentUri = Uri.parse("google.navigation:q=" + locationQuery);
        Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);
        mapIntent.setPackage("com.google.android.apps.maps");
        context.startActivity(mapIntent);
    }

But in certain cases I am getting no activity found to handle intent crash. What I am doing wrong here.

user1002448
  • 425
  • 8
  • 22
  • you say in certain cases, do you mean different devices or different lat/lon values? – Zun Apr 18 '18 at 11:27
  • Its random. Can't determine if its for certain lat lon or for devices. Its coming for different devices – user1002448 Apr 18 '18 at 11:30
  • You are forcing the com.google.android.apps.maps package, so it might be because the user does not have this app installed, or the link you're generating is broken and can not be understood by google maps (or any other apps on the device) – Zun Apr 18 '18 at 11:31
  • So if maps is not installed how do we handle the intent – user1002448 Apr 18 '18 at 11:37
  • Can't you open the map through the users' web browser? – Zun Apr 18 '18 at 11:48

1 Answers1

7

I think you should check is this package installed like this

private boolean isPackageInstalled(String packagename, PackageManager packageManager) {
    try {
        packageManager.getPackageInfo(packagename, 0);
        return true;
    } catch (NameNotFoundException e) {
        return false;
    }
}

And if it isn't then open web version. Or check out Google Maps docs. AFAIK there is a way Maps can handle it.

Or you can check is app available this way:

if (mapIntent.resolveActivity(getPackageManager()) != null) {
    ...
}

If App is not installed you can:

1.Redirect user to Google Play

2.Open map in browser.

String uri = "http://maps.google.com/maps?saddr=" + sourceLatitude + "," + sourceLongitude + "&daddr=" + destinationLatitude + "," + destinationLongitude;
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
startActivity(intent);
Anton Kazakov
  • 2,740
  • 3
  • 23
  • 34