MKMapView的使用

来源:互联网 发布:centos 6.7 升级内核 编辑:程序博客网 时间:2024/04/28 14:18


http://blog.csdn.net/swingpyzf/article/details/16801787


一、使用MKMapView 定位

二、在地图上通过经纬度标注(大头针)

项目源码:http://download.csdn.net/detail/swingpyzf/6569785


最终效果图:

打开应用程序会自动定位并且放大到自己的位置,然后在我的位置上显示自己的经纬度,通过填写经纬度可以放置大头针标注。


一、获取自身的位置和经纬度


1、新建项目,将ios的提供位置服务和地图服务的库加入到项目中 点项目名->Build Phases点开Link Binary With Libraries

将CoreLocation和MapKit两个库加入到项目中,前者是ios的位置服务库,后者是操作MKMapView的库


2、新建UIViewController 控件布局和设置好关系属性。导入<CoreLocation/CoreLocation.h>和<MapKit/MapKit.h>

还要让控制器类实现MKMapViewDelegate协议

[objc] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. #import <UIKit/UIKit.h>  
  2. #import <MapKit/MapKit.h>  
  3. #import <CoreLocation/CoreLocation.h>  
  4.   
  5. @interface MainViewController : UIViewController<MKMapViewDelegate,UITextFieldDelegate>  
  6.   
  7. //经度  
  8. @property (weak, nonatomic) IBOutlet UITextField *longitudeText;  
  9. //纬度  
  10. @property (weak, nonatomic) IBOutlet UITextField *latitudeText;  
  11. //地图  
  12. @property (weak, nonatomic) IBOutlet MKMapView *mapView;  
  13. //自己经度  
  14. @property (weak, nonatomic) IBOutlet UILabel *longitudeLabel;  
  15. //自己纬度  
  16. @property (weak, nonatomic) IBOutlet UILabel *latitudeLabel;  
  17.   
  18. //放置标注Button  
  19. - (IBAction)annotationAction:(id)sender;  
  20.   
  21. @end  


3、MKMapView可以通过 setShowsUserLocation:YES这个方法来获取自己的位置,并且当地图更新自己的位置后会调用

-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation的一个协议的委托方法,我要在这个方法里面试实现当地图位置更新后/获取自己位置后对地图进行放大。

首先在viewDidLoad方法里对让地图调用setShowsUserLocation方法来实现地图的定位,并且设置MapView的委托类。

[objc] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. - (void)viewDidLoad  
  2. {  
  3.     //设置MapView的委托为自己  
  4.     [self.mapView setDelegate:self];  
  5.       
  6.     //标注自身位置  
  7.     [self.mapView setShowsUserLocation:YES];  
  8.       
  9.     [super viewDidLoad];  
  10. }  


然后实现-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation方法:

[objc] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. //MapView委托方法,当定位自身时调用  
  2. -(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation{  
  3.     CLLocationCoordinate2D loc = [userLocation coordinate];      
  4.     //放大地图到自身的经纬度位置。  
  5.     MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(loc, 250250);  
  6.     [self.mapView setRegion:region animated:YES];  
  7. }  

CLLocationCoordinate2D 是一个结构体记录经纬度,通过地图的获取的location来给其赋值。


运行一下程序地图载入的同时获得自身的位置,并且会自动放大到你所在的位置。



调试位置:

模拟器在运行的时候,可以自定义的设置其自身所在的位置



4、获取自身的经纬度显示在两个label上,还是在-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation方法里面实现,通过MKUserLocation这个类里面有经度和纬度的属性,直接拿出来显示在label上

[objc] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. //MapView委托方法,当定位自身时调用  
  2. -(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation{  
  3.     CLLocationCoordinate2D loc = [userLocation coordinate];  
  4.     //显示到label上  
  5.     self.longitudeLabel.text = [NSString stringWithFormat:@"%f",loc.longitude];  
  6.       
  7.     self.latitudeLabel.text = [NSString stringWithFormat:@"%f",loc.latitude];  
  8.       
  9.     //放大地图到自身的经纬度位置。  
  10.     MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(loc, 250250);  
  11.     [self.mapView setRegion:region animated:YES];  
  12. }  



二、在地图上加上标注(大头针)


1、要给地图添加标注,就要自己实现一个标注类,这个类必须实现MKAnnotation协议,MKAnnotation有个必须要定义的属性

CLLocationCoordinate2D coordinate;

新建一个 MyPoint类继承自NSObject类并且实现<MKAnnotation>协议然后定义下列属性:

[objc] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. #import <Foundation/Foundation.h>  
  2. #import <MapKit/MapKit.h>  
  3.   
  4.   
  5. @interface MyPoint : NSObject <MKAnnotation>  
  6.   
  7. //实现MKAnnotation协议必须要定义这个属性  
  8. @property (nonatomic,readonly) CLLocationCoordinate2D coordinate;  
  9. //标题  
  10. @property (nonatomic,copyNSString *title;  
  11.   
  12. //初始化方法  
  13. -(id)initWithCoordinate:(CLLocationCoordinate2D)c andTitle:(NSString*)t;  
  14.   
  15. @end  


在.m文件里实现init方法

[objc] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. #import "MyPoint.h"  
  2.   
  3. @implementation MyPoint  
  4.   
  5. -(id)initWithCoordinate:(CLLocationCoordinate2D)c andTitle:(NSString *)t{  
  6.     self = [super init];  
  7.     if(self){  
  8.         _coordinate = c;  
  9.         _title = t;  
  10.     }  
  11.     return self;  
  12. }  
  13.   
  14. @end  


以上就是一个简单的标注类了。


2、回到ViewController类中在button按钮关联的- (IBAction)annotationAction:(id)sender方法中实现标注:

首先通过UITextField所填写的经纬度创建一个CLLocation类,然后赋值给CLLocationCoordinate2D:

[objc] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. //创建CLLocation 设置经纬度  
  2.     CLLocation *loc = [[CLLocation alloc]initWithLatitude:[[self.latitudeText text] floatValue] longitude:[[self.longitudeText text] floatValue]];  
  3.     CLLocationCoordinate2D coord = [loc coordinate];  


创建MyPoint实例最后调用MKMapView的addAnnotation方法将MyPoint的实例加入到标注中:- (IBAction)annotationAction:(id)sender中的实现:

[objc] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. //放置标注  
  2. - (IBAction)annotationAction:(id)sender {  
  3.     //创建CLLocation 设置经纬度  
  4.     CLLocation *loc = [[CLLocation alloc]initWithLatitude:[[self.latitudeText text] floatValue] longitude:[[self.longitudeText text] floatValue]];  
  5.     CLLocationCoordinate2D coord = [loc coordinate];  
  6.     //创建标题  
  7.     NSString *titile = [NSString stringWithFormat:@"%f,%f",coord.latitude,coord.longitude];  
  8.     MyPoint *myPoint = [[MyPoint alloc] initWithCoordinate:coord andTitle:titile];  
  9.     //添加标注  
  10.     [self.mapView addAnnotation:myPoint];  
  11.       
  12.     //放大到标注的位置  
  13.     MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(coord, 250250);  
  14.     [self.mapView setRegion:region animated:YES];  
  15. }  


另外还可以使用[self.mapView addAnnotations:NSArray]这个方法,这个方法的参数是一个数组对象,可以同时给地图上添加多个标注。


3、最后运行程序填入经纬度,就能给地图加上标注了。



0 0
原创粉丝点击