地图注解Annotation
原文地址:iphone开发-地图注解(地图上的大头针)作者:yangshuai
- (NSString?*)title
{
return?self._titleString;
}
// optional
- (NSString?*)subtitle
{
????return?_subTitleString;
}
?
- (void)dealloc
{
[_titleString?release];
[_subTitleString?release];
????[super?dealloc];
}
?
@end
?
二、创建、添加和删除注解
1、创建注解:
LocationObject?*aLocationObject = [[LocationObject?alloc]initWithTitle:nameString?latitue:[latitudeString?floatValue]?longitude:[longitudeString?floatValue]];
aLocationObject._subTitleString?= addressString;
?
2、添加注解:
先构建一个注解数组?NSMutableArray?*_mapAnnotations;
然后?
[self._mapAnnotations?addObject:aLocationObject];
[self._mapView?addAnnotations:self._mapAnnotations];
?
3、删除注解:
删除注解可执行 removeAnnotation:一次只删除一个注解,或者执行 removeAnnotation:删除一个数组中的所有项。
如果需要使地图视图回到无注解状态,可执行:
[self._mapView?removeAnnotations:self._mapView.annotations];
删除其中全部注解,MKMapView?的?annotations?属性获取了所有注解的数组,然后从地图上全部删除。
?
三、注解视图?
注解对象并非视图,是描述注解的抽象类。注解视图是属于 MKAnnotationView 的子类 MKPinAnnotationView,当地图通过 addAnnotation:或 addAnnotations:添加了注解后,MKMapViewDelegate 协议的委托方法?- (void)mapView:(MKMapView?*)mapViewdidAddAnnotationViews:(NSArray?*)views?就会通知委托,可以在此回调方法里设置注解视图,如设置大头针颜色、添加附属按钮等,例:
- (void)mapView:(MKMapView?*)mapView?didAddAnnotationViews:(NSArray?*)views
{
// Initialize each view
for?(MKPinAnnotationView?*mkaview?in?views)
{
//?当前位置 的大头针设为紫色,并且没有右边的附属按钮
if?([mkaview.annotation.title?isEqualToString:@"当前位置"])?
{
mkaview.pinColor?=?MKPinAnnotationColorPurple;
mkaview.rightCalloutAccessoryView?=?nil;
continue;
}
?
//?其他位置的大头针设为红色,右边添加附属按钮
mkaview.pinColor?=?MKPinAnnotationColorRed;
UIButton?*button = [UIButton?buttonWithType:UIButtonTypeDetailDisclosure];
mkaview.rightCalloutAccessoryView?= button;
}
}
?
四、注解视图?MKPinAnnotationView?的几个属性说明
?
newAnnotation.animatesDrop?=?YES;??// 大头针掉落的动画开启,NO-关闭
?
newAnnotation.canShowCallout=YES;??//?控制轻击按钮是否生成一个注解视图,默认为Yes-开启
?
newAnnotation.pinColor????// 设置大头针颜色,一共有三种颜色:红色(MKPinAnnotationColorRed),绿色(MKPinAnnotationColorGreen),紫色(MKPinAnnotationColorPurple)
?
?
五、自动显示注解视图(Callout)
- (MKAnnotationView?*)mapView:(MKMapView?*)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
。。。。。。
/////?自动显示?Callout
_myAnnotation?= annotation;
[self?performSelector:@selector(showCallout)?withObject:selfafterDelay:0.1];
?
??return?newAnnotation;
}
?
- (void)showCallout {
????[self._mapView?selectAnnotation:_myAnnotation?animated:YES];?
}
?