3

我有一张包含 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;
}

这是我的代码的其余部分,以防需要。

谢谢你的帮助。

4

3 回答 3

5

很简单,只需添加一个 if 语句来检查标题或副标题。当我使用地图时,我的注释类有一个副标题,它会说明地点是什么,即酒店、机场等。我只需添加一个 if 语句来检查,然后设置针点是什么并设置什么颜色我希望别针是

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)
        {

        }

您需要确保将注释转换为您创建的任何类的名称。就是这样。希望有帮助!!

于 2012-10-30T10:26:28.403 回答
2
- (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;
}
于 2012-10-30T10:36:45.077 回答
1

您必须覆盖 mkAnnotation 类并定义声明类别 ID 的特定字段。当您在地图上创建注释时设置 id,并在委托 viewForAnnotation 上检查 id 注释,并检查您必须使用哪种颜色作为当前 id 的 pin。

您还可以查看这篇文章以了解如何覆盖 MKAnnotation 类:MKAnnotation

于 2012-10-30T10:29:05.533 回答