1:加入库CoreLocation.framework,MApKit.framework;

2:@property (nonatomic, strong) CLLocationManager *locationManager;

@property (nonatomic, strong) MKMapView *mapView;

@property (nonatomic, strong) CLLocation *checkinLocation;

@property (strong, nonatomic) NSString *currentLatitude; //纬度

@property (strong, nonatomic) NSString *currentLongitude; //经度

- (void)viewDidLoad {

    [super viewDidLoad];

    self.title=@"地图";

    [self initRightBarButton];

    [self setupLocationManager];

    [self setupMapView];

}

-(void)initRightBarButton{

    UIButton* _btnRight = [UIButton buttonWithType:UIButtonTypeCustom];

    [_btnRight setFrame:CGRectMake(0, 0, 39, 34)];

    UIImage *imgNormal = [UIImage imageNamed:@"gpsbtn_dianji"];

    UIImage *imgSelect = [UIImage imageNamed:@"gpsbtn"];

    [_btnRight setBackgroundImage:imgNormal forState:UIControlStateNormal];

    [_btnRight setBackgroundImage:imgSelect forState:UIControlStateSelected];

    _btnRight.showsTouchWhenHighlighted = YES;

    [_btnRight addTarget:self action:@selector(showUserLocation:) forControlEvents:UIControlEventTouchUpInside];

    UIBarButtonItem *buttonItem = [[UIBarButtonItem alloc] initWithCustomView:_btnRight];

    self.navigationItem.rightBarButtonItem = buttonItem;

}

- (void)setupLocationManager{

    _locationManager=[[CLLocationManager alloc]init];

    _locationManager.desiredAccuracy=kCLLocationAccuracyBestForNavigation;

    //每隔多少米定位一次(这里的设置为不论什么的移动)

    _locationManager.distanceFilter=kCLDistanceFilterNone;

    //iOS 8.0以后

    if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {

        [self.locationManager requestWhenInUseAuthorization];

        [self.locationManager startUpdatingLocation];

    }

}

- (void)setupMapView{

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

    [self.view addSubview:_mapView];

    _mapView.delegate=self;

    //用户位置追踪(用户位置追踪用于标记用户当前位置,此时会调用定位服务)

    _mapView.userTrackingMode=MKUserTrackingModeFollowWithHeading;

    _mapView.mapType=MKMapTypeStandard;

    [self initLocationData];

}

- (void)initLocationData{

    NSMutableArray *arr=[[NSMutableArray alloc]init];

    for (int i=0; i<1; i++) {

        CLLocationDegrees lat=[self.currentLatitude  doubleValue];

        CLLocationDegrees longi=[self.currentLongitude doubleValue];

        NSString *lacationName=@"当前位置";

        BasicMapAnnotation *anno=[[BasicMapAnnotation alloc]initWithLatitude:lat andLongitude:longi];

        anno.title=lacationName;

        anno.index=i;

        [arr addObject:anno];

    }

    [_mapView showAnnotations:arr animated:YES];

}

- (void)showUserLocation:(id)sender{

    MKCoordinateSpan span=MKCoordinateSpanMake(0.01, 0.02);

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

    [_mapView setRegion:region animated:YES];

}

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

    NSLog(@"纬度:%f 经度:%f",userLocation.location.coordinate.latitude,userLocation.location.coordinate.longitude);

    self.currentLatitude  = [NSString stringWithFormat:@"%.4f",userLocation.location.coordinate.latitude];

    self.currentLongitude = [NSString stringWithFormat:@"%.4f",userLocation.location.coordinate.longitude];

    [self initLocationData];

    //设置地图显示范围(假设不进行区域设置会自己主动显示区域范围并指定当前用户位置为地图中心点)

    //MKCoordinateSpan span=MKCoordinateSpanMake(0.01, 0.01);

    //MKCoordinateRegion region=MKCoordinateRegionMake(_mapView.region.center, span);

    //[_mapView setRegion:region animated:true];

}

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

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

        static NSString *key=@"AnnotationKey";

        MKAnnotationView *annotationView=[_mapView dequeueReusableAnnotationViewWithIdentifier:key];

        BasicMapAnnotation *aa=(BasicMapAnnotation *)annotation;

        if (!annotationView) {

            annotationView=[[MKAnnotationView alloc]initWithAnnotation:aa reuseIdentifier:key];

            CGRect frame=annotationView.frame;

            frame.size.width=14;

            frame.size.height=35;

            annotationView.frame=frame;

            annotationView.canShowCallout=YES;

            UIImageView*imageview=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"地图标注"]];

            imageview.frame=frame;

            [annotationView addSubview:imageview];

        }

        return annotationView;

    }

    return nil;

}

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

}

最新文章

  1. ReportService未指定 OverwriteDataSources
  2. Opencv角点检测
  3. Glossary
  4. Supervisor – 用于 Unix 系统的进程监控工具
  5. Java-Stack
  6. 初步理解Java的三大特性——封装、继承和多态
  7. linux笔记:linux常用命令-文件处理命令
  8. 【四】php字符串操作
  9. leetcode:Search for a Range(数组,二分查找)
  10. C#中Invoke和BeginInvoke的区别
  11. ArcObjects SDK(AE)10.1在vs2012安装的方法
  12. 面向对象设计原则OO
  13. CentOS6安装Mysql5.7.10亲测
  14. javascript --- 事件托付
  15. 【BZOJ2002】弹飞绵羊(Link-Cut Tree)
  16. Java-IO之ByteArrayOutputStream
  17. mysql怎样配置ODBC数据源
  18. vue中的axios封装
  19. QWidget扩充父子关系
  20. 1.4 Genymotion模拟器安装

热门文章

  1. poj--1985--Cow Marathon(树的直径)
  2. ubuntu sublime text 2 破解版
  3. Maven 学习笔记(一)
  4. redis的windows版本下载地址及windows的客户端工具
  5. RTSP/RTP 媒体传输和控制协议
  6. 请求测试——Fiddler2工具(可以测试POST和Get)
  7. struts2学习之基础笔记4
  8. 如何使用pgpool failover_stream.sh自己控制选择指定的master节点
  9. oracle常规操作
  10. C语言-实现整数倒序输出