美文网首页地图iOS备忘录iOS开发之常用技术点
iOS 系统自带API地图开发相关(三)

iOS 系统自带API地图开发相关(三)

作者: 楚简约 | 来源:发表于2017-04-28 16:24 被阅读1468次

MKMapView常用功能

1.重新返回到用户所在位置

有两种方式返回用户所在位置:

方式1,设置用户的跟踪模式
方式2,设置用户所在的区域(确定中心点,确定区域大小)


//方式一
self.mapView.userTrackingMode = MKUserTrackingModeFollow;
[self.mapView setUserTrackingMode:MKUserTrackingModeFollow animated:YES];
//方式二
MKCoordinateRegion region =  MKCoordinateRegionMake(self.mapView.userLocation.location.coordinate, self.mapView.region.span);
[self.mapView setRegion:region animated:YES];
2.点击按钮 + 放大地图显示范围
//设置其区域
CLLocationDegrees latitudeDelta = self.mapView.region.span.latitudeDelta * 0.5;
CLLocationDegrees longitudeDelta = self.mapView.region.span.longitudeDelta * 0.5;
MKCoordinateSpan changeSpan = MKCoordinateSpanMake(latitudeDelta, longitudeDelta);
    
//设置变大
[self.mapView setRegion:MKCoordinateRegionMake(self.mapView.region.center, changeSpan) animated:YES];
3.点击按钮 - 缩小地图显示范围
//设置其区域
//只改变区域大小,不改变中心位置
CLLocationDegrees latitudeDelta = self.mapView.region.span.latitudeDelta * 1.5;
CLLocationDegrees longitudeDelta = self.mapView.region.span.longitudeDelta * 1.5;
MKCoordinateSpan changeSpan = MKCoordinateSpanMake(latitudeDelta, longitudeDelta);
    
//设置变大
[self.mapView setRegion:MKCoordinateRegionMake(self.mapView.region.center, changeSpan) animated:YES];
4.计算两点之间的距离
CLLocation *location1 = [[CLLocation alloc] initWithLatitude:39.6 longitude:116.2];
CLLocation *location2 = [[CLLocation alloc] initWithLatitude:28.5 longitude:118.3];
    
float distance = [location1 distanceFromLocation:location2];
NSLog(@"%f km",distance / 1000);

不过一般计算两点之间距离交给服务器处理,你只需要上传你所在位置经纬度即可

5.开始导航
// 根据两个地标对象进行调用系统导航
- (void)beginNavWithBeginPlacemark:(CLPlacemark *)beginPlacemark andEndPlacemark:(CLPlacemark *)endPlacemark
{
    // 创建起点:根据 CLPlacemark 地标对象创建 MKPlacemark 地标对象
    MKPlacemark *itemP1 = [[MKPlacemark alloc] initWithPlacemark:beginPlacemark];
    MKMapItem *item1 = [[MKMapItem alloc] initWithPlacemark:itemP1];
    
    // 创建终点:根据 CLPlacemark 地标对象创建 MKPlacemark 地标对象
    MKPlacemark *itemP2 = [[MKPlacemark alloc] initWithPlacemark:endPlacemark];
    MKMapItem *item2 = [[MKMapItem alloc] initWithPlacemark:itemP2];
    
    NSDictionary *launchDic = @{
                                // 设置导航模式参数
                                MKLaunchOptionsDirectionsModeKey : MKLaunchOptionsDirectionsModeDriving,
                                // 设置地图类型
                                MKLaunchOptionsMapTypeKey : @(MKMapTypeHybridFlyover),
                                // 设置是否显示交通
                                MKLaunchOptionsShowsTrafficKey : @(YES),
                                
                                };
    // 根据 MKMapItem 数组 和 启动参数字典 来调用系统地图进行导航
    [MKMapItem openMapsWithItems:@[item1, item2] launchOptions:launchDic];
}

这个地方需要注意的是如果采用地理编码和反地理编码进行导航,需要将CLPlacemark转换成MKPlacemark

CLPlacemark *beginMark = placemarks.lastObject;
MKPlacemark * itemP1 = [[MKPlacemark alloc]initWithPlacemark:beginMark];

当然这个的导航是调用苹果系统自带的高德地图(苹果地图)进行的,并不需要做特殊的处理

如果需要下面不同第三方地图地图导航,则需要进行相关配置


导航地图需求.png

导航两种实现方式 (集成第三方SDK、URL跳转第三方应用)

这个地方就先简单讲一下URL跳转第三方应用
这里需要注意几个问题:

1.在我国 出于国家安全考虑 国内所有导航电子地图必须使用国家测绘局制定的加密坐标系统 即将一个真实的经纬度坐标加密成一个不正确的经纬度坐标 即火星坐标) 要考虑到每种地图的坐标系都不同

2.选择框的应用选项,根据你手机上有没有安装这个地图应用,没有就不会出现。(需要)

3.实现跳转第三方应用,首先想到的肯定是配置URL Scheme 和白名单


1.坐标系.png
2.判断手机上有没有安装该地图应用。
- (BOOL)canOpenURL:(NSURL *)url NS_AVAILABLE_IOS(3_0);  
[[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"iosamap://"]]
3.几个常用地图应用的url Scheme:
baidumap //百度地图  
iosamap //高德地图  
comgooglemaps //谷歌地图  
qqmap //腾讯地图  
…. //其他地图省略 

