简体   繁体   English

从MKAnnotation对象更改图钉颜色

[英]Change the pin color from an MKAnnotation object

I struggling to achieve this point. 我努力做到这一点。 I have a class that is a subclass of NSObject and represents an annotation on the map. 我有一个类,它是NSObject的子类,并表示地图上的注释。 Inside that class I have an alert view when user press on an annotation. 在该类内部,当用户按下注释时,我会看到一个警报视图。 What I am trying to do is, after user press ok from alert I would like to change the pin's colour from red to green. 我想做的是,在用户从警报中按OK后,我想将图钉的颜色从红色更改为绿色。

Below is my MKAnnotation class... 以下是我的MKAnnotation类...

.h 。H

#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
#import "MapMenu.h"


@interface BuildingViewController : NSObject <MKAnnotation, MapMenuDelegate>{

    NSString *name;
    NSString *description;
    CLLocationCoordinate2D location;
    NSString *owner; /*user object*/

}

@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *subtitle;
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
@property (nonatomic, copy) NSString *owner;
@property (nonatomic, retain) IBOutlet MapMenu *menu;

- (id) initBuildingWithName: (NSString *) _name andCoordinates: (CLLocationCoordinate2D) _location
shortDescription:(NSString *)_description andOwner:(NSString *)_owner inDistance: (NSInteger) _distance;

- (void) addMenuOnView: (MKMapView*) mapView;
- (void) hideMenuFromView;
- (void) setAttackEnable: (BOOL) attack;
- (void) attackTo: (BuildingViewController*) selectedBuilding;

@end

.m .m

#import "BuildingViewController.h"
#import "CountDownTimer.h"

@implementation BuildingViewController{

    MapMenuItem* starMenuItem1, *starMenuItem2,
        *starMenuItem3, *starMenuItem4;
    NSInteger distance;
    MKMapView* parentView;
}

@synthesize title, subtitle, coordinate, owner, menu = _menu;

- (id) initBuildingWithName:(NSString *)_name andCoordinates:(CLLocationCoordinate2D)_location
 shortDescription:(NSString *)_description andOwner:(NSString *)_owner inDistance:(NSInteger)_distance{

    self = [super init];
    title = _name;
    coordinate = _location;
    subtitle = _description;
    owner = _owner;
    distance = (int)round(_distance);

    UIImage *storyMenuItemImage = [UIImage imageNamed:@"bg-menuitem.png"];
    UIImage *storyMenuItemImagePressed = [UIImage imageNamed:@"bg-menuitem-highlighted.png"];

    UIImage *starImage = [UIImage imageNamed:@"icon-star.png"];

    starMenuItem1 = [[MapMenuItem alloc] initWithImage:storyMenuItemImage highlightedImage:storyMenuItemImagePressed
                                          contentImage:starImage highContentImage:nil isDisable:NO];

    starMenuItem2 = [[MapMenuItem alloc] initWithImage:storyMenuItemImage highlightedImage:storyMenuItemImagePressed
                                          contentImage:starImage highContentImage:nil isDisable:NO];

    starMenuItem3 = [[MapMenuItem alloc] initWithImage:storyMenuItemImage highlightedImage:storyMenuItemImagePressed
                                          contentImage:starImage highContentImage:nil isDisable:NO];

    starMenuItem4 = [[MapMenuItem alloc] initWithImage:storyMenuItemImage highlightedImage:storyMenuItemImagePressed
                                          contentImage:starImage highContentImage:nil isDisable:NO];

    if (distance > 10){
        [self setAttackEnable:NO];
        starMenuItem1.disable = YES;
    }

    NSArray *menus = [NSArray arrayWithObjects:starMenuItem1, starMenuItem2, starMenuItem3, starMenuItem4, nil];

    _menu = [[MapMenu alloc] initWithFrame:self.menu.bounds menus:menus];
    _menu.delegate = self;

    return self;
}

- (void) addMenuOnView:(MKMapView *)mapView{

    parentView = mapView;
    [parentView addSubview: _menu];
}

- (void) hideMenuFromView{

    [_menu removeFromSuperview];
}

