为iOS动态设置地图Pin颜色
我解析一个包含字符串0、1和2的xml //供参考 0=绿色 1=红色 2=紫色 我有一个类可以确认mkannotation,它包含以下属性变量为iOS动态设置地图Pin颜色,ios,mapkit,mkannotation,mkannotationview,Ios,Mapkit,Mkannotation,Mkannotationview,我解析一个包含字符串0、1和2的xml //供参考 0=绿色 1=红色 2=紫色 我有一个类可以确认mkannotation,它包含以下属性变量 CLLocationCoordinate2D coordinate; NSString *title; NSString *subtitle; MKPinAnnotationColor pinColor; 这个类名为MyAnnotation 现在在map视图的viewDidLoad中,我运行一个for循环
CLLocationCoordinate2D coordinate;
NSString *title;
NSString *subtitle;
MKPinAnnotationColor pinColor;
这个类名为MyAnnotation
现在在map视图的viewDidLoad中,我运行一个for循环来迭代解析的数据
如下图所示(locationArray保存了这些数据,我很好地提取了所有信息
for (int i = 0; i < locationArray.count; i++) {
myAnnotation =[[MyAnnotation alloc] init];
NSString *thePointName = [[locationArray objectAtIndex:i]xmlName];
NSString *theAddress = [[locationArray objectAtIndex:i]xmlAddress];
NSString *latString = [[locationArray objectAtIndex:i]xmlLat];
NSString *lonString = [[locationArray objectAtIndex:i]xmlLon];
现在在MKAnnotationView视图中,我执行以下操作
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
// if it's the user location, just return nil.
if ([annotation isKindOfClass:[MKUserLocation class]])
return nil;
// try to dequeue an existing pin view first
static NSString* AnnotationIdentifier = @"AnnotationIdentifier";
MKPinAnnotationView* pinView = [[MKPinAnnotationView alloc]
initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier];
pinView.animatesDrop=YES;
pinView.canShowCallout=YES;
//set pin color to the correct colour
if (myAnnotation.pinColor = MKPinAnnotationColorGreen) {
pinView.pinColor = MKPinAnnotationColorGreen;
}
else if (myAnnotation.pinColor = MKPinAnnotationColorRed) {
pinView.pinColor = MKPinAnnotationColorRed;
}
else if (myAnnotation.pinColor = MKPinAnnotationColorPurple){
pinView.pinColor = MKPinAnnotationColorPurple;
}
UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[rightButton setTitle:annotation.title forState:UIControlStateNormal];
[rightButton addTarget:self
action:@selector(showDetails:)
forControlEvents:UIControlEventTouchUpInside];
pinView.rightCalloutAccessoryView = rightButton;
UIImageView *profileIconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Profile.png"]];
pinView.leftCalloutAccessoryView = profileIconView;
return pinView;
}
注释视图中的此代码:
if (myAnnotation.pinColor = MKPinAnnotationColorGreen) {
不起作用的原因有两个:
myAnnotation
的值,该值是在此委托方法之外设置的变量。不能保证委托方法将与设置了myAnnotation
的for循环同步调用。不要假设在调用add之后会立即调用viewForAnnotation
注释
。如果地图视图需要在缩放或平移后再次显示注释,则对于同一注释,甚至可以多次调用委托方法annotation
参数。这是对映射视图希望在委托方法的当前调用中查看的注释的引用
由于注释
参数的类型一般为id
,因此首先必须将其强制转换为自定义类,然后才能访问任何自定义属性:
//first make sure this annotation is our custom class before casting it...
if ([annotation isKindOfClass:[MyAnnotation class]])
{
MyAnnotation *currentAnn = (MyAnnotation *)annotation;
if (currentAnn.pinColor == MKPinAnnotationColorGreen) {
pinView.pinColor = MKPinAnnotationColorGreen;
}
else if (currentAnn.pinColor == MKPinAnnotationColorRed) {
pinView.pinColor = MKPinAnnotationColorRed;
}
else if (currentAnn.pinColor == MKPinAnnotationColorPurple) {
pinView.pinColor = MKPinAnnotationColorPurple;
}
}
或者更简单:
//first make sure this annotation is our custom class before casting it...
if ([annotation isKindOfClass:[MyAnnotation class]])
{
MyAnnotation *currentAnn = (MyAnnotation *)annotation;
pinView.pinColor = currentAnn.pinColor;
}
(不相关,但是为什么代码设置了rightButton
的标题,即使它不可见?首先我是一个笨蛋:-)是的,我应该使用==已经很晚了,其次我真的很愚蠢,如果([annotation isKindOfClass:[MyAnnotation class]]){MyAnnotation*currentAnn=(MyAnnotation*)很多深夜都能赶上我,非常感谢你抽出时间!
//first make sure this annotation is our custom class before casting it...
if ([annotation isKindOfClass:[MyAnnotation class]])
{
MyAnnotation *currentAnn = (MyAnnotation *)annotation;
if (currentAnn.pinColor == MKPinAnnotationColorGreen) {
pinView.pinColor = MKPinAnnotationColorGreen;
}
else if (currentAnn.pinColor == MKPinAnnotationColorRed) {
pinView.pinColor = MKPinAnnotationColorRed;
}
else if (currentAnn.pinColor == MKPinAnnotationColorPurple) {
pinView.pinColor = MKPinAnnotationColorPurple;
}
}
//first make sure this annotation is our custom class before casting it...
if ([annotation isKindOfClass:[MyAnnotation class]])
{
MyAnnotation *currentAnn = (MyAnnotation *)annotation;
pinView.pinColor = currentAnn.pinColor;
}