一、获取用户的位置
1.首先链接CoreLocation.framework框架,然后在需要定位的地方导入<CoreLocation/CoreLocation.h>头文件
7.0以后要遭plist文件里添加 NSLocationAlwaysUsageDescription和NSLocationWhenInUseUsageDescription
2.在开始使用定位前调用[CLLocationManager locationServicesEnabled]判断定位服务能否使用
- (void)getCurrentLocation {
if(self.locationMarnger == nil){
self.locationMarnger = [[CLLocationManager alloc] init];
}
//设置地图的精准度
self.locationMarnger.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
//设置超多100米获取新的位置
self.locationMarnger.distanceFilter = 100;
self.locationMarnger.delegate = self;
[self.locationMarnger startUpdatingLocation];
}
#pragma mark - CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray *)locations {
NSLog(@"*********success*********");
//停止定位
[manager stopUpdatingLocation];
//获取设备当前位置
CLLocation *location = [locations lastObject];
CLLocationCoordinate2D coordinate = location.coordinate;
NSLog(@"coordinate: latitude = %f,longitude = %f",coordinate.latitude,coordinate.longitude);
}
- (void)locationManager:(CLLocationManager *)manager
didFailWithError:(NSError *)error{
NSLog(@"*********fail*********");
}
注:@property (nonatomic,strong) CLLocationManager *locationMarnger;为全局变量
二、反编码
引入AddressBook.framework框架,并在头文件导入<AddressBook/AddressBook.h>
在定位成功的时候调用下面的方法
#pragma mark - 进行反编码
- (void)reverseLocation:(CLLocation *)location {
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
if(placemarks.count > 0 && error == nil){
CLPlacemark *placeMark = placemarks[0];
//存储反编码的地理信息
NSDictionary *placeDiction = placeMark.addressDictionary;
for(NSString *key in placeDiction.allKeys){
NSLog(@"%@ = %@",key,placeDiction[key]);
}
}
}];
}
三、通过地址得到地理信息
#pragma mark - 通过地址的到地理信息
- (void)getAddressInformation:(NSString *)string currenCoordinate:(CLLocationCoordinate2D)coordinate {
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
CLCircularRegion *region = [[CLCircularRegion alloc] initWithCenter:coordinate radius:100000 identifier:@"region"];
[geocoder geocodeAddressString:string inRegion:region completionHandler:^(NSArray *placemarks, NSError *error) {
if(placemarks.count > 0 && error == nil){
CLPlacemark *placeMark = placemarks[0];
//存储反编码的地理信息
NSDictionary *placeDiction = placeMark.addressDictionary;
NSLog(@"address name = %@",placeDiction[@"Name"]);
CLLocationCoordinate2D coordinate = placeMark.location.coordinate;
NSLog(@"coordinate: latitude = %f,longitude = %f",coordinate.latitude,coordinate.longitude);
}
}];
}
四、使用地图
添加MapKit.framework框架,在使用的图的地方导入<MapKit/MapKit.h>
1.添加地图
self.mapView = [[MKMapView alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.mapView.mapType = MKMapTypeStandard;
self.mapView.delegate = self;
self.mapView.showsUserLocation = YES;
[self.view addSubview:self.mapView];
//改变当前地图显示区域
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(coordinate, 10000, 10000);
[self.mapView setRegion:region animated:YES];
2.添加标注
//向地图添加标注
MyAnnotation *annottaion = [[MyAnnotation alloc] init];
annottaion.coordinate = coordinate;
annottaion.title = @"成功了";
[self.mapView addAnnotation:annottaion];
MyAnnotation的类定义,必须实现MKAnnotation协议
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
@interface MyAnnotation : NSObject<MKAnnotation>
@property (nonatomic,assign) CLLocationCoordinate2D coordinate;
@property (nonatomic,copy) NSString *title;
@end
3.实现mapView的代理方法
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
static NSString *iden = @"annotationView";
MKPinAnnotationView *annotationView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:iden];
if(annotationView == nil){
annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:iden];
annotationView.canShowCallout = YES;
annotationView.animatesDrop = YES;
annotationView.pinColor = MKPinAnnotationColorRed;
}
return annotationView;
}