• (IOS)CoreLocation 和 MapKit 的应用


    CoreLocation是控制GPS硬件获取地理坐标信息的系统类库,而MapKit是系统地图的工具包,将两者结合使用可以实现不同的地图功能。

    1.CoreLocation

    在CoreLocation中,CLLocationManager是获取坐标的工具,创建如下:

    if ([CLLocationManager locationServicesEnabled]) // 判断设备是否支持定位功能
        {
            NSLog(@"支持定位");
            locManager = [[CLLocationManager alloc] init];
            locManager.distanceFilter = 10; // 过滤距离,以m为单位,越小更新越精确越快,但越耗电
            locManager.desiredAccuracy = kCLLocationAccuracyBest; // 定位精确度,但耗电
            locManager.delegate = self;
        }
        [locManager startUpdatingLocation]; // 设置好属性后,需要开始更新定位

    CLLocationManager更新位置的代理方法如下,其中需要用到MKReverseGeocoder对经纬度进行解析,获取更直观详细的位置信息:

    - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
    {
        NSLog(@"%s",__func__);
        NSLog(@"locs = %@",locations);
        
        CLLocation *newLoc = (CLLocation *)[locations objectAtIndex:0]; // 位置信息
        
        // 解析地理编码
        MKReverseGeocoder *geo = [[MKReverseGeocoder alloc] initWithCoordinate:newLoc.coordinate]; // 解析经纬度(在IOS5中该方法过期了,需要用CLGeocoder)
        geo.delegate = self;
        [geo start];
    }

    MKReverseGeocoder解析经纬度后的代理方法:

    // 反解析成功
    - (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark
    {
        NSLog(@"国家:%@",placemark.country);
        NSLog(@"市区:%@",placemark.locality);
    }
    
    // 反解析失败
    - (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFailWithError:(NSError *)error
    {
    }

    2.MapKit

    创建一个地图视图

    mapView = [[MKMapView alloc] initWithFrame:self.view.bounds];
    mapView.mapType = MKMapTypeStandard; // 地图显示类型,分标准,卫星,俯瞰
    mapView.showsUserLocation = YES; // 是否显示当前位置
    mapView.delegate = self;

    要更新地图的话,可以在-locationManager:didUpdateLocations:中设置mapView的相关属性,如下:

    // 设置地图区域,并确定地图细腻程度 ,放在解析经纬度之前
    
        MKCoordinateRegion region;  // 显示区域
        region.center = newLoc.coordinate;
        
        MKCoordinateSpan span; // 显示范围
        span.latitudeDelta = 0.01f;   // 细腻精度系数
        span.longitudeDelta = 0.01f;
        region.span = span;
        
        mapView.region = region;

    若要在地图中插入一个大头针的话,先要创建一个遵守MKAnnotation协议的类,

    .h
    #import <Foundation/Foundation.h>
    #import <MapKit/MapKit.h>
    
    @interface BIDDemoAnnotation : NSObject <MKAnnotation>
    {
        CLLocationCoordinate2D _coordinate;
    }
    
    @property (readonly,nonatomic) CLLocationCoordinate2D coordinate;
    @property (nonatomic, copy) NSString *title;
    @property (nonatomic, copy) NSString *subtitle;
    
    - (id)initWithCoordinate2D:(CLLocationCoordinate2D)aCoordinate;
    @end
    
    #####################################
    .m
    #import "BIDDemoAnnotation.h"
    
    @implementation BIDDemoAnnotation
    @synthesize title = _title;
    @synthesize subtitle = _subtitle;
    @synthesize coordinate = _coordinate;
    
    - (id)initWithCoordinate2D:(CLLocationCoordinate2D)aCoordinate
    {
        if (self = [super init]) {
            _coordinate = aCoordinate;
            // self.coordinate = aCoordiante 错误,因为该属性为只读
        }
        return self;
    }
    @end

    然后在反解析成功的代理方法中插入如下代码:

    // [mapView removeAnnotations:mapView.annotations]; 清理已有的大头针
    
    //根据经纬度创建大头针
    BIDDemoAnnotation *ano = [[BIDDemoAnnotation alloc] initWithCoordinate2D:geocoder.coordinate]; 
        
    //设置大头针的备注信息
    ano.title = placemark.country;
    ano.subtitle = placemark.locality;
    
    //插入到地图
    [mapView addAnnotation:ano];
    [ano release];

    最后实现MKMapView的代理方法(原理和TableView一样,重用机制),设置大头针的样式等信息:

    - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
    {
        if ([annotation.title isEqualToString:@"Current Location"] == YES) {
            return nil;
        }
        
        static NSString *identifier = @"test";
        MKPinAnnotationView *pinAnnotationView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
        if (pinAnnotationView == nil) {
            pinAnnotationView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier] autorelease];
        }
        pinAnnotationView.pinColor = MKPinAnnotationColorRed;
        pinAnnotationView.animatesDrop = YES;
        
        UIButton *rightView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
        pinAnnotationView.rightCalloutAccessoryView = rightView;
        
        UIButton *leftView = [UIButton buttonWithType:UIButtonTypeCustom];
        leftView.frame = CGRectMake(0, 0, 50, 30);
        leftView.userInteractionEnabled = YES;
        
        pinAnnotationView.canShowCallout = YES;
        pinAnnotationView.leftCalloutAccessoryView = leftView;
        
        return pinAnnotationView;
    }
  • 相关阅读:
    java编辑器eclipse如何更改jdk版本
    java 获取实体类对象属性值的方法
    java 时间处理
    java file的一些方法
    java中的包装类与装箱拆箱定义
    java中的分支
    java中的循环
    冒泡排序法
    HDFS数据迁移解决方案之DistCp工具的巧妙使用
    HDFS数据迁移解决方案之DistCp工具的巧妙使用
  • 原文地址:https://www.cnblogs.com/mingfung-liu/p/3373466.html
Copyright © 2020-2023  润新知