iOS 地图SDK的使用 -- 定位基础

最近公司有点业务需求的变化,要我负责地图的相关开发,苦苦找了很多资料,今天准备整理整理一下定位相关的方法。

苹果自己有的定位的库,CoreLocation.使用corelocation定位很简单,就是创建管理者对象,让管理者的对象的代理=self,调用

startUpdatingLocation方法就可以实现定位的方法。废话不多说,直接看下面的代码:

#import "TXBMapKitController.h"
#import <CoreLocation/CoreLocation.h>

@interface TXBMapKitController ()<CLLocationManagerDelegate>

//引用管理者
@property (nonatomic, strong) CLLocationManager *locationManage;
@end

@implementation TXBMapKitController
//懒加载
- (CLLocationManager *)locationManage{
    
    if (!_locationManage) {
        _locationManage = [[CLLocationManager alloc] init];
        _locationManage.delegate = self;
    }
    return _locationManage;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
   }

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    
    if ([CLLocationManager locationServicesEnabled] == NO) {
        return;
    }
    [self.locationManage startUpdatingLocation];
    
    //测试位置距离
    [self distanceTest];
    
 
}

- (void)distanceTest{
    
    CLLocation *loc1 = [[CLLocation alloc] initWithLatitude:40.0 longitude:117.0];
    CLLocation *loc2 = [[CLLocation alloc] initWithLatitude:41.0 longitude:117.0];
    
    CLLocationDistance distance = [loc1 distanceFromLocation:loc2];
    NSLog(@"%f",distance);
}


#pragma mark  -- CLLocationManagerDelegate

//这个方法调用非常频繁,慎用
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
    
    for (CLLocation *location in locations) {
        CLLocationDegrees latitude = location.coordinate.latitude;
        CLLocationDegrees longtitude = location.coordinate.longitude;
        NSLog(@"纬度:%f,经度:%f",latitude,longtitude);
    }
    
//    [self.locationManage stopUpdatingLocation];
}
@end

这里面有几个数据结构一定要搞清楚,CoreLocation对象里面有Coordinate(结构体,含有纬度和经度)属性,可以从这里面获取精确的位置。

代理方法

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations必须是在管理者调用了<p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; font-family: Menlo;">startUpdatingLocation后才会在位置变化后调用,并且调用的频率非常高。一般情况下谨慎使用。</p>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值