iOS自带定位

来源:互联网 发布:淘宝招聘网最新招聘 编辑:程序博客网 时间:2024/04/30 08:04

由于需要用到定位:由于其版本需要在国外也可以使用故。。百度。高德等地图似乎无法在国外定位,而且他们的包也有10M以上。。。。

1.添加框架:

#import <CoreLocation/CoreLocation.h>
#import <MapKit/MapKit.h>

2.添加代理,设置属性:

CLLocationManagerDelegate

@property (nonatomic, strong) CLLocationManager *locMgr; 
3.初始化类CLLocationManager

    locationManager = [[CLLocationManager alloc] init];
    locationManager.distanceFilter = 5.0f;
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    locationManager.delegate = self;

4.实现代理方法:

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    //CLLocation中存放的是一些经纬度, 速度等信息. 要获取地理位置需要转换做地理位置编码.
    // 1.取出位置对象
    CLLocation *loc = [locations firstObject];
    NSLog(@"CLLocation----%@",loc);
    // 2.取出经纬度
    CLLocationCoordinate2D coordinate = loc.coordinate;
    // 3.打印经纬度
    NSLog(@"didUpdateLocations------%f %f", coordinate.latitude, coordinate.longitude);
    // 停止定位(省电措施:只要不想用定位服务,就马上停止定位服务)
    [locationManager stopUpdatingLocation];
    CLGeocoder *geocoder = [[CLGeocoder alloc] init];
    [geocoder reverseGeocodeLocation: loc completionHandler:^(NSArray *array, NSError *error) {
        if (array.count > 0) {
            CLPlacemark *placemark = [array objectAtIndex:0];
            NSLog(@"%@",placemark.name);
            self.grsAddress.text=placemark.name;//展示Label
        }
    }];

}
由于使用的只是简单的定位功能。。。。所以推荐使用系统自带

0 0
原创粉丝点击