
地圖
從自己的APP跳轉(zhuǎn)到用戶本地的APP進行導(dǎo)航。首先,要先查看用戶都安裝了哪些地圖類APP.
下面分3種情況進行分析:
1、用戶沒有安裝第三方的地圖,只有蘋果自帶的地圖應(yīng)用。
2、用戶安裝一款第三方地圖應(yīng)用。
3、用戶安裝了超過一款地圖應(yīng)用。
遇到第1、2情況直接跳轉(zhuǎn)應(yīng)用。第三種情況,需要彈出選項,讓用戶自主選擇。
代碼:
//查看線路
- (void)clickLine:(UIButton *)sender{
NSMutableArray *mapArr = [NSMutableArray arrayWithCapacity:0];
if([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"baidumap://"]]){
[mapArr addObject:@"百度地圖"];
}
if([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"iosamap://"]]){
[mapArr addObject:@"高德地圖"];
}
if([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"qqmap://"]]){
[mapArr addObject:@"騰訊地圖"];
}
if (mapArr.count == 1) {
[self JumpToMap:mapArr[0]];
}else if(mapArr.count > 0){
UIAlertController *mapAlert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
for (NSString *mapName in mapArr) {
UIAlertAction *Action = [UIAlertAction actionWithTitle:mapName style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
[self JumpToMap:action.title];
}];
[mapAlert addAction:Action];
}
//取消
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
}];
[mapAlert addAction:cancelAction];
[self presentViewController:mapAlert animated:YES completion:^{
}];
}else{
//使用自帶地圖
[self JumpToMap:@"蘋果地圖"];
}
}
分流函數(shù):
//選擇地圖
- (void)JumpToMap:(NSString *)mapName{
if ([mapName isEqualToString:@"蘋果地圖"]) {
[self appleMap];
}else if ([mapName isEqualToString:@"百度地圖"]){
[self BaiduMap];
}else if ([mapName isEqualToString:@"高德地圖"]){
[self iosMap];
}else if ([mapName isEqualToString:@"騰訊地圖"]){
[self qqMap];
}
}
備注:如果兼容ios10以下的應(yīng)用,跳轉(zhuǎn)請做好適配,ios10以下系統(tǒng)請使用:
- openURL:(NSURL*)url
百度地圖
//百度地圖
- (void)BaiduMap{
float shopLat = 百度坐標(biāo);
float shoplng = 百度坐標(biāo);
NSString *urlString = [NSString stringWithFormat:@"baidumap://map/direction?origin=latlng:%f,%f|name:我的位置&mode=transit&coord_type= bd09ll",self.userLocation.location.coordinate.latitude, self.userLocation.location.coordinate.longitude];
if (shopLat != 0 && shoplng != 0) {
urlString = [NSString stringWithFormat:@"%@&destination=latlng:%f,%f|name:%@", urlString, shopLat, shoplng, @"目標(biāo)地址,你可以自行替換"];
}else{
urlString = [NSString stringWithFormat:@"%@&destination=%@|name:%@",urlString, _orderModel.addressStr,_orderModel.addressStr];
}
urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString] options:@{} completionHandler:^(BOOL success) {
}];
}
高德地圖
//高德地圖
- (void)iosMap{
CLLocationCoordinate2D gcj02Coord = CLLocationCoordinate2DMake(百度坐標(biāo), 百度坐標(biāo));
float shopLat = gcj02Coord.latitude;
float shoplng = gcj02Coord.longitude;
NSString *urlString = [NSString stringWithFormat:@"iosamap://path?sourceApplication=jikexiue&backScheme=jkxe&slat=%f&slon=%f&sname=我的位置&dev=1&t=1",self.userLocation.location.coordinate.latitude, self.userLocation.location.coordinate.longitude];
if (shopLat != 0 && shoplng != 0) {
urlString = [NSString stringWithFormat:@"%@&dlat=%f&dlon=%f&dname=%@", urlString, shopLat, shoplng ,_orderModel.addressStr];
}else{
urlString = [NSString stringWithFormat:@"%@&dname=%@",urlString, _orderModel.addressStr];
}
urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString] options:@{} completionHandler:^(BOOL success) {
}];
}
騰訊地圖
//騰訊地圖
- (void)qqMap{
CLLocationCoordinate2D gcj02Coord = CLLocationCoordinate2DMake(百度坐標(biāo), 百度坐標(biāo));
float shopLat = gcj02Coord.latitude;
float shoplng = gcj02Coord.longitude;
NSString *urlString = [NSString stringWithFormat:@"qqmap://map/routeplan?type=bus&fromcoord=%f,%f&from=我的位置&referer=jikexiu",self.userLocation.location.coordinate.latitude, self.userLocation.location.coordinate.longitude];
urlString = [NSString stringWithFormat:@"%@&tocoord=%f,%f&to=%@",urlString, shopLat, shoplng, _orderModel.addressStr];
urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString] options:@{} completionHandler:^(BOOL success) {
}];
}
蘋果原生地圖
1、添加庫MapKit.framework
2、引入: #import <MapKit/MapKit.h>
//蘋果原生地圖
- (void)appleMap{
CLLocationCoordinate2D desCoordinate = CLLocationCoordinate2DMake(_orderModel.lat, _orderModel.lng);
MKMapItem *currentLocation = [MKMapItem mapItemForCurrentLocation];
currentLocation.name = @"我的位置";
MKMapItem *toLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:desCoordinate addressDictionary:nil]];
toLocation.name = [NSString stringWithFormat:@"%@",_orderModel.addressStr];
[MKMapItem openMapsWithItems:@[currentLocation, toLocation]
launchOptions:@{MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeTransit,MKLaunchOptionsShowsTrafficKey: [NSNumber numberWithBool:YES]}];
}