您的位置:首页 > 移动开发 > IOS开发

IOS 在MkMapView中添加自己当前位置的大头针的方法

2016-09-08 14:14 393 查看
利用MKMapView显示自己当前位置的地图一文中介绍了放大地图和显示自己的位置,下面介绍一下在地图自己位置上添加大头针,实现如下所示:



实现的具体过程很简单:

首先实现MKMapViewDelegate协议:

@interface iphone_MapViewController : UIViewController

{

IBOutlet MKMapView *mapView;

}

然后添加代码:

- (void)viewDidLoad {

[super viewDidLoad];

self.mapView.delegate=self;

CLLocationManager *locationManager = [[CLLocationManager alloc] init];//创建位置管理器

locationManager.delegate=self;//设置代理

locationManager.desiredAccuracy=kCLLocationAccuracyBest;//指定需要的精度级别

locationManager.distanceFilter=1000.0f;//设置距离筛选器

[locationManager startUpdatingLocation];//启动位置管理器

MKCoordinateRegion theRegion = { {0.0, 0.0 }, { 0.0, 0.0 } };

theRegion.center=[[locationManager location] coordinate];

[locationManager release];

[mapView setZoomEnabled:YES];

[mapView setScrollEnabled:YES];

theRegion.span.longitudeDelta = 0.01f;

theRegion.span.latitudeDelta = 0.01f;

[mapView setRegion:theRegion animated:YES];

}

- (MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id )annotation

{

MKPinAnnotationView *pinView = nil;

static NSString *defaultPinID = @"com.invasivecode.pin";

pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];

if ( pinView == nil ) pinView = [[[MKPinAnnotationView alloc]

initWithAnnotation:annotation reuseIdentifier:defaultPinID] autorelease];

pinView.pinColor = MKPinAnnotationColorRed;

pinView.canShowCallout = YES;

pinView.animatesDrop = YES;

[mapView.userLocation setTitle:@"欧陆经典"];

[mapView.userLocation setSubtitle:@"vsp"];

return pinView;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息