(IOS)CoreLocation 和 MapKit 的应用

本文详细介绍了如何使用CoreLocation获取设备的地理位置信息,并通过MapKit将其展示在地图上。包括初始化CoreLocation,配置定位参数,解析经纬度获取详细地址信息,以及在地图视图中更新位置和添加标记点。通过反解析经纬度得到国家和城市信息,最终在地图上以大头针的形式展示当前位置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
}

 

转载于:https://www.cnblogs.com/mingfung-liu/p/3373466.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值