IOS开发 使用地图 MapKit

因为有个项目要在地图中显示位置,所以用到了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





  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值