我的地图具有3个类别的兴趣点。目前,我让它们可以显示,但是它们都是相同的颜色。我该如何根据他们所处的类别来显示颜色?
我已经四处搜索,找不到我可以理解的任何应用于我的代码的东西。
这是我当前注释引脚的代码:
-(void)pins:(NSString *)name lat:(NSString *)lat lon:(NSString *)lon poiID:(NSString *)poiID
{
//cast string to float
CGFloat Lat = (CGFloat)[lat floatValue];
CGFloat Lon = (CGFloat)[lon floatValue];
//Set coordinates of pin
CLLocationCoordinate2D coordinate;
coordinate.latitude = Lat;
coordinate.longitude = Lon;
//Create Pin
MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];
//set details
[annotation setCoordinate:coordinate];
[annotation setTitle:name];
[annotation setAccessibilityLabel:poiID]; //set the label eq to the id so we know which poi page to go to
// Add pin to map
[self.showMapView addAnnotation:annotation];
}
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
//create annotation
MKPinAnnotationView *pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"pinView"];
if (!pinView) {
pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"pinView"];
pinView.pinColor = MKPinAnnotationColorRed;
pinView.animatesDrop = FALSE;
pinView.canShowCallout = YES;
//details button
UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
pinView.rightCalloutAccessoryView = rightButton;
} else {
pinView.annotation = annotation;
}
return pinView;
}
这是我的其余代码,它是需要的。
感谢您的任何帮助。
足够简单,只需添加一个IF语句以检查标题或字幕即可。当我使用地图时,我的注释课带有一个字幕,在这里说明了什么地方,即酒店,机场等我希望PIN
if([annotation.subtitle isEqualToString:@"Hotel"])
{
pinView.animatesDrop=YES;
pinView.pinColor=MKPinAnnotationColorPurple;
}else if([annotation.subtitle isEqualToString:@"Airport"])
{
pinView.animatesDrop=YES;
pinView.pinColor=MKPinAnnotationColorRed;
}
,或者如果您希望它是图像,只需使用
pinView.image = [UIImage imageNamed:@"airportMarker.png"];
编辑:更新的代码
创建一个自定义注释类
@interface MyAnnotation : NSObject<MKAnnotation>
{
CLLocationCoordinate2D coordinate;
NSString* title;
NSString* subtitle;
NSInteger categoryID;
}
@property NSInteger categoryID;
@property (nonatomic, assign) CLLocationCoordinate2D coordinate;
@property (nonatomic, copy) NSString* title;
@property (nonatomic, copy) NSString* subtitle;
@end
然后,当您将对象添加到屏幕上时,给每个一个一个ID,即
CLLocationCoordinate2D hotelCoordinates1;
hotelCoordinates1.latitude = 35.38355;
hotelCoordinates1.longitude = 1.11004;
MyAnnotation* exampleHotel=[[MyAnnotation alloc] init];
exampleHotel.coordinate=hotelCoordinates1;
exampleHotel.title=@"Example Hotel";
exampleHotel.subtitle=@"Hotel";
exampleHotel.categoryID = 1;
然后在此方法中
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
//create annotation
只是做这个
if([(MyAnnotation*)annotation categoryID] == 1)
{
}
您需要确保将注释投入到创建的班级名称的任何名称中。就是这样。希望有帮助!
- (MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id <MKAnnotation>)annotation {
static NSString *AnnotationViewID = @"annotationViewID";
MKAnnotationView *annotationView = (MKAnnotationView *)[MymapView dequeueReusableAnnotationViewWithIdentifier:AnnotationViewID];
if (annotationView == nil)
{
annotationView = [[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationViewID] autorelease];
}
if (annotation == MymapView.userLocation)
return nil;
/////i give just an example with category
if(category == 1){
annotationView.image = [UIImage imageNamed:@"Category1.png"];
}
else if(category == 2){
annotationView.image = [UIImage imageNamed:@"Category2.png"];
}
annotationView.annotation = annotation;
annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
annotationView.rightCalloutAccessoryView.tag = 101;
annotationView.canShowCallout = YES;
return annotationView;
}
您必须覆盖mkannotation类并定义specyfif字段,在此声明类别ID。当您在地图上创建注释时,将ID设置为ID,并在委托ViewForantation上检查ID注释,并检查当前ID的PIN所需的颜色。
您还可以查看此帖子,以查看如何发表Ovveride Mkannotation类:Mkannotation