我希望我的注释有两种颜色。为此,我使用了以下代码,
- (MKAnnotationView *)mapView:(MKMapView *)sender viewForAnnotation:(id <MKAnnotation>)annotation {
static NSString *identifier = @"MyLocation";
if ([annotation isKindOfClass:[PlaceMark class]]) {
MKPinAnnotationView *annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
annotationView.enabled = YES;
annotationView.canShowCallout = YES;
@try {
if (annotationView == nil) {
annotationView = [[MKPinAnnotationView alloc]
initWithAnnotation:annotation
reuseIdentifier:identifier];
} else {
annotationView.annotation = annotation;
if([[nsCenterOrOffice objectAtIndex:z] isEqualToString:@"C"])
{
annotationView.pinColor = MKPinAnnotationColorRed;
}
else
{
annotationView.pinColor = MKPinAnnotationColorGreen;
}
}
}
@catch (NSException *exception) {
NSLog(@"nsCenterOrOffice exception = %@",exception);
}
return annotationView;
}
return nil;
}
但是我仍然无法为所需的注释设置所需的颜色。有时特定的注释针颜色是红色,有时它是绿色的。我不明白为什么会这样。有谁能够帮我 ?谢谢......
我修改了我的代码..这是我的更新代码
MapAnnotation.h
#import <MapKit/MapKit.h>
#import <Foundation/Foundation.h>
@interface MapAnnotation : MKPointAnnotation
{
NSString *title;
NSString *subtitle;
int dealLnk;
float latitude;
float longitude;
CLLocationCoordinate2D coordinate;
}
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *subtitle;
@property (nonatomic, assign) int dealLnk;
@property (nonatomic, assign) float latitude;
@property (nonatomic, assign) float longitude;
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
- (id)initWithTitle:(NSString *)ttl subTitle:(NSString *)subttl dealLink:(int)dealLnk latitude:(float)latitude longitude:(float)longitude andCoordinate:(CLLocationCoordinate2D)c2d;
@end
MapAnnotation.m
#import "MapAnnotation.h"
@implementation MapAnnotation
@synthesize title,subtitle,dealLnk,coordinate,latitude,longitude;
- (id)initWithTitle:(NSString *)ttl subTitle:(NSString *)subttl dealLink:(int)z latitude:(float)latitude1 longitude:(float)longitude1 andCoordinate:(CLLocationCoordinate2D)c2d {
title = ttl;
subtitle = subttl;
dealLnk =z;
coordinate = c2d;
longitude = longitude1;
latitude = latitude1;
return self;
}
@end
这是我的实施文件
-(void)startAddingAnnotation
{
@try {
CLLocationCoordinate2D annotationCoord;
z=0;
for (int i=0; i < [nslatitude count] ; i++,z++)
{
MapAnnotation *dealAnnotation = [[MapAnnotation alloc] init];
dealAnnotation.dealLnk = z;
annotationCoord.latitude = (CGFloat) [[nslatitude objectAtIndex:i] floatValue];
annotationCoord.longitude = (CGFloat)[[nslongitude objectAtIndex:i] floatValue];
dealAnnotation.coordinate = annotationCoord;
dealAnnotation.title = [nsCenterName objectAtIndex:i];
dealAnnotation.subtitle = [nsCenterAddress objectAtIndex:i];
NSLog(@"latitude = %f",dealAnnotation.latitude);
NSLog(@"longitude = %f",dealAnnotation.longitude);
NSLog(@"dealAnnotation.dealLnk = %d",dealAnnotation.dealLnk);
NSLog(@"dz = %d",z);
[mapView addAnnotation:dealAnnotation];
}
}
@catch (NSException *exception) {
NSLog(@"Exception = %@",exception);
}
cord.longitude = -112.05186;
cord.latitude = 33.46577;
MKCoordinateRegion region;
region.center = cord;
MKCoordinateSpan span = {.latitudeDelta = 1.0, .longitudeDelta = 1.0};
region.span = span;
[mapView setRegion:region];
}
#pragma mark - MapView_Delegate
- (MKAnnotationView *)mapView:(MKMapView *)sender viewForAnnotation:(id <MKAnnotation>)annotation {
MapAnnotation *annotation1 = (MapAnnotation *)annotation;
z = annotation1.dealLnk;
NSLog(@"annotation1.longitude = %f",annotation1.latitude);
NSLog(@"annotation1.longitude = %f",annotation1.longitude);
if(z<[nslatitude count])
{
MKAnnotationView *pinView = nil;
static NSString *defaultPinID = @"pin1";
pinView = (MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
if ( pinView == nil )
pinView = [[MKAnnotationView alloc]
initWithAnnotation:annotation1 reuseIdentifier:defaultPinID];
pinView.canShowCallout = YES;
@try {
if([[nsCenterOrOffice objectAtIndex:z] isEqualToString:@"C"])
{
pinView.image = [UIImage imageNamed:@"flag1.png"];
}
else
{
pinView.image = [UIImage imageNamed:@"flag2.png"];
}
pinView.annotation = annotation1;
pinView.tag = [[nsCenterName objectAtIndex:z] intValue];
}
@catch (NSException *exception) {
NSLog(@"nsCenterOrOffice exception = %@",exception);
}
UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[rightButton setTitle:annotation.title forState:UIControlStateNormal];
[pinView setRightCalloutAccessoryView:rightButton];
return pinView;
}
else
{
return nil;
}
}
现在的问题是,所有的注释都指向了一些不同的位置。我不明白为什么会发生这种情况?我的代码中有什么问题吗?
答案 0 :(得分:1)
确定引脚颜色的条件基于此委托方法的外部,并且无法保证在为特定注释调用此委托方法时它将与之同步。 / p>
在此if
条件中:
if([[nsCenterOrOffice objectAtIndex:z] isEqualToString:@"C"])
z
变量显然是在此委托方法之外声明和设置的一些实例变量(可能在添加注释之前)。
同样,无保证在添加注释后会立即调用viewForAnnotation
委托方法。也不能保证每个注释只会调用一次委托方法。 因此,z
值可能与要调用委托方法的当前 annotation
不对应。
调用addAnnotation
之后很有可能调用委托方法,它将以与添加注释不同的顺序调用,并且每个注释将多次调用它(例如,如果用户平移或缩放地图并且注释返回到视图中)。
要解决此问题,请将“is center or office”属性添加到Placemark
类本身,并在创建注释时设置该属性,并在调用addAnnotation
之前设置。然后,在viewForAnnotation
方法中,您可以将annotation
参数转换为Placemark
并访问该属性以实现该条件。例如:
//where you create and add the annotation:
Placemark *pm = [[Placemark alloc...
pm.coordinate = ...
pm.title = ...
pm.centerOrOffice = [nsCenterOrOffice objectAtIndex:z];
[mapView addAnnotation:pm];
//in viewForAnnotation:
Placemark *myPlacemark = (Placemark *)annotation;
if ([myPlacemark.centerOrOffice isEqualToString:@"C"])
...
一个单独的,不相关的问题是注释视图创建逻辑不太正确
代码调用initWithAnnotation
两次,实际上第二个initWithAnnotation
永远不会被调用,因为第一次调用将始终成功。
您可能想要的是首先致电dequeueReusableAnnotationViewWithIdentifier:
然后,如果返回nil
,请致电initWithAnnotation
。
重新构建viewForAnnotation
中的代码时,请务必将设置pinColor
之外的代码放在注释视图的dequeue / init之前(例如,在...之前) return annotationView;
)以正确处理重用的视图。
答案 1 :(得分:0)
您的viewForAnnotation
依赖于某些外部变量nsCenterOrOffice
和z
。在您向我们展示如何设置这些变量之前,我们当然无法为您提供帮助。
无论如何,这不是一种谨慎的做法。您的viewForAnnotation
通常应仅依赖于annotation
的属性,而不是外部变量。