一个关于定位的demo

首先要做的准备

  • 点击工程名,滑到最下面,点击+号,导入MapKit.framework系统库。
  • 如果你是iOS8及以上的版本,则需要在info.plist文件下加入以下的东西,最后面的字符大家可以随便打,这个无所谓的。

demo的详细做法

  1. 首先在storyboard里面拖一个MapView,通过autoLayout添加约束让他占满全屏。
  2. 在左下角拖一个button,设置他的普通图片和高亮时候的图片,这个button主要是为了使用户返回自己当前所在位置的。
  3. 将storyboard的两个控件拉到ViewController中,这样我们就可以得到他们的属性了。
  4. 如果大家有谁在模拟器中运行的话,等模拟器运行后点击上方的debug,进去之后点击location,选择第二个custom location在里面设置你当前位置的经纬度,因为模拟器是没办法定位的,他运行之后默认定位的是美国,所以大家需要改上面的东西,如果是真机,则不需要设置,直接就可以运行。
  5. 下面给大家展示demo做好之后的效果。

 

 

最后给大家奉上代码,注释写的挺详细的,相信你们可以看懂的。

#import "ViewController.h"
#import <MapKit/MapKit.h>

@interface ViewController ()<MKMapViewDelegate>

@property (weak, nonatomic) IBOutlet MKMapView *mapView;

@property (nonatomic, strong) CLLocationManager * manager;

@end

@implementation ViewController

//返回用户当前位置
- (IBAction)currentLocation:(id)sender {
    
    //获取用户所在的经纬度
    CLLocationCoordinate2D coordinate = self.mapView.userLocation.location.coordinate;
    
    MKCoordinateSpan span = MKCoordinateSpanMake(0.1, 0.1);

    MKCoordinateRegion region = MKCoordinateRegionMake(coordinate, span);
    
    //定位可见区域
    [self.mapView setRegion:region animated:YES];
}

- (CLLocationManager *)manager {
    
    if (!_manager) {
        
        //创建定位管理器
        CLLocationManager * locationManager = [[CLLocationManager alloc] init];
        //定位的精确度,精确度越高越耗电
        locationManager.desiredAccuracy = kCLLocationAccuracyBest;
        //定位的更新频率,单位为米
        locationManager.distanceFilter = 5;
        
        _manager = locationManager;
    }
    return _manager;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    
    //iOS8的做法
    
    if (![CLLocationManager locationServicesEnabled]) {
        
        NSLog(@"提示用户打开定位服务");
    } else {
        
        //获取当前定位的状态
        CLAuthorizationStatus status = [CLLocationManager authorizationStatus];
        //如果定位状态为未打开
        if (status == kCLAuthorizationStatusNotDetermined) {
            
            //requestWhenInUseAuthorization  前端定位
            //requestAlwaysAuthorization 前端和后台定位
            
            [self.manager requestWhenInUseAuthorization];
            //            [self.manager requestAlwaysAuthorization];
            
        }
    }
    
    
    self.mapView.delegate = self;
    
    /*
     
     MKMapTypeStandard = 0,//标准地图2D
     MKMapTypeSatellite, //卫星地图
     MKMapTypeHybrid, //混合地图
     
     */
    
    //设置地图类型
    self.mapView.mapType = MKMapTypeStandard;
    
    //显示用户位置
    self.mapView.showsUserLocation = YES;
    
    
}

//当用户位置改变时,调用。
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation {
    
    //获取当前用户的经纬度
     CLLocationCoordinate2D coordinate = userLocation.location.coordinate;
    
    //将当前屏幕设为中心点
//    [mapView setCenterCoordinate:coordinate animated:YES];
    
    /*
     
     typedef struct {
     CLLocationDegrees latitudeDelta; 纬度跨度
     CLLocationDegrees longitudeDelta; 经度跨度
     } MKCoordinateSpan;
     
     typedef struct {
     CLLocationCoordinate2D center;  中心
     MKCoordinateSpan span;
     } MKCoordinateRegion;

     
     */
    
    //设置屏幕显示区域的经纬跨度
    MKCoordinateSpan span = MKCoordinateSpanMake(0.1, 0.1);
    
    MKCoordinateRegion region = MKCoordinateRegionMake(coordinate, span);
    
    //定位可见区域
    [mapView setRegion:region animated:YES];
}

 

posted @ 2016-03-29 20:28  往事亦如风  阅读(647)  评论(0编辑  收藏  举报