1.IPhone中显示地图十分简单,只需要在你的xib文件中添加一个地图视图就可以:MapView
2.使程序支持地图应用,必须加入相应的库,并引入相应的头文件
3.为了能操纵这个地图,我们把它设置成输出口,同时设计几个UILabel并设置成输出口,显示经纬度等信息
#import <UIKit/UIKit.h>
#import<CoreLocation/CoreLocation.h>
#import<MapKit/MapKit.h>
@interfaceASViewController :UIViewController<CLLocationManagerDelegate,MKMapViewDelegate>
@property(nonatomic,retain)IBOutletUILabel*lon;
@property(nonatomic,retain)IBOutletUILabel*lat;
@property(nonatomic,retain)IBOutletUILabel*speed;
@property(nonatomic,retain)IBOutletUILabel*direction;
@property(nonatomic,retain)IBOutletUILabel*time;
@property(nonatomic,retain)CLLocationManager*locationManager;//位置管理器
@property(nonatomic,retain)IBOutletMKMapView *map;
@end
4. 在ViewController.m中添加
- (void)viewDidLoad
{
[superviewDidLoad];
//实例化位置管理器
_locationManager = [[CLLocationManageralloc]init];
_locationManager.delegate =self;
//设置最佳经度
_locationManager.desiredAccuracy =kCLLocationAccuracyBest;
//位置过滤:10m更新一次
_locationManager.distanceFilter =10.0;
//启动位置服务
[_locationManagerstartUpdatingLocation];
//设置地图代理
map.delegate =self;
//确定地图类型
map.mapType =MKMapTypeStandard;
//默认显示用户当前位置
map.showsUserLocation =YES;
//为地图添加注解:这里只做示例添加一个注解点
CLLocationCoordinate2D coordinate = {37.997868,114.5158};
ASPin *pin =[[ASPinalloc]initWithCoordinate:coordinate];
[mapaddAnnotation:pin];
[pin release];
}
5.解释ASPin 这是自定义的一个类,继承NSObject,遵守
MKAnnotation协议
ASPin.h:
#import <Foundation/Foundation.h>
#import <Mapkit/Mapkit.h>
@interface ASPin :NSObject<MKAnnotation>
{
CLLocationCoordinate2Dcoordinate;
}
@property(nonatomic,readonly)CLLocationCoordinate2Dcoordinate;
-(id)initWithCoordinate:(CLLocationCoordinate2D)aCoordinate;
@end
ASPin.m:
#import "ASPin.h"
@implementation ASPin
@synthesize coordinate;
-(id)initWithCoordinate:(CLLocationCoordinate2D)aCoordinate
{
if(self =[superinit])
{
coordinate =aCoordinate;
}
returnself;
}
-(void)dealloc
{
[superdealloc];
}
-(CLLocationCoordinate2D)coordinate
{
returncoordinate;
}
//注解标题
-(NSString *)title
{
return@"位置标题";
}
@end
6.为了保证显示位置的实事更新,在ViewController中实现CLLocationManagerDelegate协议
#pragma mark - CLLocationManagerDelegate
//位置更新
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
lon.text = [NSStringstringWithFormat:@"%.6lf",newLocation.coordinate.longitude];
lat.text = [NSStringstringWithFormat:@"%.6lf",newLocation.coordinate.latitude];
direction.text = [NSStringstringWithFormat:@"%.6lf",newLocation.course];//0表示北
speed.text = [NSStringstringWithFormat:@"%.6lf",newLocation.speed]; //double类型单位m/s
time.text = [NSStringstringWithFormat:@"%@",[newLocation.timestampdescription]];
floatzoomLevel =0.01; //设置显示精度
//确定显示初始位置
CLLocationCoordinate2D coordinate = newLocation.coordinate;
MKCoordinateRegion region =MKCoordinateRegionMake(coordinate,MKCoordinateSpanMake(zoomLevel, zoomLevel));
[self.mapsetRegion:regionanimated:YES];
self.map.centerCoordinate = newLocation.coordinate;
}
7.为了显示大头针和注解,在ViewController中实现MKMapViewDelegate协议
#pragma mark - MKMapViewDelegate
//添加注解
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
if([annotationisKindOfClass:[MKUserLocationclass]])
returnnil;
MKPinAnnotationView *pinView =nil;
staticNSString *defaultPinID =@"AS.PIN";
pinView = (MKPinAnnotationView *)[self.mapdequeueReusableAnnotationViewWithIdentifier:defaultPinID];
if(pinView==nil)
{
pinView = [[[MKPinAnnotationViewalloc]initWithAnnotation:annotationreuseIdentifier:defaultPinID]autorelease];
pinView.canShowCallout =YES; //是否点击显示注释文字
pinView.animatesDrop =NO;
}
return pinView;
}