iOS开发 地图MKMapView小Demo

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

@interface ViewController ()<MKMapViewDelegate>
{
    MKMapView *_mapview;
    CLLocationManager *_manager;
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _manager = [[CLLocationManager alloc]init];
    [_manager requestAlwaysAuthorization];
    [_manager requestWhenInUseAuthorization];
    [_manager startUpdatingLocation];
    
    _mapview = [[MKMapView alloc]initWithFrame:self.view.bounds];
    _mapview.mapType = MKMapTypeStandard;
    /*
     MKMapTypeStandard 街景
     MKMapTypeSatellite 卫星
     MKMapTypeHybrid 混合
     */
    _mapview.zoomEnabled = YES;//允许被缩放
    _mapview.scrollEnabled = YES; //允许被拖动
    _mapview.showsUserLocation = YES; //展示用户位置
    _mapview.delegate = self; //打开代理
    [self.view addSubview:_mapview];
    
    
}
//协议方法,用户位置加载完毕
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
    //获取用户当前的位置
    CLLocation *location = userLocation.location;
    
    CLLocationCoordinate2D coordinate = location.coordinate; //用户的经纬度
    MKCoordinateSpan span = MKCoordinateSpanMake(0.1, 0.1);//指定地图缩放比率 0~1.0
    MKCoordinateRegion region = MKCoordinateRegionMake(coordinate, span);
    //让地图移到指定的位置并以对应的缩放比率显示地图
    [_mapview setRegion:region animated:YES];
    //大概意思就是region表示地图中的一块区域,它有两个字段一个是center,就是这块区域中心点的经纬度;另一个是span,大概意思就是span表示的是regoin的范围。它有两个字段一个是latitudeDelta,表示纬度范围,南纬和北纬加一起应该有180度,所以它的范围应该是大于0度,小于等于180度;另一个是longitudeDelta,表示经度范围,东经和西经加一起应该有360度,所以它的范围应该是大于0度,小于360度    
}

@end




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值