码迷,mamicode.com
首页 > 其他好文 > 详细

关于原生地图定位的简单操作MapKit的使用

时间:2015-12-16 21:20:57      阅读:232      评论:0      收藏:0      [点我收藏+]

标签:

//

//  ViewController.m

//  Demo_1 MKMapView

//

//  Created by tareba on 15/12/16.

//  Copyright © 2015 tanada. All rights reserved.

//

 

#import "ViewController.h"

#import <MapKit/MapKit.h>

#import "TRAnnotation.h"

@interface ViewController ()<MKMapViewDelegate>

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

/**

 *  相同点:也要征求用户的同意

 */

@property (nonatomic,strong)CLLocationManager *manager;

 

- (IBAction)addAnnotation:(UIButton *)sender;

 

 

@end

 

@implementation ViewController

 

- (void)viewDidLoad {

    [super viewDidLoad];

  //初始化manger

    self.manager=[[CLLocationManager alloc]init];

    //询问 征求用户同意

    [self.manager requestWhenInUseAuthorization];

    //delegate

    self.mapView.delegate=self;

  

    //设置地图不行允许旋转

    self.mapView.rotateEnabled=NO;

    //设置地图的显示类型(卫星/彼岸准地图/混合)

    //self.mapView.mapType=MKMapTypeHybrid;

    //开始定位

    self.mapView.userTrackingMode=MKUserTrackingModeFollow;

}

#pragma mark - MapViewDelegate

//已经定位到用户的位置并且显示完

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

    NSLog(@"维度%f,精度%f",userLocation.location.coordinate.latitude,userLocation.location.coordinate.longitude);

    userLocation.title=@"用户位置";

    userLocation.subtitle=@"描述信息";

}

-  (void)mapView:(MKMapView *)mapView regionWillChangeAnimated:(BOOL)animated{

    NSLog(@"地图即将发生移动");

}

//确定地图已经发生移动

- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated{

    NSLog(@"地图已经发生移动");

}

 

- (IBAction)addAnnotation:(UIButton *)sender {

    //创建模型类

    //遵循协议

    //.h中声明三个属性1个必须,两个可选

    //添加到地图视图上,(必须遵循MKAnnotation协议)

    CLLocationDegrees latitude = 35.123+arc4random_uniform(10);//0~10

    CLLocationDegrees longitude = 116.125+arc4random_uniform(20);

    TRAnnotation *annotation=[TRAnnotation new];

    annotation.coordinate=CLLocationCoordinate2DMake(latitude, longitude);

    //设置地图视图的显示区域(大头针都显示到视图的中心位置)

    //跨度

    MKCoordinateSpan span=MKCoordinateSpanMake(0.5, 0.52);

    //中心位置 跨度

    MKCoordinateRegion region=MKCoordinateRegionMake(annotation.coordinate, span);

    [self.mapView setRegion:region animated:YES];

    [self.mapView addAnnotation:annotation];

}

 

@end

 

 

 

#import <Foundation/Foundation.h>

#import <MapKit/MapKit.h>

 

@interface TRAnnotation : NSObject<MKAnnotation>

@property (nonatomic, assign) CLLocationCoordinate2D coordinate;

 

@property (nonatomic, copy) NSString *title;

@property (nonatomic, copy) NSString *subtitle;

@end

关于原生地图定位的简单操作MapKit的使用

标签:

原文地址:http://www.cnblogs.com/zhao-jie-li/p/5052262.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!