简体   繁体   中英

How to create maps intent in such a way that if maps app is not installed on user's device than it should open the maps in a browser?

I'm creating an app where a user can tap in a given address (in a TextView form) and it takes the user to the exact location in the Google maps app. I use this code to achieve this:

// Creates button view which is connected to a view in the XML layout, which gets triggered on touching the view.
        Button btnLoc = findViewById(R.id.location);
        btnLoc.setOnClickListener(new View.OnClickListener()

        {
            @Override
            public void onClick(View v) {

                // Creates an Intent that will load the location of Mycoffee cafe in map app.
                Uri gmmIntentUri = Uri.parse("geo:00.0000,00.0000");
                Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);
                mapIntent.setPackage("com.google.android.apps.maps");
                startActivity(mapIntent);


            }

        });

The problem here is, in case the Google maps app isn't installed in the user's device, the app crashes on tapping the location view. So in such a condition, instead of the app getting crashed, what I want to do is either the app should open any browser and load the location there (in Google maps website) or it should display a toast message asking the user to install the Google Maps app. How can I achieve this?

If possible,please share solution for both.

What you going to do is to check with the packagemanager if the desired app is actually installed, if it is - promote with the values you gave it the Google maps app. If not, redirect the user to the play store to install it.

  private boolean isAppInstalledOrNot(String uri) {
    PackageManager pm = getPackageManager();
    boolean app_installed = false;
    try {
        pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES);
        app_installed = true;
    } catch (PackageManager.NameNotFoundException e) {
        Log.w("TAG","Exception :"+e.getMessage());
    }
    return app_installed;
}

Send it with

Uri uri = Uri.parse("com.google.android.apps.maps");

so

boolean isAppInstalled = isAppInstalledOrNot(uri);

then

if(isAppInstalled){
// send the lat and long and promote the Google Maps app
}else{ 
        Uri uri = Uri.parse("market://details?id=com.google.android.apps.maps");
        Intent goToMarket = new Intent(Intent.ACTION_VIEW, uri);
        Toast.makeText(this, "Google Maps not Installed",
                Toast.LENGTH_SHORT).show();
        startActivity(goToMarket);
}

if you would like to open it in for example browser or any other application that can handle then

Intent openNav= new Intent(Intent.ACTION_VIEW, Uri
    .parse("http://maps.google.com/maps?saddr="
            + Constants.latitude + ","
            + Constants.longitude + "&daddr="
            + latitude + "," + longitude));
      startActivity(openNav);

pay attention to replace the latitude and longitude here aswell.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM