在开发过程中,可能会有这样的需求:从我们自己的app点击去这里跳转至地图进行导航,此时你的手机上若安装了高德,则跳转至高德地图进行导航,若安装了百度地图,则跳转至百度地图进行导航,若是都没有安装,则跳转至系统地图进行导航。
实现这样一个需求,我们需要三个参数:目的地adress、latitude、longitude。
具体代码实现:
在需要的地方调用gothere方法
附:方便粘贴
-(BOOL)canOpenUrl:(NSString *)string {
return [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:string]];
}
- (void)gothereWithAddress:(NSString *)address andLat:(NSString *)lat andLon:(NSString *)lon {
if ([self canOpenUrl:@"baidumap://"]) {///跳转百度地图
NSString *urlString = [[NSString stringWithFormat:@"baidumap://map/direction?origin={{我的位置}}&destination=latlng:%@,%@|name=%@&mode=driving&coord_type=bd09ll",lat, lon,address] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
return;
}else if ([self canOpenUrl:@"iosamap://"]) {///跳转高德地图
NSString *urlString = [[NSString stringWithFormat:@"iosamap://navi?sourceApplication=%@&backScheme=%@&lat=%@&lon=%@&dev=0&style=2",@"神骑出行",@"TrunkHelper",lat, lon] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
return;
}else{////跳转系统地图
CLLocationCoordinate2D loc = CLLocationCoordinate2DMake([lat doubleValue], [lon doubleValue]);
MKMapItem *currentLocation = [MKMapItem mapItemForCurrentLocation];
MKMapItem *toLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:loc addressDictionary:nil]];
[MKMapItem openMapsWithItems:@[currentLocation, toLocation]
launchOptions:@{MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving,
MKLaunchOptionsShowsTrafficKey: [NSNumber numberWithBool:YES]}];
return;
}
}