美文网首页
iOS 定位&地图

iOS 定位&地图

作者: WeekDiffculty | 来源:发表于2016-08-26 22:31 被阅读0次

获取当前位置

  • Corelocation获取位置满足一下2个要求:
    • 得到用户的许可
    • 确保设备启用定位服务

使用

  • 1 . 倒入框架 MapKit Corelocatin
  • 2 . 倒入Corelocation Mapkit 头文件
  • 3 . 向用户发出请求
    • 1 . 在infoplist 中添加key NSLocationWhenInUseUsageDescription 或NSLocationAlwaysUsageDescription申请时给用户提示的文字作为key的内容。
    • 2 . 在代码中申请授权

self.locationManager = [[CLLocationManager alloc]init];
self.locationManager.delegate = self;
if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusNotDetermined) {
[self.locationManager requestAlwaysAuthorization];//总是
//[self.locationManager requestWhenInUseAuthorization];
}

       - 3 .用户许可和响应

//申请授权,如果用户没有选择过
if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusNotDetermined) {
//当应用在前台时授权
// [self.locationManager requestAlwaysAuthorization];//总是
[self.locationManager requestWhenInUseAuthorization];
}

       - 4 .一些配置
      ```
      //更新频率
    self.locationManager.distanceFilter = 10.0f;
    //精度
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    //定位服务是否可用
    if ([CLLocationManager locationServicesEnabled]) {
        [self.locationManager startUpdatingLocation];//开始定位
    }else{
        //暂停定位
    }
    //设置mapView 的delegate
    self.mapVIew.delegate = self;
    //修改地图的类型
     //[self.mapVIew setMapType:MKMapTypeHybrid];
   -  5 . 授权改变的代理方法
//授权状态的改变
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status{
    NSLog(@"%d",status);
}
   - 6 . 定位到位置时的代理方法
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations{
    NSLog(@"locations%@",locations);
    CLLocation *location = locations.lastObject;
    //如果水平精度过差,就放弃该店
    if (location.horizontalAccuracy>kCLLocationAccuracyHundredMeters) {
        return;
    }
    CLLocationCoordinate2D coordinate = location.coordinate;
    //把地图调整到定位区域
    //定位到的点作为中心点
    //设定一个跨度
    MKCoordinateSpan pan = MKCoordinateSpanMake(0.05, 0.05);
    //构造区域
    MKCoordinateRegion region = MKCoordinateRegionMake(coordinate, pan);
    //将地图调整到该区域
    [self.mapVIew setRegion:region animated:YES];
//    //添加一个系统标注点
//    MKPointAnnotation *annotation = [[MKPointAnnotation alloc]init];
//    annotation.coordinate = coordinate;
//    [self.mapVIew addAnnotation:annotation];
    //添加一个自定义的标注点
    QYAnnotation *annotation = [[QYAnnotation alloc]init];
    annotation.coordinate = coordinate;
    annotation.title = @"香港";
    annotation.subtittle = @"就在这";
    [self.mapVIew addAnnotation:annotation];
}
     - 自定义的 MKAnnotation 类  QYAnonotation
//  Copyright © 2016 qingyun. All rights reserved.
//
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
@interface QYAnnotation : NSObject<MKAnnotation>
//位置点方法
@property (nonatomic ) CLLocationCoordinate2D coordinate;
//标题和子标题
@property (nonatomic, copy) NSString *title;  //重写MkAnnotation的属性 不能写错
@property (nonatomic, copy) NSString *subtittle;
@end
   - 7 .发生错误时调用的代理方法
//发生错误的时候
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{
    NSLog(@"%@",error);
}
   - 8 .返回注释视图的代理方法
//返回注释的delegate
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{
    if ([annotation isKindOfClass:[QYAnnotation class]]) {//我门自己添加的
        //定义一个复用的字符串
        static NSString *indentifier = @"qingyun";
        //调用复用的方法
        MKAnnotationView *view = [mapView dequeueReusableAnnotationViewWithIdentifier:indentifier];
        if (!view) {
            view = [[MKAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:indentifier];
        }
        //绑定数据
        view.annotation = annotation;
        view.image = [UIImage imageNamed:@"iconpng"];
        view.centerOffset = CGPointMake(0, -15);
        view.canShowCallout = YES;//是否显示
        return view;
    }
    return nil;
}

Mapkit 的代理

#pragma  --mark mapViewdelegate
//区域改变的时候
- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated{
    NSLog(@"区域改变");
}
//将地图诠释推到详细的视图控制器时,告诉委托用户利用注释视图的一个辅助按钮。
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control{
    NSLog(@"%@",view);
}
Simulator Screen Shot Aug 26, 2016, 10.33.16 PM.png

相关文章

  • IOS地图定位导航

    title : IOS地图定位导航category : UI 地图定位导航 标签(空格分隔): IOS 概述 I...

  • iOS地图和定位

    iOS地图定位 本文发布在http://he8090.cn/2016/07/18/地图与定位/ 导入地图框架 1、...

  • 地图与定位

    OCiOS开发:地图与定位 - 李鴻耀 - 博客频道 - CSDN.NET iOS开发之地图-定位/...

  • iOS Mapkit的使用

    【iOS】Mapkit的使用:地图显示、定位、大头针、气泡等 标签:iOS地图mapkit 1.显示地图 (1)首...

  • iOS百度地图使用时的注意问题

    iOS使用百度地图时会发现,API本身不带定位功能的,定位是通过iOS系统自身的定位实现的,百度地图API只是封装...

  • iOS 地图定位-地图

    地图 准备工作 导入MapKit框架(iOS5之后不在需要程序员自己导入) 导入主头文件#import MapKi...

  • iOS 地图定位-定位

    定位 常用方法的介绍 CLLocationManager位置管理器,我们的有关于位置的方法和属性都是通过它来管理设...

  • iOS 地图

    iOS 地图 基础 一 :定位 在 iOS 程序开发中 地图功能是普遍存在的,刚好最近都在研究地图的功能。总结一下...

  • 获取定位信息

    ios官方定位插件 百度地图定位Cordova插件,支持Android,IOS,ionic 1x 2x 均可使用(...

  • 定位及耗电

    iOS-GPS定位基础知识 iOS -GPS定位服务和地图应用是两套完全不同的API iOS7 的四种定位服务-G...

网友评论

      本文标题:iOS 定位&地图

      本文链接:https://www.haomeiwen.com/subject/dtezsttx.html