目录
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;
}
}];
}

使用高德地图
下载安装配置
- 高德开放平台 https://lbs.amap.com
- 注册账号,获取 key
使用百度地图
下载安装配置
- 百度地图开放平台 http://lbsyun.baidu.com/index.php?title=首页
- 注册账号,获取 key
本文详细介绍了iOS中进行GPS定位的方法,包括使用CoreLocation.framework获取经纬度,结合MapKit.framework显示地图和标注位置。同时,讲解了如何使用CLGeocoder进行坐标转换,并探讨了集成高德地图和百度地图的步骤,包括下载安装和获取API Key的过程。
1158

被折叠的 条评论
为什么被折叠?



