博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
地图位置定位的封装开发
阅读量:7199 次
发布时间:2019-06-29

本文共 4317 字,大约阅读时间需要 14 分钟。

在很多的APP开发中经常使用到定位功能,对于这种常用的方法很有必要对其封装使用

话不多说直接上代码:使用到了单例设计模式:

`

import <Foundation/Foundation.h>

import <MapKit/MapKit.h>

@protocol PositionToolDelegate <NSObject>

@optional

/**

  • 位置改变时通知传值
    */
    • (void)noticePositionChanged;

@end

@interface PositionTool : NSObject<CLLocationManagerDelegate,PositionToolDelegate> {

CLLocationManager _locationManager;
NSMutableArray
_locationArr ;
}

@property (nonatomic,assign) CLLocationDegrees latitude;//经度

@property (nonatomic,assign) CLLocationDegrees longtitude;//纬度
/**

  • 位置地区信息
    /
    @property (nonatomic,strong) NSString
    positionName;
    @property (nonatomic,strong) NSString province;
    @property (nonatomic,strong) NSString
    city;
    @property (nonatomic,strong) NSString *area;

//@property (nonatomic,strong) id<PositionToolDelegate> delegate;

/**

  • 添加回调代理方法
    */
    • (void)addDelegate:(id)delegate;
      /**
  • 移除回调代理方法
    */
    • (void)removeDelegate:(id)delegate;
      /**
  • 获取位置中心信息
    */
    • (CLLocationCoordinate2D)getCenter;
      /**
  • 更新位置
    */
    • (void)updateLocation;
  • (void)getLocation;

/**

  • 单例初始化方法
    */
    • (PositionTool )shareInfo;
      /*
  • 释放单例信息
    */
    • (void)freeInfo;

@end

.m方法的实现

  • (PositionTool*)shareInfo {
    @synchronized(self) {
    if (_positionTool == nil) {      _positionTool=[[PositionTool alloc] init];      _positionTool.positionName=@"没有定位"; }
    }
    return _positionTool;
    }
    /**
    • 释放单例实现
      */
  • (void)freeInfo {

    if (_positionTool) {

    _positionTool = nil;

    }

    }

  • (id)init {

    self=[super init];
    if (self) {

    _locationArr=[[NSMutableArray alloc] init];

    }

    return self;
    }
    /**

    • 代理回调添加和删除实现方法
      *
    • @param delegate 代理
      */
  • (void)addDelegate:(id<PositionToolDelegate>)delegate {
    [_locationArr addObject:delegate];
    }
  • (void)removeDelegate:(id)delegate {
    [_locationArr removeObject:delegate];
    }

/**

  • 获取位置信息
    */
  • (CLLocationCoordinate2D)getCenter {
    return [_locationManager location].coordinate;
    }
  • (void)updateLocation {
    [_locationManager startUpdatingLocation];
    }

    pragma mark ========= 获取位置 ==========

  • (void)getLocation {
    if ([CLLocationManager locationServicesEnabled]) {
    _locationManager=[[CLLocationManager alloc] init];  _locationManager.delegate=self;  //精度最高,耗电最大  _locationManager.desiredAccuracy=kCLLocationAccuracyBest;  _locationManager.distanceFilter = 200; [_locationManager startUpdatingLocation]; if ([[[UIDevice currentDevice] systemVersion] floatValue] >=8.0) { [_locationManager requestAlwaysAuthorization]; }
    }
    }

pragma mark - 调用地图代理方法

  • (void)locationManager:(CLLocationManager )manager didFailWithError:(NSError )error {
    NSLog(@"%@",error);
    }
  • (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {

    switch (status) {

    case kCLAuthorizationStatusNotDetermined: if ([_locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) { [_locationManager requestAlwaysAuthorization]; } break; default: break;

    }

    }
    //获取经纬度

  • (void)locationManager:(CLLocationManager )manager didUpdateLocations:(NSArray<CLLocation > )locations {

    CLLocation
    newLocation=locations[0];
    //CLLocationCoordinate2D oldCoordinate=newLocation.coordinate;
    //NSLog(@"旧的经度:%f,旧的纬度:%f",oldCoordinate.longitude,oldCoordinate.latitude);
    //NSLog(@"新的经度:%f,新的纬度:%f",newLocation.coordinate.longitude,newLocation.coordinate.latitude);
    self.latitude=newLocation.coordinate.latitude;
    self.longtitude=newLocation.coordinate.longitude;
    [manager stopUpdatingLocation];

    CLGeocoder geocoder=[[CLGeocoder alloc] init];

    [geocoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray<CLPlacemark > _Nullable placemarks, NSError _Nullable error) {

    for (CLPlacemark *place in placemarks) {      self.positionName=place.name; self.city=place.locality; self.area=place.subLocality; NSLog(@"%@",self.area); } for (id
    delegate in _locationArr) { if (delegate && [delegate respondsToSelector:@selector(noticePositionChanged)]) { [delegate noticePositionChanged]; } }

    }];

    for (id<PositionToolDelegate> delegate in _locationArr) {

    if (delegate && [delegate respondsToSelector:@selector(noticePositionChanged)]) {      [delegate noticePositionChanged];  }

    }

    }

-(void)locationManager:(CLLocationManager )manager didUpdateToLocation:(CLLocation )newLocation fromLocation:(CLLocation *)oldLocation {

NSLog(@"aaa%@", @"ok");
}
`
使用时候直接在使用到的地方加入
[[PositionTool shareInfo] getLocation];

文/Mikko(简书作者)
原文链接:http://www.jianshu.com/p/39228fd5da68
著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。

转载于:https://www.cnblogs.com/dengniqukanhai/p/5632546.html

你可能感兴趣的文章
Converter of C#&VB.NET
查看>>
修改ASPCMS升级扩展功能
查看>>
模拟HTTP请求的返回&shell写cgi
查看>>
问题tips
查看>>
如何给NSMutableArray排序
查看>>
CF991C Candies 二分 第十五
查看>>
查找两个有序数组中的第K个元素(find kth smallest element in 2 sorted arrays)
查看>>
sqlmap注入之tamper绕过WAF防火墙过滤
查看>>
如何用Tacker将NFV带入OpenStack?
查看>>
快速排序简明教程
查看>>
【转】Unity3D学习日记(二)使用UGUI制作虚拟摇杆控制摄像机
查看>>
【BZOJ3110】【Zjoi2013】K大数查询 - 2
查看>>
zabbix安装源
查看>>
java例程练习(多线程的两种创建方式)
查看>>
随题而学(二)多维数组转一维数组
查看>>
创建Dynamic Web Project工程
查看>>
Confirm the Ending
查看>>
day5 python全栈学习笔记
查看>>
ntp网络时间服务搭建
查看>>
php 正则 常用基础
查看>>