MapKit显示用户的位置

//
//  ViewController.m
//  MapViewIntroduction
//
//  Created by hq on 16/5/17.
//  Copyright  2016年 hanqing. All rights reserved.
//

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

@interface ViewController () <MKMapViewDelegate,CLLocationManagerDelegate>

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

@property(nonatomic,strong) CLLocationManager *clm;

@end

@implementation ViewController

-(CLLocationManager *)clm{
 
    if (_clm==nil) {
        
        _clm=[[CLLocationManager alloc]init];
        
        if ([[UIDevice currentDevice].systemVersion floatValue] >=8.0) {
            [_clm requestAlwaysAuthorization];
        }
    }
    
    return _clm;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.mapView.delegate=self;
    
    self.mapView.mapType=MKMapTypeHybrid;
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

    [self clm];
    
    self.mapView.rotateEnabled=NO;
    
    //显示用户位置,需要请求权限
    self.mapView.showsUserLocation=YES;
    
    //该方法会自动放到到用户所在到区域,不用我们再缩放
    //self.mapView.userTrackingMode=MKUserTrackingModeFollowWithHeading;
    
}


//用户的位置发生了变化就会调用该方法
-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation{
    
    CLLocation *loc=userLocation.location;
    
    //将经纬度转换为我们的地点
    CLGeocoder *geo=[[CLGeocoder alloc]init];
    
    [geo reverseGeocodeLocation:loc completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
        
        //获取一个地标
        CLPlacemark *mark=[placemarks firstObject];
        
        //设置地理位置上边的标题
        userLocation.title=mark.administrativeArea;
        
        userLocation.subtitle=mark.locality;
        
    }];
    
    //设置地图显示中心为我们的坐标点,不能设置我们的放大范围
    //[self.mapView setCenterCoordinate:userLocation.location.coordinate];
    
    
    //设置中心点,同时设置放大的范围
    
    //后边的两个数不知道填啥,可以通过我们的代理方法regionDidChangeAnimated获取合适的值
    
    //使用该方法,就不能使用自动缩放到用户所在到位置了,只能通过self.mapView.showsUserLocation=YES;
    //该方法会自动放到到用户所在到区域,不用我们再缩放
    //self.mapView.userTrackingMode=MKUserTrackingModeFollowWithHeading;

    MKCoordinateSpan span=MKCoordinateSpanMake(0.005361, 0.004050);
    
    MKCoordinateRegion region=MKCoordinateRegionMake(userLocation.location.coordinate, span);
    
    [self.mapView setRegion:region animated:YES];
    
}

-(void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated{
    
    NSLog(@"%f--%f",mapView.region.span.latitudeDelta,mapView.region.span.longitudeDelta);
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

@end

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值