[英]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.