如何在mapview中添加多个颜色图钉。

时间:2012-10-30 10:21:37

标签: iphone objective-c ios xcode

我有一张有三类兴趣点的地图。目前我已让它们显示,但它们都是相同的颜色。如何根据它们所属的类别让它们显示颜色?

我进行了搜索,无法找到任何我能理解的内容以应用于我的代码。

这是我当前用于注释引脚的代码:

-(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;
}

这是Code的其余部分,需要它。

感谢您的帮助。

3 个答案:

答案 0 :(得分: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)
        {

        }

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

答案 1 :(得分: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;
}

答案 2 :(得分:1)

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

你也可以看到这篇文章,看看如何ovveride MKAnnotation类:MKAnnotation