IPhone定位当前位置

1、需要加入CoreLocation.framework,导入#import <CoreLocation/CoreLocation.h>

2、在类中生命两个变量,并实现代理

@interface ViewController () <CLLocationManagerDelegate>{
    CLLocationManager * locationManager;
    CLLocationCoordinate2D  curLocation;
}

3、定位的方法如下

//获得自己的地理位置信息
- (void) getCurrentLocation {
    //开始探测位置
    if (locationManager == nil) {
        locationManager = [[CLLocationManager alloc] init];
    }
    
    if ([CLLocationManager locationServicesEnabled]) {
        locationManager.delegate = self;
        locationManager.desiredAccuracy = kCLLocationAccuracyBest;
        locationManager.distanceFilter = 10.0f;
        /*
        启动对当前位置的搜索,只要当前位置移动的范围超过了distanceFilter指定的范围,
        那么就会重新收到最新的坐标位置信息,如果要关闭对位置的更新,需要调用 stopUpdatingLocation
        */
        [locationManager startUpdatingLocation];
    }
}

4、在代理方法中获得地理位置

//响应当前位置的更新,在这里记录最新的当前位置
- (void) locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{
    NSLog(@"newLocation:%@",[newLocation description]);
    curLocation = newLocation.coordinate;
}
原文地址:https://www.cnblogs.com/benbenzhu/p/3014838.html