iOS开发中经常会涉及到地图开发,比较长见的如打车软件、点评软件、团购软件、旅游软件还有许多社交软件,都会涉及。地图开发主要有以下几个步骤:
导入markit、action包-》生成MKMapView对象-》设置MKMapView代理-》实现代理。
一、显示以某个区域为中心的地图
1、生成MKMapView对象
_mapView = [[MKMapView alloc] initWithFrame:CGRectMake(0, 64, width, height - 64 - 49 - 50)];
2、设置MapView代理方法:
_mapView.delegate = self;
[self.view addSubview:_mapView];
3、定义一个坐标
CLLocationCoordinate2D coordinate = {31.19316, 121.43154};
4、设置缩放级别
MKCoordinateSpan span = {0.005, 0.005};
MKCoordinateRegion region = {coordinate, span};
[_mapView setRegion:region];
二、定位用户位置开发路线:MKMapView设置showsUserLocation=YES可以显示定位=》创建CLLocationManager =》开始定位=》实现定位代理方法。
1.创建CLLocationManager方法
self.locationManager = [[CLLocationManager alloc] init];
2.设置CLLocationManager代理
self.locationManager.delegate = self;
首先需在target info中增加NSLocationWhenInUseUsageDescription 字段,其次请求用户授权
if ([[UIDevice currentDevice].systemVersion doubleValue] >= 8.0) { // 如果系统设备>= 8.0
[self.locationManager requestWhenInUseAuthorization] ;
}
然后在授权状态改变的代理方法中,处理授权状态的改变。
- (void) locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
if (status == kCLAuthorizationStatusNotDetermined) {
NSLog(@"等待用户授权");
}else if (status == kCLAuthorizationStatusAuthorizedAlways ||
status == kCLAuthorizationStatusAuthorizedWhenInUse) {
4. [self.locationManager startUpdatingLocation];
} else {
NSLog(@"授权失败");
}
}
4.开始定为操作
[self.locationManager startUpdatingLocation];
5.实现定位代理方法。定位成功或失败根据代理来设置
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
//定位完成后停止定位
[self.locationManager stopUpdatingLocation];
NSLog(@"%@", error.description);
}
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
[self.locationManager stopUpdatingLocation];
self.currentLocation = newLocation;
NSString locationStr = [NSString stringWithFormat:@"(%f,%f)", newLocation.coordinate.latitude,
newLocation.coordinate.longitude];
}
两点距离: CLLocationDistance distance = [newLocation distanceFromLocation:oldLocation];通常会出现于 你与那个地方相距多远。
完整代码:
#import <UIKit/UIKit.h>
#import "BLBaseViewController.h"
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
@interface LocationViewController:UIViewController
<CLLocationManagerDelegate, MKMapViewDelegate>
{
MKMapView *_mapView;
UILabel *_locationLabel;
NSMutableArray *_annotations;
}
@property (nonatomic, strong) CLLocationManager *locationManager;
@end
@interface LocationViewController ()
@end
@implementation LocationViewController
- (void)viewDidLoad {
[super viewDidLoad];
CGFloat width = self.view.bounds.size.width;
CGFloat height = self.view.bounds.size.height;
_mapView = [[MKMapView alloc] initWithFrame:CGRectMake(0, 64, width, height - 64 - 49 - 50)];
_mapView.showsUserLocation = YES;
_mapView.delegate = self;
[self.view addSubview:_mapView];
CLLocationCoordinate2D coordinate = {31.19316, 121.43154};
MKCoordinateSpan span = {0.05, 0.05};
MKCoordinateRegion region = {coordinate, span};
[_mapView setRegion:region];
_locationLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, _mapView.frame.origin.y + _mapView.frame.size.height, width, 50)];
_locationLabel.textAlignment = NSTextAlignmentCenter;
_locationLabel.backgroundColor = [UIColor cyanColor];
_locationLabel.text = @"user's location.";
[self.view addSubview:_locationLabel];
UIBarButtonItem *locationButton
= [[UIBarButtonItem alloc] initWithTitle:@"定位"
style:UIBarButtonItemStyleBordered
target:self
action:@selector(locationButtonClicked:)];
self.navigationItem.leftBarButtonItem = locationButton;
}- (void)locationButtonClicked:(id)sender
{
if (self.locationManager == nil) {
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
}
if ([[UIDevice currentDevice].systemVersion doubleValue] >= 8.0) { // 如果系统设备>= 8.0
[self.locationManager requestWhenInUseAuthorization] ;
} else {
// 2. 开始定位
[self.locationManager startUpdatingLocation] ;
}
}
#pragma mark - CLLocationManagerDelegate methods
- (void) locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
if (status == kCLAuthorizationStatusNotDetermined) {
NSLog(@"等待用户授权");
}else if (status == kCLAuthorizationStatusAuthorizedAlways ||
status == kCLAuthorizationStatusAuthorizedWhenInUse) {
[self.locationManager startUpdatingLocation];
} else {
NSLog(@"授权失败");
}
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
[self.locationManager stopUpdatingLocation];
NSLog(@"%@", error.description);
}
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
[self.locationManager stopUpdatingLocation];
self.currentLocation = newLocation;
_locationLabel.text = [NSString stringWithFormat:@"(%f,%f)", newLocation.coordinate.latitude,
newLocation.coordinate.longitude];
}