1.创建地图:
_mapView = [[MKMapView alloc] initWithFrame:self.view.bounds];
_mapView.delegate = self;
_mapView.userInteractionEnabled = YES;
_mapView.centerCoordinate = CLLocationCoordinate2DMake(39.820102, 116.288928);
[self.view addSubview:_mapView];
2.添加大头针
MKPointAnnotation * annotation = [[MKPointAnnotation alloc] init];
annotation.coordinate = _mapView.centerCoordinate;
CLLocationCoordinate2D location=CLLocationCoordinate2DMake(39.820102, 116.288928);
annotation.title = @"title";//点击大头针弹框里显示的文字内容
annotation.subtitle = @"subtitle";
http://www.it165.net/pro/html/201403/10827.html
4.自定义大头针样式
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
static NSString *placemarkIdentifier = @"my annotation identifier";
if ([annotation isKindOfClass:[MKPointAnnotation class]]) {
MKAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:placemarkIdentifier];
if (annotationView == nil) {
annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:placemarkIdentifier];
annotationView.image = [UIImage imageNamed:@"map_zuobiao_xiao"];
}
else{
annotationView.annotation = annotation;
}
annotationView.canShowCallout = YES;
return annotationView;
}
return nil;
}
annotationView.canShowCallout = YES;
这样设置之后点击大头针就会有弹框出现了
如果不点击直接显示弹框,调用代码
[_mapView selectAnnotation:annotation animated:YES];
自定义的callOutView其实也是一种大头针,所以如果你要自定义弹框,你需要自定义你的弹框的annotationView。下面,CallOutAnnotationView是集成了大头针和样式的弹框,CallOutMapAnnotation是弹框大头针,JingDianMapCell是弹框样式。
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
if ([annotation isKindOfClass:[CalloutMapAnnotation class]]) {
CallOutAnnotationVifew *annotationView = (CallOutAnnotationVifew *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"CalloutView"];
if (!annotationView) {
annotationView = [[[CallOutAnnotationVifew alloc] initWithAnnotation:annotation reuseIdentifier:@"CalloutView"] autorelease];
JingDianMapCell *cell = [[[NSBundle mainBundle] loadNibNamed:@"JingDianMapCell" owner:self options:nil] objectAtIndex:0];
[annotationView.contentView addSubview:cell];
}
return annotationView;
} else if ([annotation isKindOfClass:[BasicMapAnnotation class]]) {
MKAnnotationView *annotationView =[self.mapView dequeueReusableAnnotationViewWithIdentifier:@"CustomAnnotation"];
if (!annotationView) {
annotationView = [[[MKAnnotationView alloc] initWithAnnotation:annotation
reuseIdentifier:@"CustomAnnotation"] autorelease];
annotationView.canShowCallout = NO;
annotationView.image = [UIImage imageNamed:@"pin.png"];
}
return annotationView;
}
return nil;
}
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view {
if ([view.annotation isKindOfClass:[BasicMapAnnotation class]]) {
if (_calloutAnnotation.coordinate.latitude == view.annotation.coordinate.latitude&&
_calloutAnnotation.coordinate.longitude == view.annotation.coordinate.longitude) {
return;
}
if (_calloutAnnotation) {
[mapView removeAnnotation:_calloutAnnotation];
_calloutAnnotation = nil;
}
_calloutAnnotation = [[[CalloutMapAnnotation alloc]
initWithLatitude:view.annotation.coordinate.latitude
andLongitude:view.annotation.coordinate.longitude] autorelease];
[mapView addAnnotation:_calloutAnnotation];
[mapView setCenterCoordinate:_calloutAnnotation.coordinate animated:YES];
}
else{
if([delegate respondsToSelector:@selector(customMKMapViewDidSelectedWithInfo:)]){
[delegate customMKMapViewDidSelectedWithInfo:@"点击至之后你要在这干点啥"];
}
}
}