簡體   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?

我正在創建一個應用程序,用戶可以在其中點擊給定地址(在TextView表單中),它將用戶帶到 Google 地圖應用程序中的確切位置。 我使用這段代碼來實現這一點:

// 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);


            }

        });

這里的問題是,如果谷歌地圖應用程序沒有安裝在用戶的設備中,應用程序在點擊location視圖時會崩潰。 所以在這種情況下,我想做的不是應用程序崩潰,而是應用程序應該打開任何瀏覽器並在那里加載位置(在谷歌地圖網站中),或者它應該顯示一條吐司消息,要求用戶安裝谷歌地圖應用。 我怎樣才能做到這一點?

如果可能,請分享兩者的解決方案。

您要做的是檢查包管理器是否實際安裝了所需的應用程序,如果是 - 使用您在 Google 地圖應用程序中提供的值進行推廣。 如果沒有,請將用戶重定向到 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;
}

發送它

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

所以

boolean isAppInstalled = isAppInstalledOrNot(uri);

然后

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);
}

如果您想在瀏覽器或任何其他可以處理的應用程序中打開它

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

注意這里也要替換經緯度。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM