您的位置:首页 > 其它

使用MapKit框架(持续更新)

2014-05-15 09:54 211 查看
使用MapKit框架

//
//  RootViewController.m
//  CoreLocation
//
//  Copyright (c) 2014年 Y.X. All rights reserved.
//

#import "RootViewController.h"
#import <MapKit/MapKit.h>

@interface RootViewController ()<CLLocationManagerDelegate>

@property (nonatomic, strong) CLLocationManager *locationManager;

@end

@implementation RootViewController

- (void)viewDidLoad
{
[super viewDidLoad];

// 判断定位功能是否可以使用
if([CLLocationManager locationServicesEnabled])
{
// 初始化定位管理器
_locationManager = [[CLLocationManager alloc] init];
_locationManager.delegate = self;

// 开始定位
[_locationManager startUpdatingLocation];
}
else
{
NSLog(@"定位功能不可用");
}
}

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
// 获取坐标信息
CLLocation *newLocation = [locations lastObject];

// 定位结束
[manager stopUpdatingLocation];

// 初始化地图控件
MKMapView *mapView = [[MKMapView alloc] initWithFrame:self.view.bounds];

// 地图的类型
mapView.mapType = MKMapTypeStandard;

// 视图的宽度和高度将和父视图的宽度一起成比例变化
mapView.autoresizingMask = \
UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

// 显示用户所在位置(此处系统会询问你是否使用当前位置)
mapView.showsUserLocation = YES;

// 地图缩放级别
MKCoordinateSpan span = {0.02, 0.02};

// 被显示的区域
MKCoordinateRegion region = {newLocation.coordinate, span};

// 设置显示的区域
[mapView setRegion:region];

// 显示地图
[self.view addSubview:mapView];
}

- (void)locationManager:(CLLocationManager *)manager
didFailWithError:(NSError *)error
{
NSLog(@"%@", error);

// 定位结束
[manager stopUpdatingLocation];
}

@end


RootViewController.m



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: