请我最近制作了一张地图,假设显示用户位置(绿色针脚颜色)和其他服务站注释(红色针脚颜色),目前,我能够显示所有相同颜色的注释和我仍然无法告诉MKMapView
代表如何区分两种引脚类型,因此为每种类型指定要显示的正确标题。
这是我的MKAnnotationView
方法:
- (MKAnnotationView *)mapView:(MKMapView *)map viewForAnnotation:(id <MKAnnotation>)annotation {
static NSString *identifier = @"MyLocation";
if ([annotation isKindOfClass:[MyLocation class]]) {
MKPinAnnotationView *annotationView = (MKPinAnnotationView *) [map dequeueReusableAnnotationViewWithIdentifier:identifier];
if (annotationView == nil) {
annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
} else {
annotationView.annotation = annotation;
}
annotationView.enabled = YES;
[annotationView setAnimatesDrop:YES];
annotationView.canShowCallout = YES;
return annotationView;
}
return nil;
}
对于实现MKAnnotation
委托的我的类,我有这个方法:
-(id)initWithName:(NSString *)enseigneDeLaStation distanceVersLaStation:(NSString *) distanceVersLaStation coordinate:(CLLocationCoordinate2D)coordinate
{
if ((self = [super init])) {
_enseigneDeLaStation = [enseigneDeLaStation copy];
_distanceVersLaStation = [distanceVersLaStation copy];
_coordinate = coordinate;
}
return self;
}
事先得到你的帮助:)
修改 我试着像这样制作用户位置注释,如果我错了,请纠正我:
location2D = (CLLocationCoordinate2D){ .latitude = latitudeOfUserLocation, .longitude = longitudeOfUserLocation };
MyLocation *annotation=[[[MyLocation alloc]initWithName:@"You are here" distanceVersLaStation:@"" coordinate:location2D]autorelease];
[mapView addAnnotation:annotation];
编辑2 嗨再次,我尝试制作一个注释,以确保它甚至调用注释方法,所以想象这个简单的例子:
latitudeOfUserLocation=43.2923;
longitudeOfUserLocation=5.45427;
location2D = (CLLocationCoordinate2D){ .latitude = latitudeOfUserLocation, .longitude = longitudeOfUserLocation };
MyLocation *annotation=[[[MyLocation alloc]initWithName:@"You are here" distanceVersLaStation:@"coucou" coordinate:location2D]autorelease];
[mapView addAnnotation:annotation];
MKCoordinateSpan span={latitudeDelta:1,longitudeDelta:0.5};
MKCoordinateRegion region={location2D,span};
[mapView setRegion:region];
当我运行应用程序时,我看到区域缩放得很好,与43.2923 / 5.45427一致,但我没有看到注释,为什么它没有调用注释方法,我无法继续,因为它没有显示用户注释,请提前帮助:)
编辑3
嗨,我认为这总是与用户和站点引脚之间的差异有关,所以我声明了一个var codeColor
,在调用注释时将其设置为1(用户的类型)并将其设置为2 (电台类型)调用电台的注释时:
-(void)viewWillAppear:(BOOL)animated
{
codeColor=1;//it's the first type, the user one
MyLocation *annotation=[[[MyLocation alloc]initWithName:@"You are right here" distanceVersLaStation:@"" coordinate:location2D]autorelease];
annotation.pinColor = MKPinAnnotationColorGreen;
[mapView addAnnotation:annotation];
}
和:
-(void)requestFinished:(ASIHTTPRequest *)request
{ codeColor=2;
[MBProgressHUD hideHUDForView:self.view animated:YES];
MyLocation *annotation=[[[MyLocation alloc]initWithName:ensStation distanceVersLaStation:distance coordinate:location2D]autorelease];
annotation.pinColor = MKPinAnnotationColorPurple;
[mapView addAnnotation:annotation];
}
和注释方法:
if (codeColor==2) {
annotationView.rightCalloutAccessoryView=[UIButton buttonWithType:UIButtonTypeDetailDisclosure];
}
现在第一次测试时,一切正常,没有按钮的用户注释和第一站的按钮,但是当我从视图移动并且我回来时,所有注释都带有按钮,甚至是用户一。能不能请你帮助我,提前:)
答案 0 :(得分:1)
一种简单的方法是将一个pinColor属性添加到MyLocation类:
@property (nonatomic, assign) MKPinAnnotationColor pinColor;
创建注释时,请设置pinColor:
MyLocation *annotation=[[[MyLocation alloc]initWithName:@"You are here" distanceVersLaStation:@"" coordinate:location2D]autorelease];
annotation.pinColor = MKPinAnnotationColorGreen; //or red or whatever
[mapView addAnnotation:annotation];
并在viewForAnnotation方法中:
//...
annotationView.canShowCallout = YES;
annotationView.pinColor = ((MyLocation *)annotation).pinColor;