lundi 4 janvier 2021

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.

Aucun commentaire:

Enregistrer un commentaire