最近客户新提了需求,地址字段要能通过第三方的地图进行定位,于是对Android和IOS端进行了调整。
以下是调用地图部分的代码。
android可按照包名来判断app是否存在:
方法:
/*
* check the app is installed
*/
private boolean isAppInstalled(Context context, String packagename) {
PackageInfo packageInfo;
try {
packageInfo = context.getPackageManager().getPackageInfo(packagename, 0);
} catch (PackageManager.NameNotFoundException e) {
packageInfo = null;
e.printStackTrace();
}
if (packageInfo == null) {
//System.out.println("没有安装");
return false;
} else {
//System.out.println("已经安装");
return true;
}
}
这是调用,我的是直接调用启动地图。你可以用原生实现一个操作表让用户选择后启动相应的APP。
if (isAppInstalled(context, "com.autonavi.minimap")) {
url = "amapuri://poi?sourceApplication=ewpower.com&keywords="+address;
showToast("启动高德地图");
}else if (isAppInstalled(context, "com.baidu.BaiduMap")) {
url = "baidumap://map/geocoder?src=openApiDemo&address="+address;
showToast("启动百度地图");
} else {
showToast("检测到您未安装地图APP,无法开始导航,建议您安装最新版的高德地图或百度地图");
return;
}
IOS可用canOpenURL来判断Schema是否存在判断,代码如下:
记得添加 lsapplicationqueriesschemes
if([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"iosamap://"]])
{
url = [[NSString stringWithFormat:@"iosamap://poi?sourceApplication=applicationName&name=%@",address]stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
}
else if([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"baidumap://"]])
{
url = [[NSString stringWithFormat:@"baidumap://map/geocoder?address=%@&src=%@",address,@"ewpower.com"] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
}else
{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil message:@"检测到您未安装地图APP,无法开始导航,建议您安装最新版的高德地图或百度地图" delegate:self cancelButtonTitle:@"知道啦"otherButtonTitles:nil, nil];
[alert show];
return;
} NSURL *schema = [NSURL URLWithString:url];
if ([[UIDevice currentDevice].systemVersion integerValue] >= ) {
//iOS10以后,使用新API [[UIApplication sharedApplication] openURL:schema options:@{} completionHandler:^(BOOL success) { NSLog(@"scheme调用结束"); }];
} else {
//iOS10以前,使用旧API
[[UIApplication sharedApplication] openURL:schema];
}