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协议
- #import <UIKit/UIKit.h>
- #import <MapKit/MapKit.h>
- #import <CoreLocation/CoreLocation.h>
- @interface MainViewController : UIViewController<MKMapViewDelegate,UITextFieldDelegate>
- //经度
- @property (weak, nonatomic) IBOutlet UITextField *longitudeText;
- //纬度
- @property (weak, nonatomic) IBOutlet UITextField *latitudeText;
- //地图
- @property (weak, nonatomic) IBOutlet MKMapView *mapView;
- //自己经度
- @property (weak, nonatomic) IBOutlet UILabel *longitudeLabel;
- //自己纬度
- @property (weak, nonatomic) IBOutlet UILabel *latitudeLabel;
- //放置标注Button
- - (IBAction)annotationAction:(id)sender;
- @end
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
@interface MainViewController : UIViewController<MKMapViewDelegate,UITextFieldDelegate>
//经度
@property (weak, nonatomic) IBOutlet UITextField *longitudeText;
//纬度
@property (weak, nonatomic) IBOutlet UITextField *latitudeText;
//地图
@property (weak, nonatomic) IBOutlet MKMapView *mapView;
//自己经度
@property (weak, nonatomic) IBOutlet UILabel *longitudeLabel;
//自己纬度
@property (weak, nonatomic) IBOutlet UILabel *latitudeLabel;
//放置标注Button
- (IBAction)annotationAction:(id)sender;
@end
3、MKMapView可以通过 setShowsUserLocation:YES这个方法来获取自己的位置,并且当地图更新自己的位置后会调用
-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation的一个协议的委托方法,我要在这个方法里面试实现当地图位置更新后/获取自己位置后对地图进行放大。
首先在viewDidLoad方法里对让地图调用setShowsUserLocation方法来实现地图的定位,并且设置MapView的委托类。
- - (void)viewDidLoad
- {
- //设置MapView的委托为自己
- [self.mapView setDelegate:self];
- //标注自身位置
- [self.mapView setShowsUserLocation:YES];
- [super viewDidLoad];
- }
- (void)viewDidLoad
{
//设置MapView的委托为自己
[self.mapView setDelegate:self];
//标注自身位置
[self.mapView setShowsUserLocation:YES];
[super viewDidLoad];
}
然后实现-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation方法:
- //MapView委托方法,当定位自身时调用
- -(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation{
- CLLocationCoordinate2D loc = [userLocation coordinate];
- //放大地图到自身的经纬度位置。
- MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(loc, 250, 250);
- [self.mapView setRegion:region animated:YES];
- }
//MapView委托方法,当定位自身时调用
-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation{
CLLocationCoordinate2D loc = [userLocation coordinate];
//放大地图到自身的经纬度位置。
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(loc, 250, 250);
[self.mapView setRegion:region animated:YES];
}
CLLocationCoordinate2D 是一个结构体记录经纬度,通过地图的获取的location来给其赋值。
运行一下程序地图载入的同时获得自身的位置,并且会自动放大到你所在的位置。
调试位置:
模拟器在运行的时候,可以自定义的设置其自身所在的位置
4、获取自身的经纬度显示在两个label上,还是在-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation方法里面实现,通过MKUserLocation这个类里面有经度和纬度的属性,直接拿出来显示在label上
- //MapView委托方法,当定位自身时调用
- -(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation{
- CLLocationCoordinate2D loc = [userLocation coordinate];
- //显示到label上
- self.longitudeLabel.text = [NSString stringWithFormat:@"%f",loc.longitude];
- self.latitudeLabel.text = [NSString stringWithFormat:@"%f",loc.latitude];
- //放大地图到自身的经纬度位置。
- MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(loc, 250, 250);
- [self.mapView setRegion:region animated:YES];
- }
//MapView委托方法,当定位自身时调用
-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation{
CLLocationCoordinate2D loc = [userLocation coordinate];
//显示到label上
self.longitudeLabel.text = [NSString stringWithFormat:@"%f",loc.longitude];
self.latitudeLabel.text = [NSString stringWithFormat:@"%f",loc.latitude];
//放大地图到自身的经纬度位置。
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(loc, 250, 250);
[self.mapView setRegion:region animated:YES];
}
二、在地图上加上标注(大头针)
1、要给地图添加标注,就要自己实现一个标注类,这个类必须实现MKAnnotation协议,MKAnnotation有个必须要定义的属性
CLLocationCoordinate2D coordinate;
新建一个 MyPoint类继承自NSObject类并且实现<MKAnnotation>协议然后定义下列属性:
- #import <Foundation/Foundation.h>
- #import <MapKit/MapKit.h>
- @interface MyPoint : NSObject <MKAnnotation>
- //实现MKAnnotation协议必须要定义这个属性
- @property (nonatomic,readonly) CLLocationCoordinate2D coordinate;
- //标题
- @property (nonatomic,copy) NSString *title;
- //初始化方法
- -(id)initWithCoordinate:(CLLocationCoordinate2D)c andTitle:(NSString*)t;
- @end
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
@interface MyPoint : NSObject <MKAnnotation>
//实现MKAnnotation协议必须要定义这个属性
@property (nonatomic,readonly) CLLocationCoordinate2D coordinate;
//标题
@property (nonatomic,copy) NSString *title;
//初始化方法
-(id)initWithCoordinate:(CLLocationCoordinate2D)c andTitle:(NSString*)t;
@end
在.m文件里实现init方法
- #import "MyPoint.h"
- @implementation MyPoint
- -(id)initWithCoordinate:(CLLocationCoordinate2D)c andTitle:(NSString *)t{
- self = [super init];
- if(self){
- _coordinate = c;
- _title = t;
- }
- return self;
- }
- @end
#import "MyPoint.h"
@implementation MyPoint
-(id)initWithCoordinate:(CLLocationCoordinate2D)c andTitle:(NSString *)t{
self = [super init];
if(self){
_coordinate = c;
_title = t;
}
return self;
}
@end
以上就是一个简单的标注类了。
2、回到ViewController类中在button按钮关联的- (IBAction)annotationAction:(id)sender方法中实现标注:
首先通过UITextField所填写的经纬度创建一个CLLocation类,然后赋值给CLLocationCoordinate2D:
- //创建CLLocation 设置经纬度
- CLLocation *loc = [[CLLocation alloc]initWithLatitude:[[self.latitudeText text] floatValue] longitude:[[self.longitudeText text] floatValue]];
- CLLocationCoordinate2D coord = [loc coordinate];
//创建CLLocation 设置经纬度
CLLocation *loc = [[CLLocation alloc]initWithLatitude:[[self.latitudeText text] floatValue] longitude:[[self.longitudeText text] floatValue]];
CLLocationCoordinate2D coord = [loc coordinate];
创建MyPoint实例最后调用MKMapView的addAnnotation方法将MyPoint的实例加入到标注中:- (IBAction)annotationAction:(id)sender中的实现:
- //放置标注
- - (IBAction)annotationAction:(id)sender {
- //创建CLLocation 设置经纬度
- CLLocation *loc = [[CLLocation alloc]initWithLatitude:[[self.latitudeText text] floatValue] longitude:[[self.longitudeText text] floatValue]];
- CLLocationCoordinate2D coord = [loc coordinate];
- //创建标题
- NSString *titile = [NSString stringWithFormat:@"%f,%f",coord.latitude,coord.longitude];
- MyPoint *myPoint = [[MyPoint alloc] initWithCoordinate:coord andTitle:titile];
- //添加标注
- [self.mapView addAnnotation:myPoint];
- //放大到标注的位置
- MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(coord, 250, 250);
- [self.mapView setRegion:region animated:YES];
- }
//放置标注
- (IBAction)annotationAction:(id)sender {
//创建CLLocation 设置经纬度
CLLocation *loc = [[CLLocation alloc]initWithLatitude:[[self.latitudeText text] floatValue] longitude:[[self.longitudeText text] floatValue]];
CLLocationCoordinate2D coord = [loc coordinate];
//创建标题
NSString *titile = [NSString stringWithFormat:@"%f,%f",coord.latitude,coord.longitude];
MyPoint *myPoint = [[MyPoint alloc] initWithCoordinate:coord andTitle:titile];
//添加标注
[self.mapView addAnnotation:myPoint];
//放大到标注的位置
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(coord, 250, 250);
[self.mapView setRegion:region animated:YES];
}
另外还可以使用[self.mapView addAnnotations:NSArray]这个方法,这个方法的参数是一个数组对象,可以同时给地图上添加多个标注。
3、最后运行程序填入经纬度,就能给地图加上标注了。