zoukankan      html  css  js  c++  java
  • iOS- 用MapKit和CoreLocation 来实现移动设备(地图与定位)

    1.前言                              

    发现在很多的社交软件都引入了地图和定位功能,如果我们要想实现这两大功能,需要利用到两个框架:MapKitCoreLocation
     
    我们先来看看CoreLocation框架:
     
    它可以使用硬件设备来进行定位服务,不需要地图,精度相对略差,省电。
     
    MapKit框架:
     
    能够使应用程序做一些地图展示与交互的相关功能,必须有地图,精度相对较高,费电。
     
    下面我就说说它的能实现的一些常用功能

    2.地图常见操作                        

    @property (weak, nonatomic) IBOutletMKMapView *mapView;

    2.1. 标记用户当前位置、跟踪用户位置                        

    1     // 标记用户当前位置
    2     // 跟踪用户位置
    3     [_mapView setUserTrackingMode:MKUserTrackingModeFollow];

    2.1. 地图的类型                                  

    可以通过设置MKMapView的mapViewType设置地图类型
    MKMapTypeStandard 普通地图
    MKMapTypeSatellite 卫星云图
    MKMapTypeHybrid普通地图覆盖于卫星云图之上                     
     // 地图类型
        [_mapView setMapType:MKMapTypeHybrid];

    2.2.通过代理的方式可以跟踪用户的位置变化                     

    2.2.1.mapViewWillStartLoadingMap: 当地图界面将要加载时调用
    2.2.2.mapView:viewForAnnotation: 当地图上有大头针时调用
    2.2.3.mapViewWillStartLocatingUser:当准备进行一个位置定位时调用
    2.2.4.mapView:regionDidChangeAnimated: 当显示的区域发生变化时调用
     

    2.2.5.mapView:didUpdateUserLocation:当用户位置发生变化时调用

    1 // 通过代理的方式可以跟踪用户的位置变化
    2     _mapView.delegate = self;
    #pragma mark - 地图代理方法
    #pragma mark 会频繁调用,非常费电
    - (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
    {
    
        
        // 显示用户周边信息 拉近地图 设置地图显示区域

        CLLocationCoordinate2D center = userLocation.location.coordinate;

    
    
    
    

        MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(center, 100.0, 100.0);

    
    
    
    
    

        [mapView setRegion:region animated:YES];

    }

    2.3.添加默认大头针(地标)                              

    1.通过MapView的addAnnotation方法可以添加一个大头针到地图上
    2.通过MapView的addAnnotations方法可以添加多个大头针到地图上
    头文件里说明:- (void)addAnnotation:(id <MKAnnotation>)annotation;
    4.说明需要传入一个遵守了MKAnnotation协议的对象
    1     MyAnnotation *annotation2 = [[MyAnnotation alloc] init];
    2     annotation2.coordinate = CLLocationCoordinate2DMake(30, 106);
    3     annotation2.title = @"重庆";
    4     annotation2.subtitle = @"重庆详细描述";
    5     annotation2.imageName = @"head0";
         [_mapView addAnnotation:annotation2];

    2.4.添加自定义大头针(重用地标)                            

     1 #pragma mark 自定义大头针视图
     2 - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
     3 {
     4     // 判断annotation参数是否是MyAnnotation
     5     // 如果不是MyAnnotaion说明是系统的大头针,无需做处理
     6     if (![annotation isKindOfClass:[MyAnnotation class]]) {
     7         // 使用系统默认的大头针
     8         return nil;
     9     }
    10     
    11     // 可重用标示符
    12     static NSString *ID = @"MyAnno";
    13     
    14     // 查询可重用的大头针
    15     MKAnnotationView *annoView = [mapView dequeueReusableAnnotationViewWithIdentifier:ID];
    16     
    17     // 如果没有找到,再去实例化大头针
    18     if (annoView == nil) {
    19         annoView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:ID];
    20         
    21         // 自定义大头针视图,如果要接受用户响应,需要设置此属性
    22         annoView.canShowCallout = YES;
    23     }
    24     
    25     // 设置大头针
    26     annoView.annotation = annotation;
    27     // 转换成MyAnnotation
    28     // 设置大头针视图的图像
    29     MyAnnotation *anno = annotation;
    30     annoView.image = [UIImage imageNamed:anno.imageName];
    31     
    32     NSLog(@"自定义大头针");
    33     
    34     return annoView;
    35 }

     3.定位                              

       // 定位服务管理器
        CLLocationManager *_locationManager;
        
        // 使用地理编码器
        CLGeocoder          *_geocoder;

     1. 在开发LBS类的应用时,获取用户定位信息之前,一定要判断一下定位服务是否允许  

     locationServicesEnabled

    1     
    2     if (![CLLocationManager locationServicesEnabled]) {
    3         NSLog(@"定位服务不可用!");
    4         return;
    5     }
    6     

     2. 开启定位,获取自己的当前位置                            

     [_locationManager startUpdatingLocation];

     3. 根据经纬度,知道准确的地名                              

        reverseGeocodeLocation

     1 - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
     2 {
     3     NSLog(@"位置变化: %@", locations[0]);
     4     
     5     // 根据经纬度查找(去苹果后台查找准确的位置,必须联网才能用)
     6     [_geocoder reverseGeocodeLocation:locations[0] completionHandler:^(NSArray *placemarks, NSError *error) {
     7         
     8         NSLog(@"%@", placemarks[0]);
     9         
    10     }];
    11 }

     4. 根据定名,获取到经纬度,一般用在导航                        

        geocodeAddressString

    1    [_geocoder geocodeAddressString:@"王府井" completionHandler:^(NSArray *placemarks, NSError *error) {
    2         
    3         for (CLPlacemark *placemark in placemarks) {
    4             NSLog(@"aaaa______%@ %lu", placemark, (unsigned long)placemarks.count);
    5         }
    6         
    7     }];

                                                                 清澈Saup  

  • 相关阅读:
    智慧养老民政监管平台建设方案
    CF600E Lomsat gelral dsu on tree
    dsu on tree详解
    【Spring 从0开始】Spring5 新功能,整合日志框架 Log4j2
    【Spring 从0开始】JdbcTemplate 数据库事务管理
    【Spring 从0开始】JdbcTemplate 数据库事务参数
    【Spring 从0开始】JdbcTemplate 数据库事务管理
    【Spring 从0开始】JdbcTemplate 操作数据库
    【Spring 从0开始】AOP 操作
    【Spring 从0开始】AOP 操作中的相关术语、环境准备
  • 原文地址:https://www.cnblogs.com/qingche/p/3516591.html
Copyright © 2011-2022 走看看