我无法给MKAnnotation一个图像!

时间:2010-02-03 17:22:17

标签: iphone iphone-sdk-3.0 mkannotation

嘿伙计们!给MKAnnotationView一个图像而不是一个pin视图我遇到了一些麻烦。换句话说,我在显示目标图像(target.png)时遇到了问题,而不是正常的引脚视图。这是我的代码---


// .h file
#import  //Here it says to import mapkit & UIKit.  The code blockquote doesn't let me
#import  //show you that

@interface AddressAnnotation : NSObject {
    CLLocationCoordinate2D coordinate;

    NSString *mTitle;
    NSString *mSubTitle;
}

@end

@interface ChosenLocationMap : UIViewController {
IBOutlet MKMapView *mapView;
AddressAnnotation *addAnnotation;
}
-(CLLocationCoordinate2D) addressLocation;

// .m file
@implementation AddressAnnotation
@synthesize coordinate;

- (NSString *)subtitle{
    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    NSString *stitle = [prefs objectForKey:@"addressKey"];
    return @"%@",stitle;
}

- (NSString *)title{
    return @"TARGET";
}

-(id)initWithCoordinate:(CLLocationCoordinate2D) c{
    coordinate=c;
    NSLog(@"%f,%f",c.latitude,c.longitude);
    return self;
}

@end


@implementation ChosenLocationMap
@synthesize destinationLabel, startbutton, accelloop, aimview, bombblowupview, bombleftview1, bombleftview2, bombleftview3, firebutton;

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
- (void)viewDidLoad {
mapView.mapType = MKMapTypeSatellite;
MKCoordinateSpan span;
    span.latitudeDelta=0.2;
    span.longitudeDelta=0.2;
CLLocationCoordinate2D location = [self addressLocation];
region.span=span;
    region.center=location;
if(addAnnotation != nil) {
        [mapView removeAnnotation:addAnnotation];
        [addAnnotation release];
        addAnnotation = nil;
    }
addAnnotation = [[AddressAnnotation alloc] initWithCoordinate:location];
    [mapView addAnnotation:addAnnotation];
 [super viewDidLoad];
}
-(CLLocationCoordinate2D) addressLocation {
    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    NSString *destinationstring = [prefs objectForKey:@"addressKey"];
    NSString *urlString = [NSString stringWithFormat:@"http://maps.google.com/maps/geo?q=%@&output=csv", 
                           [destinationstring stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    NSString *locationString = [NSString stringWithContentsOfURL:[NSURL URLWithString:urlString]];
    NSArray *listItems = [locationString componentsSeparatedByString:@","];

    double latitude = 0.0;
    double longitude = 0.0;

    if([listItems count] >= 4 && [[listItems objectAtIndex:0] isEqualToString:@"200"]) {
        latitude = [[listItems objectAtIndex:2] doubleValue];
        longitude = [[listItems objectAtIndex:3] doubleValue];
    }
    else {
        //Show error
    }
    CLLocationCoordinate2D location;
    location.latitude = latitude;
    location.longitude = longitude;

    return location;
}

- (MKAnnotationView *)map:(MKMapView *)map viewForAnnotation:(id )annotation{
    MKAnnotationView *annView;
    annView = (MKAnnotationView *) [mapView dequeueReusableAnnotationViewWithIdentifier:annotation.title];

    if(annView == nil)
        annView = [[[MKAnnotationView alloc]
                    initWithAnnotation:annotation reuseIdentifier:annotation.title] autorelease];
    else
        annView.annotation = annotation;


    [annView setImage:[UIImage imageNamed:@"target.png"]];
    annView.canShowCallout = TRUE;

    return annView;
}

请注意,我只包含了实际涉及mapview的代码。提前谢谢!

编辑:我更改了我的Xcode文档中的代码以了解答案1中的更改​​。我太懒了,无法将所有内容传输到上面的代码块,但仍然无法使用图片。

SHOOP DA EDIT:谢谢你的回复!我的解决方案是我忘了说mapView.delegate = self。再见!

3 个答案:

答案 0 :(得分:1)

这段代码太糟糕了: - )

首先,您遗漏了@property

中的AddressAnnotation声明
@property (nonatomic,assign) CLLocationCoordinate2D coordinate;

在字幕方法中,您可以这样做:

return @"%@",stitle;

但这是Objective-C而不是Python,因此您可能希望将其更改为:

return stitle;

然后你的initWithCoordinate是完全错误的。你没有初始化超级。这样更好:

-(id) initWithCoordinate: (CLLocationCoordinate2D) c
{
    if ((self = [super init]) != nil) {
        coordinate=c;
        NSLog(@"%f,%f",c.latitude,c.longitude);
    }
    return self;
}

首先尝试修复这些内容,看它是否有帮助: - )

答案 1 :(得分:0)

我是Objective C的新手,但我认为你的委托功能的原型应该是:

- (MKAnnotationView *)mapView:(MKMapView *)map viewForAnnotation:(id )annotation{
    MKAnnotationView *annView;

代理人姓名为 mapView :viewForAnnotation :,不是地图:viewForAnnotation:

答案 2 :(得分:0)

此外,AddressAnnotation还没有实现MKAnnotation协议