在info.plist里面设置url scheme白名单,不然无法打开对应的应用


scheme.png

百度地图 URL Scheme: baidumap:// 百度地图官方文档链接
高德地图 URL Scheme: iosamap:// 高德地图官方文档链接
谷歌地图 URL Scheme:comgooglemaps:// 谷歌地图官方文档链接
腾讯地图 URL Scheme:qqmap:// 腾讯地图官方文档链接

actionSheet弹框 -- 导航数组

#pragma mark - 导航方法  
- (NSArray *)getInstalledMapAppWithEndLocation:(CLLocationCoordinate2D)endLocation  
{  
    NSMutableArray *maps = [NSMutableArray array];  
      
    //苹果地图  
    NSMutableDictionary *iosMapDic = [NSMutableDictionary dictionary];  
    iosMapDic[@"title"] = @"苹果地图";  
    [maps addObject:iosMapDic];  
      
    //百度地图  
    if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"baidumap://"]]) {  
        NSMutableDictionary *baiduMapDic = [NSMutableDictionary dictionary];  
        baiduMapDic[@"title"] = @"百度地图";  
        NSString *urlString = [[NSString stringWithFormat:@"baidumap://map/direction?origin={{我的位置}}&destination=latlng:%f,%f|name=北京&mode=driving&coord_type=gcj02",endLocation.latitude,endLocation.longitude] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];  
        baiduMapDic[@"url"] = urlString;  
        [maps addObject:baiduMapDic];  
    }  
      
    //高德地图  
    if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"iosamap://"]]) {  
        NSMutableDictionary *gaodeMapDic = [NSMutableDictionary dictionary];  
        gaodeMapDic[@"title"] = @"高德地图";  
        NSString *urlString = [[NSString stringWithFormat:@"iosamap://navi?sourceApplication=%@&backScheme=%@&lat=%f&lon=%f&dev=0&style=2",@"导航功能",@"nav123456",endLocation.latitude,endLocation.longitude] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];  
        gaodeMapDic[@"url"] = urlString;  
        [maps addObject:gaodeMapDic];  
    }  
      
    //谷歌地图  
    if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"comgooglemaps://"]]) {  
        NSMutableDictionary *googleMapDic = [NSMutableDictionary dictionary];  
        googleMapDic[@"title"] = @"谷歌地图";  
        NSString *urlString = [[NSString stringWithFormat:@"comgooglemaps://?x-source=%@&x-success=%@&saddr=&daddr=%f,%f&directionsmode=driving",@"导航测试",@"nav123456",endLocation.latitude, endLocation.longitude] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];  
        googleMapDic[@"url"] = urlString;  
        [maps addObject:googleMapDic];  
    }  
      
    //腾讯地图  
    if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"qqmap://"]]) {  
        NSMutableDictionary *qqMapDic = [NSMutableDictionary dictionary];  
        qqMapDic[@"title"] = @"腾讯地图";  
        NSString *urlString = [[NSString stringWithFormat:@"qqmap://map/routeplan?from=我的位置&type=drive&tocoord=%f,%f&to=终点&coord_type=1&policy=0",endLocation.latitude, endLocation.longitude] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];  
        qqMapDic[@"url"] = urlString;  
        [maps addObject:qqMapDic];  
    }  
      
    return maps;  
}  

ActionSheetDelegate弹框代理方法

#pragma mark ActionSheetDelegate  
-(void)actionSheet:(LCActionSheet *)actionSheet didClickedButtonAtIndex:(NSInteger)buttonIndex  
{  
    if (buttonIndex != -1) {  
        if (buttonIndex == 0) {  
            [self navAppleMap];  
            return;  
        }  
        NSDictionary *dic = self.maps[buttonIndex];  
        NSString *urlString = dic[@"url"];  
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];  
    }  
}  
  
//苹果地图  
- (void)navAppleMap  
{  
    CLLocationCoordinate2D gps = [JZLocationConverter bd09ToWgs84:self.destinationCoordinate2D];  
      
    MKMapItem *currentLoc = [MKMapItem mapItemForCurrentLocation];  
    MKMapItem *toLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:gps addressDictionary:nil]];  
    NSArray *items = @[currentLoc,toLocation];  
    NSDictionary *dic = @{  
                          MKLaunchOptionsDirectionsModeKey : MKLaunchOptionsDirectionsModeDriving,  
                          MKLaunchOptionsMapTypeKey : @(MKMapTypeStandard),  
                          MKLaunchOptionsShowsTrafficKey : @(YES)  
                          };  
      
    [MKMapItem openMapsWithItems:items launchOptions:dic];  
}

iOS 系统自带API地图开发相关(一)
iOS 系统自带API地图开发相关(二)
iOS 系统自带API地图开发相关(三)


第三节到此为止.记录下来同大家分享!!!🙂🙂🙂

我是楚简约,感谢您的阅读,

喜欢就点个赞呗,“❤喜欢”,

鼓励又不花钱,你在看,我就继续写~

非简书用户,可以点右上角的三个“...”,然后"在Safari中打开”,就可以点赞咯~


相关文章

网友评论

本文标题:iOS 系统自带API地图开发相关(三)

本文链接:https://www.haomeiwen.com/subject/eyxazttx.html