首先需要判断手机上是否已经安装了对应的地图导航APP
// 判断是否安装指定app
public static bool IsInstallApp(Context context, string app_package)
{
PackageManager pm = context.PackageManager;
bool installed = false;
try
{
pm.GetPackageInfo(PACKAGE_WECHAT, PackageInfoFlags.Activities);
installed = true;
}
catch (Exception e)
{
installed = false;
}
return installed;
}
打开百度地图代码如下
/// <summary>
/// 打开百度地图
/// </summary>
/// <returns></returns>
public static bool OpenBaiduMap()
{
Context context = global::Android.App.Application.Context;
//传入指定应用包名
if (IsInstallApp(context, "com.baidu.BaiduMap"))
{
double longitude = 120.090546;//经度
double latitude = 30.430953;//维度
try
{
//intent = Intent.getIntent("intent://map/direction?origin=latlng:34.264642646862,108.95108518068|name:我家&destination=大雁塔&mode=driving®ion=西安&src=yourCompanyName|yourAppName#Intent;scheme=bdapp;package=com.baidu.BaiduMap;end");
Intent intent = Intent.GetIntent("intent://map/direction?" +
//"origin=latlng:"+"34.264642646862,108.95108518068&" + //起点 此处不传值默认选择当前位置
"destination=latlng:" + latitude + "," + longitude + "|name:我的目的地" + //终点
"&mode=driving&" + //导航路线方式
"region=杭州" + //
"&src=我的App#Intent;scheme=bdapp;package=com.baidu.BaiduMap;end");
intent.SetFlags(ActivityFlags.NewTask);
context.StartActivity(intent); //启动调用
return true;
}
catch (URISyntaxException e)
{
return false;
//Log.e("intent", e.getMessage());
}
}
else
{
//未安装
return false;
}
}
打开高德地图代码如下
/// <summary>
/// 打开高德地图
/// </summary>
/// <returns></returns>
public static bool OpenGaoDeMap()
{
Context context = global::Android.App.Application.Context;
if (IsInstallApp(context, "com.autonavi.minimap"))
{
double longitude = 120.090546;//经度
double latitude = 30.430953;//维度
try
{
Intent intent = Intent.GetIntent("androidamap://navi?sourceApplication=我的App&poiname=我的目的地&lat=" + latitude + "&lon=" + longitude + "&dev=0");
intent.SetFlags(ActivityFlags.NewTask);
context.StartActivity(intent);
return true;
}
catch (URISyntaxException e)
{
return false;
//e.printStackTrace();
}
}
else
{
return false;
}
}
打开腾讯地图代码如下
/// <summary>
/// 打开腾讯地图
/// </summary>
/// <param name="latitude"></param>
/// <param name="longitude"></param>
/// <param name="address"></param>
/// <returns></returns>
public DataResult OpenTententMap(double latitude, double longitude, string address)
{
if (latitude == 0 || longitude == 0)
{
return DataProcess.Error("经纬度数据异常");
}
if (address.IsMissing())
{
address = "我的目的地";
}
Context context = global::Android.App.Application.Context;
//double longitude = 120.090546;//经度
//double latitude = 30.430953;//维度
try
{
//qqmap://map/routeplan?type=drive&from=清华&fromcoord=39.994745,116.247282&to=怡和世家&tocoord=39.867192,116.493187&referer=OB4BZ-D4W3U-B7VVO-4PJWW-6TKDJ-WPB77
StringBuilder driveLine = new StringBuilder("qqmap://map/routeplan?type=drive");
//driveLine.Append("&from=清华");
//driveLine.AppendFormat("&fromcoord=39.994745,116.247282");
driveLine.AppendFormat("&to={0}", address);
driveLine.AppendFormat("&tocoord={0},{1}", latitude, longitude);
driveLine.Append("&referer=OB4BZ-D4W3U-B7VVO-4PJWW-6TKDJ-WPB77");
Intent intent = Intent.GetIntent(driveLine.ToString());
intent.SetFlags(ActivityFlags.NewTask);
context.StartActivity(intent);
return DataProcess.Success();
}
catch (URISyntaxException e)
{
return DataProcess.Error(e.Message);
//e.printStackTrace();
}
}
注意:AndroId需要增加配置参数
打开/Platforms/Android下的AndroidManifest.xml文件
添加以下配置,package节点下需要拉起的App程序包名称,如果不添加,那么无法判断对应app是否已安装
<queries>
<package android:name="com.tencent.mm"/>
<package android:name="com.baidu.BaiduMap"/>
<package android:name="com.autonavi.minimap"/>
<package android:name="com.tencent.map"/>
</queries>