当前位置:编程学习 > wap >>

iOS开发那些事-iOS6苹果地图实用开发

在iOS 6之后,不再使用谷歌地图了,而是使用苹果自己的地图,但是API编程接口没有太大的变化。开发人员不需要再学习很多新东西就能开发地图应用,这是负责任的做法。因此本节介绍的内容也同样适用于iOS5上运行地图应用开发。


iOS应用程序中使用Map Kit API开发地图应用程序。 其核心是MKMapView类使用。我们可以设置地图显示方式、控制地图,可以在地图上添加标注。

显示地图

在Map Kit API中显示地图的视图是MKMapView,它的委托协议是MKMapViewDelegate。Map Kit API使用需要导入MapKit框架。

下面我们通过一个案例介绍一下面我们介绍一下Map Kit API的使用。这个案例在“输入查询地点关键字”文本框中输入关键字,点击“查询”按钮,先进行地理信息编码查询,查询获得地标信息后,在地图上标注出来。

 

首先添加框架MapKit.framework。然后在工程中打开MainStoryboard.storyboard的IB设计,从对象库中拖拽Map View到设计画面中。

 

调整它的位置和大小使得Map View尽可能填出画面下面的空白部分,然后为Map View定义输入出口。下面我们看看主视图控制器ViewController.h代码:

[cpp]
#import <UIKit/UIKit.h>  
 
#import <MapKit/MapKit.h>  
 
#import ”MapLocation.h”  
 
  
 
@interface ViewController : UIViewController <MKMapViewDelegate> 
 
  
 
@property (weak, nonatomic) IBOutlet UITextField *txtQueryKey; 
 
  
 
@property (weak, nonatomic) IBOutlet MKMapView *mapView; 
 
  
 
- (IBAction)geocodeQuery:(id)sender; 
 
  
 
@end 

#import <UIKit/UIKit.h>

#import <MapKit/MapKit.h>

#import ”MapLocation.h”

 

@interface ViewController : UIViewController <MKMapViewDelegate>

 

@property (weak, nonatomic) IBOutlet UITextField *txtQueryKey;

 

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

 

- (IBAction)geocodeQuery:(id)sender;

 

@end


由于使用Map Kit API,需要引入头文件<MapKit/MapKit.h>,头文件”MapLocation.h”是我们自己定义的描述地图标注点类。在定义ViewController时,还需要声明实现MKMapViewDelegate协议。txtQueryKey属性是查询关键字文本框,mapView属性是MKMapView类型,它与画面对应。点击查询按钮触发geocodeQuery:方法,它处理查询并在地图上做标注。

下面我看看ViewController.m的viewDidLoad方法代码:

[cpp]
- (void)viewDidLoad 
 

 
[super viewDidLoad]; 
 
_mapView.mapType = MKMapTypeStandard; 
 
_mapView.delegate = self; 
 

- (void)viewDidLoad

{

[super viewDidLoad];

_mapView.mapType = MKMapTypeStandard;

_mapView.delegate = self;

}


在viewDidLoad方法中设置地图的类型,它的类型有3种:

MKMapTypeStandard 标注地图类型。

MKMapTypeSatellite 卫星地图类型。在卫星地图中没有街道名称等信息;

MKMapTypeHybrid 混合地图类型。在混合地图是在卫星地图上标注出街道等信息;

 

viewDidLoad方法的_mapView.delegate = self语句是当前视图控制器赋值给地图视图的delegate属性,这样地图视图在需要的时候就会回调ViewController,如果失败,回调下面的失败方法:

[cpp]
- (void)mapViewDidFailLoadingMap:(MKMapView *)theMapView withError:(NSError *)error { 
 
NSLog(@”error : %@”,[error description]); 
 

- (void)mapViewDidFailLoadingMap:(MKMapView *)theMapView withError:(NSError *)error {

NSLog(@”error : %@”,[error description]);

}


跟踪用户位置变化

MapKit提供了跟踪用户位置和方向变化的API,我们不用自己编写定位服务代码。开启地图视图的showsUserLocation属性,并设置方法setUserTrackingMode:就可以了,代码如下:

[cpp]
- (void)viewDidLoad 
 

 
[super viewDidLoad]; 
 
if ([CLLocationManager locationServicesEnabled]) 
 

 
_mapView.mapType = MKMapTypeStandard; 
 
_mapView.delegate = self; 
 
_mapView.showsUserLocation = YES; 
 
[_mapView setUserTrackingMode:MKUserTrackingModeFollow animated:YES]; 
 

 

- (void)viewDidLoad

{

[super viewDidLoad];

if ([CLLocationManager locationServicesEnabled])

{

_mapView.mapType = MKMapTypeStandard;

_mapView.delegate = self;

_mapView.showsUserLocation = YES;

[_mapView setUserTrackingMode:MKUserTrackingModeFollow animated:YES];

}

}


其中代码_mapView.showsUserLocation = YES,允许跟踪显示用户位置信息。在iOS设备中显示用户位置方式是一个发亮的蓝色小圆点。

 

补充:移动开发 , IOS ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,