1, 引用框架 CoreGraphics.framework MapKit.framework CoreLocation.framework
2 导入主头文件 iOS5之后不需要手动导入
#import <CoreLocation/CoreLocation.h>
#import <MapKit/MapKit.h>
(1)MapKit :用于地图展示
(2)CoreLocation :用于地理定位
CoreLocation框架使用须知
(1) Corelocation框架中所有数据类型的前缀都是CL
(2) Corelocation中使用的CLLocationManager对象来做用户定位
这是初始化
locationManager = [[CLLocationManager alloc] init];//初始化
locationManager.delegate = self;//委托自己
locationManager.desiredAccuracy = kCLLocationAccuracyBest;//精度设定
[locationManager startUpdatingLocation];//开启位置更新
定义经纬坐标
CLLocationCoordinate2D theCoordinate;
theCoordinate.latitude=21.238928; 经度
theCoordinate.longitude=113.313353;纬度
//
// ViewController.m
// MapStudy
//
// Created by lanouhn on 15/3/21.
// Copyright (c) 2015年 niutiantian. All rights reserved.
//
#import "ViewController.h"
#import <CoreLocation/CoreLocation.h>
#import <MapKit/MapKit.h>
#import "Annotation.h"
@interface ViewController ()<MKMapViewDelegate, CLLocationManagerDelegate[CDATA[]]>
{
//用来做用户定位
CLLocationManager *locationManeger;
CLLocation *location;
CLGeocoder *geocoder; //反向地理编码
CLLocationCoordinate2D coordinate; //地理位置坐标
MKMapView *_mapView;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//添加地图
_mapView = [[MKMapView alloc] initWithFrame:self.view.bounds];
_mapView.delegate = self;
[self.view addSubview:_mapView];
//定位管理器
locationManeger = [[CLLocationManager alloc] init];
//定位管理器没有打开
if (![CLLocationManager locationServicesEnabled]) {
NSLog(@"请打开定位");
return;
}
//请求用户授权
//kCLAuthorizationStatusNotDetermined 未授权
//kCLAuthorizationStatusAuthorizedWhenInUse 授权
if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusNotDetermined) {
[locationManeger requestWhenInUseAuthorization];
} else if ([CLLocationManager authorizationStatus]== kCLAuthorizationStatusAuthorizedWhenInUse) {
//设置代理
locationManeger.delegate = self;
locationManeger.desiredAccuracy = kCLLocationAccuracyBest;//精度设定
[locationManeger startUpdatingLocation]; //开启位置更新
}
_mapView.userTrackingMode = YES;//用户追踪当前位置
//设置地图类型
_mapView.mapType = MKMapTypeStandard;
//coordinate.latitude coordinate.latitude 经度 纬度
// CLLocation *location = [[CLLocation alloc] initWithLatitude:coordinate.latitude longitude:coordinate.latitude ];
}
#pragma mark - MKMapViewDelegate
#pragma mark - CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray *)locations {
location = [locations firstObject];
coordinate = location.coordinate;
//地理位置反编码
geocoder = [[CLGeocoder alloc] init];
[geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
CLPlacemark *placeMark = [placemarks firstObject];
NSLog(@"%@", placeMark.addressDictionary);
}];
//添加标记大头针
[self addAnnotation];
}
- (void)addAnnotation {
NSLog(@"%f", coordinate.latitude);
Annotation *annotation1 = [[Annotation alloc] init];
annotation1.title = @"CMJ Studio";
annotation1.subTitle = @"Kenshin Cui's Studios";
annotation1.coordinate = coordinate;
[_mapView addAnnotation:annotation1];
}
-(void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[locationManeger stopUpdatingLocation];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
@interface Annotation : NSObject<MKAnnotation[CDATA[]]>
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *subTitle;
@property (nonatomic, assign) CLLocationCoordinate2D coordinate;
@end