- (void) setAttackEnable:(BOOL)attack{

    if (attack) {
        [starMenuItem1 setHighlighted:NO];
    }else{
        [starMenuItem1 setHighlighted:YES];
        [starMenuItem1 setImage:[UIImage imageNamed:@"bg-menuitem-highlighted.png"]];
    }
}

- (void)MapMenu:(MapMenu *)menu didSelectButton:(NSInteger)index{

    NSLog(@"Select the index : %d\n",index);
    NSLog(@"%@", self.owner);

    if (index == 0) {
        if (self.owner == nil && distance < 10) {
            CountDownTimer* countDown = [[CountDownTimer alloc]init];
            [countDown startTimerOn:parentView];
            [self performSelector:@selector(attackTo:) withObject:nil afterDelay:20.0];

        }
        else if (self.owner == @"Player_1")
            NSLog(@"You have already occupy this building with name, %@", self.title);
    }
}

- (void) attackTo: (BuildingViewController*) selectedBuilding{

    self.owner = @"Player_1";
    [self showMessage:@"Success" withContent:@"You are the new owner of the building."];
    //NSLog(@"Building has a new owner with name, %@", self.owner);
}

- (void) showMessage: (NSString*) msgTitle withContent: (NSString*) content{

    UIAlertView *message = [[UIAlertView alloc] initWithTitle:msgTitle
        message:content
        delegate:self
        cancelButtonTitle:@"OK"
        otherButtonTitles:nil];
    [message show];

}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{

    if (buttonIndex == 0) {

        //I want to change the pin colour here.
    }
}

@end

I am getting a warning on the line where I am adding pin on the parentView (mapView): Sending 'MKPinAnnotationView *__strong' to parameter of incompatible type 'id<MKAnnotation>' and when I am running the app it is crashing when pressing OK. 我在添加在parentView(mapView)上的图钉的行上收到警告: Sending 'MKPinAnnotationView *__strong' to parameter of incompatible type 'id<MKAnnotation>' ,当我运行应用程序时,按OK时崩溃。 I don't know what I am doing wrong. 我不知道我在做什么错。

Thanks in advance. 提前致谢。

I'm giving it a shot, not sure it will work. 我正在试一试,不确定它是否会起作用。

Add a property in your annotation : 在注释中添加属性:

@property (nonatomic) BOOL annotationWasSelected;

This will be set to TRUE when the user dismisses the UIAlertView (init it to FALSE ). 当用户关闭UIAlertView时将其设置为TRUE (将其初始化为FALSE )。

So replace your code with that : 因此,将您的代码替换为:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{

    if (buttonIndex == 0) {

        _annotationWasSelected = TRUE;

        // Remove and add the annotation so it's re-drawn
        [parentView removeAnnotation:self];
        [parentView addAnnotation:self];
    }
}

In your mapView's delegate, you should be doing something like this : 在mapView的委托中,您应该执行以下操作:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {

    static NSString *identifier = @"MyAnnotation";
    MKPinAnnotationView *annotationView = (MKPinAnnotationView *) [_mapView dequeueReusableAnnotationViewWithIdentifier:identifier];

    YourAnnotationClass *myAnnotation = (YourAnnotationClass*) annotation;

    // If a new annotation is created
    if (annotationView == nil) {

        annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:myAnnotation reuseIdentifier:identifier];

        // Annotation's color
        if (myAnnotation.annotationWasSelected) {
            annotationView.pinColor = MKPinAnnotationColorGreen;
        }
        else {
            annotationView.pinColor = MKPinAnnotationColorRed;
        }

    } else {

        annotationView.annotation = annotation;

        // Annotation's color
        if (myAnnotation.annotationWasSelected) {
            annotationView.pinColor = MKPinAnnotationColorGreen;
        }
        else {
            annotationView.pinColor = MKPinAnnotationColorRed;
        }
    }

    return annotationView;
}

You'll have to merge this code with your existing code. 您必须将此代码与现有代码合并。

Again, I'm not entirely sure of this code and can't test it right now. 同样,我不确定此代码,因此无法立即对其进行测试。 Let me know if it helps. 让我知道是否有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM