MapKit
加入MapKit library
首先得在项目中加入MapKit,如图
MapView
先增加一个ViewController,我这里用的storyboard,这个玩意还是挺好用的,比以前用xib好多了。
然后拖一个mapview上去,如:
给新增加的ViewController绑定一个class。首先得增加一个class,从uiViewController继承下来。这个很简单,如图
把新增加的ViewController绑定到这个class,也很easy,发现Xcode还是挺牛的。就是在右边Identity inspector里面的custom class里面改成新增加的类,原来是UIViewController。
然后给map view控件绑定一个变量,类型是MKMapView
然后就初始化mapview,显示。代码如下:
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- // Do any additional setup after loading the view.
- _mapView.mapType = MKMapTypeStandard;//标准模式
- _mapView.showsUserLocation = YES;//显示自己
- _mapView.zoomEnabled = YES;//支持缩放
- CLLocationCoordinate2D pos = {39.931203, 116.395573};//找个坐标,我是用百度坐标抓取弄的。http://api.map.baidu.com/lbsapi/getpoint/
- MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(pos,2000, 2000);//以pos为中心,显示2000米
- MKCoordinateRegion adjustedRegion = [_mapView regionThatFits:viewRegion];//适配map view的尺寸
- [_mapView setRegion:adjustedRegion animated:YES];
- }
我这里使用百度坐标,找了个坐标(直接搜索“百度 坐标”),然后在我们自己的地图里显示。这样运行一下就可以看到:
Map view delegate 回调
可以实现协议MKMapViewDelegate, 这样就会有几个回调。
- - (void) mapViewWillStartLoadingMap:(MKMapView *)mapView//开始从服务器获取地图数据
- {
- }
- -(void)mapViewDidFinishLoadingMap:(MKMapView *)mapView//获取数据结束
- {
- }
- - (void) mapViewDidFailLoadingMap:(MKMapView *)mapView withError:(NSError *)error//获取数据失败了。
- {
- }
获取设备当前位置并且在地图中显示
增加一个按钮,点击这个按钮,将显示设备当前位置。点击上面的按钮将显示某个固定位置。
CLLocationManager,首先使用CLLocationManager来获取设备的当前位置。
代码也是很简单
- //获得自己的当前的位置信息
- - (void) getCurPosition
- {
- //开始探测自己的位置
- if (locationManager==nil)
- {
- locationManager =[[CLLocationManager alloc] init];
- }
- if ([CLLocationManager locationServicesEnabled])
- {
- locationManager.delegate=self;
- locationManager.desiredAccuracy=kCLLocationAccuracyBest;
- locationManager.distanceFilter=10.0f;
- [locationManager startUpdatingLocation];
- }
- }
然后实现回调函数
- #pragma mark -- CLLocationManagerDelegate
- - (void) locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
- {
- if ([locations count] > 0) {
- CLLocation* loc = [locations objectAtIndex:0];
- CLLocationCoordinate2D pos = [loc coordinate];
- NSLog(@"locationManager, longitude: %f, latitude: %f", pos.longitude, pos.latitude);
- if (show == NO) {
- MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(pos,2000, 2000);//以pos为中心,显示2000米
- MKCoordinateRegion adjustedRegion = [_mapView regionThatFits:viewRegion];//适配map view的尺寸
- [_mapView setRegion:adjustedRegion animated:YES];
- show = YES;
- }
- }
- }
当设备位置变化时,这个函数会被调用。这样我们就可以根据位置来做一些事情了。这个例子里就在第一次获取位置的时候更新一下地图显示。以设备当前位置为中心,显示2000米。
完了。贴一下mapview所在的controller代码:
- //
- // KMapViewController.m
- // MapDemo
- //
- // Created by Kevin on 14-2-10.
- // Copyright (c) 2014年 Kevin. All rights reserved.
- //
- #import "KMapViewController.h"
- @interface KMapViewController ()
- @end
- @implementation KMapViewController
- - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
- {
- self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
- if (self) {
- // Custom initialization
- }
- return self;
- }
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- // Do any additional setup after loading the view.
- show = NO;
- _mapView.mapType = MKMapTypeStandard;//标准模式
- _mapView.showsUserLocation = YES;//显示自己
- _mapView.delegate = self;
- _mapView.zoomEnabled = YES;//支持缩放
- NSString* i = self.Index;
- if([i isEqualToString:@"1"])
- {
- CLLocationCoordinate2D pos = {39.931203, 116.395573};//找个坐标,我是用百度坐标抓取弄的。http://api.map.baidu.com/lbsapi/getpoint/
- MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(pos,2000, 2000);//以pos为中心,显示2000米
- MKCoordinateRegion adjustedRegion = [_mapView regionThatFits:viewRegion];//适配map view的尺寸
- [_mapView setRegion:adjustedRegion animated:YES];
- }
- else
- {
- [self getCurPosition];
- }
- }
- - (void)didReceiveMemoryWarning
- {
- [super didReceiveMemoryWarning];
- // Dispose of any resources that can be recreated.
- }
- - (void) dealloc
- {
- // [super dealloc];
- }
- //获得自己的当前的位置信息
- - (void) getCurPosition
- {
- //开始探测自己的位置
- if (locationManager==nil)
- {
- locationManager =[[CLLocationManager alloc] init];
- }
- if ([CLLocationManager locationServicesEnabled])
- {
- locationManager.delegate=self;
- locationManager.desiredAccuracy=kCLLocationAccuracyBest;
- locationManager.distanceFilter=10.0f;
- [locationManager startUpdatingLocation];
- }
- }
- #pragma mark -- MPMapViewDelegate
- - (void) mapViewWillStartLoadingMap:(MKMapView *)mapView
- {
- }
- -(void)mapViewDidFinishLoadingMap:(MKMapView *)mapView
- {
- }
- - (void) mapViewDidFailLoadingMap:(MKMapView *)mapView withError:(NSError *)error
- {
- }
- #pragma mark -- CLLocationManagerDelegate
- - (void) locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
- {
- if ([locations count] > 0) {
- CLLocation* loc = [locations objectAtIndex:0];
- CLLocationCoordinate2D pos = [loc coordinate];
- NSLog(@"locationManager, longitude: %f, latitude: %f", pos.longitude, pos.latitude);
- if (show == NO) {
- MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(pos,2000, 2000);//以pos为中心,显示2000米
- MKCoordinateRegion adjustedRegion = [_mapView regionThatFits:viewRegion];//适配map view的尺寸
- [_mapView setRegion:adjustedRegion animated:YES];
- show = YES;
- }
- }
- }
- @end
posted on 2015-11-05 12:14 阿斯顿发-斯语iOS 阅读(100) 评论(0) 编辑 收藏 举报