#import <UIKit/UIKit.h> #import <CoreLocation/CoreLocation.h> #import <MapKit/MapKit.h> @interface MyAnnotication : NSObject<MKAnnotation> @property (nonatomic) CLLocationCoordinate2D coordinate; @property (nonatomic, copy) NSString *title; @property (nonatomic, copy) NSString *subtitle; @end @interface ViewController : UIViewController<CLLocationManagerDelegate,MKMapViewDelegate> @end
#import "ViewController.h" @implementation MyAnnotication @end @interface ViewController () @end @implementation ViewController{ CLLocationManager* _Manager; CLGeocoder* _Geocoder; CLLocation* _MyLocation; MKMapView* _MapView; } - (void)viewDidLoad { [super viewDidLoad]; _Manager = [[CLLocationManager alloc] init]; _MapView = [[MKMapView alloc] initWithFrame:[UIScreen mainScreen].bounds]; _MapView.delegate = self; _MapView.userTrackingMode = MKUserTrackingModeFollow; [self.view addSubview:_MapView]; if (![CLLocationManager locationServicesEnabled]) { NSLog(@"定位功能还没有打开"); return; } CLAuthorizationStatus state = [CLLocationManager authorizationStatus]; if (state ==kCLAuthorizationStatusNotDetermined){ [_Manager requestWhenInUseAuthorization]; } else if (state == kCLAuthorizationStatusAuthorizedWhenInUse){ _Manager.delegate = self; _Manager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters; CLLocationDistance distance = 10.0; _Manager.distanceFilter = distance; [_Manager startUpdatingLocation]; } _Geocoder = [[CLGeocoder alloc] init]; /*地理编码测试*/ // [_Geocoder geocodeAddressString:@"昌平区沙河镇" completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) { // [placemarks enumerateObjectsUsingBlock:^(CLPlacemark * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { // CLLocation* Location = obj.location; // CLRegion* Region = obj.region; // NSData* data = [NSJSONSerialization dataWithJSONObject:obj.addressDictionary options:NSJSONWritingPrettyPrinted error:nil]; // NSString* str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; // NSLog(@"位置:%@,区域:%@,详细信息:%@",Location,Region,str); // }]; // }]; } -(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations { _MyLocation = [locations firstObject]; CLLocationCoordinate2D FirstCoordinate = [_MyLocation coordinate]; NSLog(@"经度:%f,纬度:%f,海拔:%f,航向:%f,行走速度:%f",FirstCoordinate.longitude,FirstCoordinate.latitude,_MyLocation.altitude,_MyLocation.course,_MyLocation.speed); /*地理反编码测试~*/ [_Geocoder reverseGeocodeLocation:_MyLocation completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) { CLPlacemark *placemark=[placemarks firstObject]; NSData* data = [NSJSONSerialization dataWithJSONObject:placemark.addressDictionary options:NSJSONWritingPrettyPrinted error:nil]; NSString* str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; MyAnnotication *annotation1=[[MyAnnotication alloc]init]; annotation1.title=[placemark.addressDictionary valueForKey:@"Name"]; annotation1.subtitle=[placemark.addressDictionary valueForKey:@"SubLocality"]; annotation1.coordinate=FirstCoordinate; [_MapView addAnnotation:annotation1]; /*定位一次后,关闭*/ [_Manager stopUpdatingLocation]; NSLog(@"详细信息:%@",str); }]; } -(MKAnnotationView*)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation { static NSString* ViewIdentifer = @"ViewIdentifer"; if ([annotation isKindOfClass:[MyAnnotication class]]) { MKAnnotationView* AnnotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:ViewIdentifer]; if (!AnnotationView) { AnnotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:ViewIdentifer]; AnnotationView.canShowCallout = YES;/*允许用户点击*/ AnnotationView.calloutOffset = CGPointMake(0, 1); AnnotationView.leftCalloutAccessoryView =[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"J.jpg"]]; /*如果不设置这个值,什么都看不到~*/ AnnotationView.image = [UIImage imageNamed:@"J.jpg"]; } return AnnotationView; } else{ return nil; } } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
定位效果还挺理想,不错啊。
想利用这个写个移动签到的小程序。。。
参考:http://www.cnblogs.com/kenshincui/p/4125570.html