IOS开发之地图与定位

使用定位服务: 设置app有访问定位服务的权限,首先要在info.plist文件中添加以下liang'ti

 1.NSLocationWhenInUseUsageDescription 在使用应用期间

 2.NSLocationAlwaysUsageDescription  始终



#import "ViewController.h"

#import <CoreLocation/CoreLocation.h> //定位服务

#import <MapKit/MapKit.h> //地图界面


#import "WXAnnotation.h"

@interface ViewController ()<MKMapViewDelegate>

{

    CLLocationManager *_manager;

    MKMapView *_mapView;

}

@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];



   //接下来创建一个对象来管理相关的定位服务

  _manager = [[CLLocationManager alloc] init];


 //manager判断: 手机是否开启定位 / app是否有访问定位的权限

    //[CLLocationManager locationServicesEnabled]; //手机是否开启定位

    //[CLLocationManager authorizationStatus];  //app访问定位的权限的状态

    

    if(![CLLocationManager locationServicesEnabled] || [CLLocationManager authorizationStatus] != kCLAuthorizationStatusAuthorizedWhenInUse ){

    

        [_manager requestWhenInUseAuthorization];  //向用户请求访问定位服务

    }

    

  //创建地图

    [self createMapView];


  //创建大头针

    [self createAnnotationView];



}

- (void)createMapView

{

    //创建MapView

    _mapView = [[MKMapView alloc] initWithFrame:self.view.bounds];

    

    //地图的类型

    _mapView.mapType = MKMapTypeStandard;

    

    //显示用户当前的位置

    _mapView.showsUserLocation = YES;

    

    //是否定位(是否追终用户的位置)

    _mapView.userTrackingMode = MKUserTrackingModeFollow;

    

    _mapView.delegate = self;

    

    [self.view addSubview:_mapView];

    

}


//创建大头针

- (void)createAnnotationView

{

    //创建model对象

    WXAnnotation *annotation = [[WXAnnotation alloc] init];

    annotation.title = @"定位的位置";

    annotation.subtitle = @"杀杀杀!";

    

    //坐标的结构体

    CLLocationCoordinate2D coord = { 39.9927359964, 116.3965684639 };

    

    //大头针所在的坐标位置

    annotation.coordinate = coord;

    

    //把标注添加给mapView

    [_mapView addAnnotation:annotation];

    

}

//更新用户的位置(地图视图的代理方法)

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation

{

    //根据经纬度设置MapView

    //设置显示的区域范围,设置显示的精度

    

    //设置显示的精度(比列尺)

    MKCoordinateSpan span = MKCoordinateSpanMake(0.01, 0.01);

    

    //指定的范围

    MKCoordinateRegion region = MKCoordinateRegionMake(userLocation.location.coordinate,span);

    

    //移动mapView显示的位置

    [_mapView setRegion:region animated:YES];

}


//创建标注视图的代理方法

/**

 *  调用时机:annotation要添加到mapView,坐标出现在mapView,就调用代理方法添加一个大头针

 */

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation

{

    //当前用户的位置,不显示大头针

    if ([annotation isKindOfClass:[MKUserLocation class]]) {

        

        return nil;

    }

//    MKAnnotationView  不能直接使用 (自定义大头针视图,继承)

    //MKPinAnnotationView 能显示的大头针视图


    static NSString *iden = @"Annotation_view";

    

    //大头针视图

    MKPinAnnotationView *annotationView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:iden];

    if (annotationView == nil) {

        

        annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:iden];

        

        //大头针的颜色

        annotationView.pinColor = MKPinAnnotationColorPurple;

        

        //大头针从天而降的动画

        annotationView.animatesDrop = YES;

        

        //显示内容

        annotationView.canShowCallout = YES;

        

        //辅助视图

        annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];

    }

    //重新设置大头针视图的model,解决重用的问题

    annotationView.annotation = annotation;

    

    return annotationView;

}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值