iOS开发学习之路【高级主题】——GPS 定位

本文详细介绍了iOS中进行GPS定位的方法,包括使用CoreLocation.framework获取经纬度,结合MapKit.framework显示地图和标注位置。同时,讲解了如何使用CLGeocoder进行坐标转换,并探讨了集成高德地图和百度地图的步骤,包括下载安装和获取API Key的过程。

GPS 定位简介

在 iOS SDK 中提供了两个框架,来实现位置服务。分别是 CoreLocation.framework 和 MapKit.framework。

CoreLocation.framework 主要提供了获得设备信息的API,例如经纬度信息等;MapKit.framework 主要提供了展示地图的API。

这两个框架的核心类是 CLLocationManager类和 MKMapView类,CLLocationManager类提供了获得位置信息的功能,MKMapView类提供了展示地图的功能。

使用 MKMapView 显示地图

导入框架 MapKit.framework;

在storyboard中添加组件;
在这里插入图片描述

#import "MapKit/MapKit.h"

@interface ViewController : UIViewController<MKMapViewDelegate>
@property (strong, nonatomic) IBOutlet MKMapView *myMapView;

- (IBAction)change:(UISegmentedControl *)sender {
    // 地图类型
    NSInteger index = sender.selectedSegmentIndex;
    switch (index) {
        case 0:
            self.myMapView.mapType = MKMapTypeStandard;
            break;
        case 1:
            self.myMapView.mapType = MKMapTypeSatellite;
            break;
        case 2:
            self.myMapView.mapType = MKMapTypeHybrid;
            break;
            
        default:
            break;
    }
}
// 开始加载地图
- (void)mapViewWillStartLoadingMap:(MKMapView *)mapView{
    NSLog(@"mapViewWillStartLoadingMap...");
}
// 地图加载完成
- (void)mapViewDidFinishLoadingMap:(MKMapView *)mapView{
    NSLog(@"mapViewDidFinishLoadingMap...");
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // 设置代理
    self.myMapView.delegate = self;
    // Do any additional setup after loading the view.
}

在这里插入图片描述

使用 CLLocationManager 获得经纬度

导入 CoreLocation.framework 框架

#import <UIKit/UIKit.h>
#import "CoreLocation/CoreLocation.h"

@interface ViewController : UIViewController<CLLocationManagerDelegate>

@end
#import "ViewController.h"

@interface ViewController ()
@property(nonatomic,strong)CLLocationManager *manager;
@end

@implementation ViewController
- (IBAction)location:(UIButton *)sender {
    
    [self.manager startUpdatingLocation];
    
}

// 获取位置
- (void)locationManager:(CLLocationManager *)manager
     didUpdateLocations:(NSArray<CLLocation *> *)locations{
    if(locations.count > 0){
        CLLocation *l = [locations objectAtIndex:0];
        NSLog(@"[%f,%f]", l.coordinate.latitude, l.coordinate.longitude);
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.manager = [[CLLocationManager alloc]init];
    if([CLLocationManager locationServicesEnabled]){
        NSLog(@"locationServicesEnabled...");
        // 精度
        self.manager.desiredAccuracy = 0.3;
        // 最小距离
        self.manager.distanceFilter = 0.5;
        self.manager.delegate = self;
    }
    
    // Do any additional setup after loading the view.
}

@end

在地图上标注位置

导入 CoreLocation.framework 和 MapKit.framework 框架

在storyboard中加入组件

在这里插入图片描述

创建标注类

#import <Foundation/Foundation.h>
#import "MapKit/MapKit.h"

NS_ASSUME_NONNULL_BEGIN

@interface MyAnnotation : NSObject<MKAnnotation>

@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;

// Title and subtitle for use by selection UI.
@property (nonatomic, readonly, copy, nullable) NSString *title;
@property (nonatomic, readonly, copy, nullable) NSString *subtitle;

-(instancetype)initWith:(CLLocationCoordinate2D) c andTitle:(NSString*)title andSubTile:(NSString*)subTitle;

@end

NS_ASSUME_NONNULL_END
#import "MyAnnotation.h"

@implementation MyAnnotation
-(instancetype)initWith:(CLLocationCoordinate2D) c andTitle:(NSString*)title andSubTile:(NSString*)subTitle{
    self = [super init];
    if (self) {
        _coordinate = c;
        _title = title;
        _subtitle = subTitle;
    }
    return self;
}
@end

在ViewController中添加标注

#import "ViewController.h"
#import "MyAnnotation.h"

@interface ViewController ()
@property (strong, nonatomic) IBOutlet MKMapView *MyMapView;

@end

@implementation ViewController
- (IBAction)pin:(id)sender {
    
    MyAnnotation *a = [[MyAnnotation alloc]initWith:CLLocationCoordinate2DMake(28.5, 121.5) andTitle:@"Title" andSubTile:@"sub title"];
    
    [self.MyMapView addAnnotation:a];
    
    // 移动到该位置并设置level,平移放大
    MKCoordinateRegion region;
    region.center.latitude = 28.5;
    region.center.longitude = 121.5;
    region.span.latitudeDelta = 1;
    region.span.longitudeDelta = 1;
    self.MyMapView.region = region;
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}


@end

在这里插入图片描述

使用 CLGeocoder

#import "CoreLocation/CoreLocation.h"

将经纬度转化为位置坐标

- (IBAction)convert1:(UIButton *)sender {
    
    CLLocation *l = [[CLLocation alloc]initWithLatitude:28.5 longitude:121.5];
    [self.coder reverseGeocodeLocation:l completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
        if(placemarks.count > 0 && error == nil){
            CLPlacemark *mark = [placemarks objectAtIndex:0];
            NSString *info = [NSString stringWithFormat:@"%@,%@",mark.country,mark.locality];
            NSLog(@"%@",info);
        }
    }];
    
}

在这里插入图片描述

将位置坐标转化为经纬度

- (IBAction)convert2:(id)sender {
    
    [self.coder geocodeAddressString:self.address.text completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
        if ([placemarks count]>0 && error==nil) {
            CLPlacemark *mark = [placemarks objectAtIndex:0];
            double lot = mark.location.coordinate.latitude;
            double lon = mark.location.coordinate.longitude;
            CLLocationCoordinate2D coord = CLLocationCoordinate2DMake(lot, lon);
            MyAnnotation *ann = [[MyAnnotation alloc]initWith:coord andTitle:@"My Title" andSubTile:@"my sub titlt"];
            [self.MyMapView addAnnotation:ann];
            // 移动到该位置并设置level,平移放大
            MKCoordinateRegion region;
            region.center.latitude = lot;
            region.center.longitude = lon;
            region.span.latitudeDelta = 1;
            region.span.longitudeDelta = 1;
            self.MyMapView.region = region;
        }
    }];

}

在这里插入图片描述

使用高德地图

下载安装配置

  1. 高德开放平台 https://lbs.amap.com
  2. 注册账号,获取 key

使用百度地图

下载安装配置

  1. 百度地图开放平台 http://lbsyun.baidu.com/index.php?title=首页
  2. 注册账号,获取 key
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值