通过CLLocationManager类可以获取到当前位置的经纬度:
1.要包含头文件<CoreLocation/CoreLocation.h>
2.添加CLLocationManagerDelegate协议
3.在下面这个函数中:
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
newLocation.coordinate.latitude 当前位置的经度
newLocation.coordinate.longitude 当前位置的纬度
在.h文件中:
#import <UIKit/UIKit.h> #import <CoreLocation/CoreLocation.h> @interface mapViewController : UIViewController<CLLocationManagerDelegate> @property (nonatomic, strong) CLLocationManager* myLocationManager; @end
在.m文件中:
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { NSLog(@"latitude:%f",newLocation.coordinate.latitude); NSLog(@"longitude:%f",newLocation.coordinate.longitude); } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. if ([CLLocationManager locationServicesEnabled]) { self.myLocationManager = [[CLLocationManager alloc] init]; self.myLocationManager.delegate = self; [self.myLocationManager startUpdatingLocation]; } }
浙公网安备 33010602011771号