zoukankan      html  css  js  c++  java
  • iOS定位服务与地图开发(5)---使用程序外地图之调用iOS 6苹果地图

    一般,在应用程序中调用程序外地图有2个选择:iOS 6苹果地图和谷歌web地图。

    1、调用iOS 6苹果地图:

    iOS 设备中带有一个地图应用。

    在iOS 6之后,可以在自己的应用程序中调用它,并且可以传递一些参数进行初始化显示。

    实现Demo:

    iOS 6中实现这个功能需要使用Map Kit中的MKPlacemark和MKMapItem两个类。

    - (IBAction)geocodeQuery:(id)sender {
        
        // 从界面文本框获取查询地址字符串
        if (_txtQueryKey.text == nil || [_txtQueryKey.text length] == 0) {
            return ;
        }
        
        CLGeocoder *geocoder = [[CLGeocoder alloc] init];
        
        [geocoder geocodeAddressString:_txtQueryKey.text completionHandler:^(NSArray *placemarks, NSError *error) {
            
            NSLog(@"查询记录数:%i",[placemarks count]);
            
            if ([placemarks count] > 0 ) {
                
                CLPlacemark * placemark = placemarks[0];
                
                CLLocationCoordinate2D coordinate = placemark.location.coordinate;
                NSDictionary *address = placemark.addressDictionary;
                
                // MKPlacemark是地图上的地标类,CLPlacemark是定位使用的地标类
                MKPlacemark *place = [[MKPlacemark alloc] initWithCoordinate:coordinate addressDictionary:address];
                
                // MKMapItem类封装了地图上一个点得信息类
                MKMapItem *mapItem = [[MKMapItem alloc] initWithPlacemark:place];
                
                // 调用 iOS 自带的苹果地图应用,是MKMapItem类的实例方法
                // 参数:NSDictionary类型,包含一些键:
                /*
                 MKLaunchOptionsDirectionsModeKey 设定路线模式,值对应2个:驾车路线和步行路线
                 MKLaunchOptionsShowsTrafficKey 设置显示交通状况
                 MKLaunchOptionsMapSpanKey  设置地图跨度
                 MKLaunchOptionsMapCenterKey  设置地图中心点
                 MKLaunchOptionsMapTypeKey   设置地图类型
                 */
                
                [mapItem openInMapsWithLaunchOptions:nil];
                
                // 关闭键盘
                [_txtQueryKey resignFirstResponder];
            }
        }];
    }

    效果图:

        程序启动后进入页面(起始地点预设为香港)                输入目的地深圳市(点击“查询”按钮)                                       调转到苹果内置地图

    - (IBAction)geocodeQuery:(id)sender {
        
        // 从界面文本框获取查询地址字符串
        if (_txtQueryKey.text == nil || [_txtQueryKey.text length] == 0) {
            return ;
        }
         
        .......
                
                // 调用 iOS 自带的苹果地图应用,是MKMapItem类的实例方法
                // 参数:NSDictionary类型,包含一些键:
                /*
                 MKLaunchOptionsDirectionsModeKey 设定路线模式,值对应2个:驾车路线和步行路线
                 MKLaunchOptionsShowsTrafficKey 设置显示交通状况
                 MKLaunchOptionsMapSpanKey  设置地图跨度
                 MKLaunchOptionsMapCenterKey  设置地图中心点
                 MKLaunchOptionsMapTypeKey   设置地图类型
                 */
                NSDictionary *options = [[NSDictionary alloc] initWithObjectsAndKeys:MKLaunchOptionsDirectionsModeDriving,MKLaunchOptionsDirectionsModeKey, nil];
                [mapItem openInMapsWithLaunchOptions:options];
                
                // 关闭键盘
                [_txtQueryKey resignFirstResponder];
            }
        }];
    }

    如果有多个点需要标注,我们可以使用MKMapItem的类方法:

    + (BOOL)openMapsWithItems:(NSArray *)mapItems launchOptions:(NSDictionary *)launchOptions

    其中mapItmes是标注点的集合

    launchOptions是启动参数

    - (IBAction)geocodeQuery:(id)sender {
        
        // 从界面文本框获取查询地址字符串
        if (_txtQueryKey.text == nil || [_txtQueryKey.text length] == 0) {
            return ;
        }
        
        CLGeocoder *geocoder = [[CLGeocoder alloc] init];
        
        [geocoder geocodeAddressString:_txtQueryKey.text completionHandler:^(NSArray *placemarks, NSError *error) {
            
            NSLog(@"查询记录数:%i",[placemarks count]);
    
            NSMutableArray *array = [NSMutableArray array];
            for (int i = 0; i < [placemarks count]; i++) {
                
                CLPlacemark * placemark = placemarks[i];
                
                CLLocationCoordinate2D coordinate = placemark.location.coordinate;
                NSDictionary *address = placemark.addressDictionary;
                
                // MKPlacemark是地图上的地标类,CLPlacemark是定位使用的地标类
                MKPlacemark *place = [[MKPlacemark alloc] initWithCoordinate:coordinate addressDictionary:address];
                
                // MKMapItem类封装了地图上一个点得信息类
                MKMapItem *mapItem = [[MKMapItem alloc] initWithPlacemark:place];
                
                [array addObject:mapItem];
                
                // 关闭键盘
                [_txtQueryKey resignFirstResponder];
    
            }
            
            NSDictionary *options = [[NSDictionary alloc] initWithObjectsAndKeys:MKLaunchOptionsDirectionsModeDriving,MKLaunchOptionsDirectionsModeKey, nil];
            if ([array count] > 0) {
                [MKMapItem openMapsWithItems:array launchOptions:options];
            }
        }];
    }
  • 相关阅读:
    (004)maven执行junit单元测试,指定执行哪些测试类
    (009)Nginx静态资源web服务
    (008)Nginx的访问控制_介绍实现访问控制的基本方式
    (03)Nginx将配置文件default.conf重命名后报Job for nginx.service failed because the control process exited with error code. See "systemctl status nginx.service" and "journalctl -xe" for details.解决方案
    (007)Nginx的请求限制_配置语法与原理
    (006)Nginx之模块讲解
    (005)Nginx之日志log_format
    (004)Nginx默认配置语法解析及演示
    (003)Nginx编译配置参数讲解
    Gym
  • 原文地址:https://www.cnblogs.com/yaoxc/p/3723156.html
Copyright © 2011-2022 走看看