简体   繁体   English

如何以这样一种方式创建地图意图,即如果地图应用程序未安装在用户的设备上,那么它应该在浏览器中打开地图?

[英]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.我正在创建一个应用程序,用户可以在其中点击给定地址(在TextView表单中),它将用户带到 Google 地图应用程序中的确切位置。 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.这里的问题是,如果谷歌地图应用程序没有安装在用户的设备中,应用程序在点击location视图时会崩溃。 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.您要做的是检查包管理器是否实际安装了所需的应用程序,如果是 - 使用您在 Google 地图应用程序中提供的值进行推广。 If not, redirect the user to the play store to install it.如果没有,请将用户重定向到 Play 商店进行安装。

  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.注意这里也要替换经纬度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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