浅谈一下地图组件的使用

      这几天开始找工作,每个公司做得产品也各不相同.有一个公司做地图的.说起地图,很长时间没有去研究和学习了,都有点儿淡忘了,所以趁着现在空闲,赶紧又给抓起来,捂一捂.也算是温故而知新.

     首先,讲讲定位.要定位,首先需要导入CoreLocation.framework库.并且在.h文件中签订协议,和声明定位管理器变量.

      @interface RootViewController : UIViewController <CLLocationManagerDelegate>{

       CLLocationManager *gpsManager; // 定位管理器

       UILabel *gpsLabel;

}

    接着在我们实现代码的过程中,我们可以先在view上添加两个按钮,一个控制开始定位,一个控制停止定位.并且添加一个label来显示定位的经纬度.

- (void)viewDidLoad

{

    [super viewDidLoad];

// Do any additional setup after loading the view.

    // 加一个打开定位开关

    UIBarButtonItem *startItm = [[UIBarButtonItem alloc] initWithTitle:@"开始定位" style:UIBarButtonItemStylePlain target:self action:@selector(startLocation)];

    self.navigationItem.rightBarButtonItem = startItm;

    

    UIBarButtonItem *stopItm = [[UIBarButtonItem alloc] initWithTitle:@"停止定位" style:UIBarButtonItemStylePlain target:self action:@selector(stopLocation)];

    self.navigationItem.leftBarButtonItem = stopItm;

    gpsLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 100, 320, 40)];

    gpsLabel.backgroundColor = [UIColor grayColor];

    gpsLabel.text = @"还没有经纬度";

    [self.view addSubview:gpsLabel];

}

接下来我们可以继续完成开始实现定位方法.

- (void) startLocation {

    // 1. 创建定位管理对象

    gpsManager = [[CLLocationManager alloc] init];

    // 2. 设置代理

    gpsManager.delegate = self;

    // 3. 设置定位精度 

    gpsManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;

    // 4. 判断是否gps已经打开了

    if ([CLLocationManager locationServicesEnabled] == NO) {

        NSLog(@"你的gps还没有打开");

        return;

    }

    // 5. 开始定位

    [gpsManager startUpdatingLocation];

    

    // 打开罗盘定位,这个是罗盘定位,想要了解的可以自己研究,我只是提一下.

//    [gpsManager startUpdatingHeading];

    // 关闭罗盘

//    [gpsManager stopUpdatingHeading];

}

接着,当开始定位获取到新位置时就会调用下面的代理方法

#pragma mark - 获取定位数据代理函数

// 一旦这个函数调用了 那么表示位置发生变化了 最新位置在

// newLocation 之前的位置在oldLocation里面

- (void) locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {

    

}

// 和上面函数一样的。表示gps位置发生变化了 就调用这个函数 这个函数传过来一个位置数组

// 这个位置数组表示里面最新的所有位置

- (void) locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {

    for (CLLocation *oneLocation in locations) {

        NSLog(@"经纬度是 %f  %f",

              [oneLocation coordinate].latitude,

              [oneLocation coordinate].longitude);

        gpsLabel.text = [NSString stringWithFormat:@"%f,%f",

                         [oneLocation coordinate].latitude,

                         [oneLocation coordinate].longitude];

        }

           

}

- (void) locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading {

    

}

停止定位的方法里书写

 [gpsManager stopUpdatingLocation];就ok了.

当然定位功能只能在真机上进行测试,有条件的童鞋可以自己测试下.

至于地图显示,首先,我们需要导入MapKit.framework库.

声明一个地图显示空间变量

@interface RootViewController : UIViewController {

    MKMapView *myMapView; // 一个地图显示的控件

    // /xXXView

}

接着,我们要定制地图显示范围,和地图类型

- (void)viewDidLoad

{

    [super viewDidLoad];

// Do any additional setup after loading the view.

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

//    myMapView.mapType = MKMapTypeSatellite; // 卫星地图

    // MKMapTypeStandard标准交通地图

    

    // 设置地图初始的区域:

    MKCoordinateRegion region;

    region.center.longitude = 116.351167;

    region.center.latitude = 40.035907;

    region.span.latitudeDelta = 0.01;

    region.span.longitudeDelta = 0.01;

    [myMapView setRegion:region];

    

    // 显示自己的位置

    myMapView.showsUserLocation = YES;

    

    [self.view addSubview:myMapView];

    // 如何放大缩小呢 是按住屏幕Option键

    

    // 加一个按钮 当前位置

    UIButton *b = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    b.frame = CGRectMake(10, 400, 100, 40);

    [b setTitle:@"当前位置" forState:UIControlStateNormal];

    [b addTarget:self action:@selector(gotoMyselfLocation:) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:b];

}

 接着就是定位自己当前位置,并在地图上显示

- (void) gotoMyselfLocation:(UIButton *)b {

    MKCoordinateRegion region;

    // 经纬度需要通过gps取得...通过地图取得当前位置坐标

    CLLocation *myselfLocation = myMapView.userLocation.location;

    

    region.center.longitude = [myselfLocation coordinate].longitude;

    region.center.latitude = [myselfLocation coordinate].latitude;

    region.span.latitudeDelta = 0.01;

    region.span.longitudeDelta = 0.01;

    [myMapView setRegion:region animated:YES];

}

 这个是地图的简单使用,当然当你定位到自己位置时,也需要在真机上才能测试.

 

posted on 2015-02-15 11:02  myselfxiaoxiao  阅读(248)  评论(0)    收藏  举报

导航