1

大家好!我在给 MKAnnotationView 提供图像而不是 pin 视图时遇到了一些麻烦。换句话说,我无法显示目标图像 (target.png) 而不是正常的 pin 视图。这是我的代码---


// .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;
}

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

编辑:我更改了我的 Xcode 文档中的代码以更改答案 1。我懒得将所有内容都转移到上面的代码块中,但图片仍然无法正常工作。

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

4

3 回答 3

1

哇,这段代码错得太多了:-)

首先,您@property在您的AddressAnnotation

@property (nonatomic,assign) CLLocationCoordinate2D coordinate;

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

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

尝试先修复这些东西,看看是否有帮助:-)

于 2010-02-03T18:23:12.583 回答
0

我是 Objective C 的新手,但我认为您的委托函数的原型应该是:

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

委托名称是mapView :viewForAnnotation:,而不是map :viewForAnnotation:

于 2010-02-05T14:55:47.077 回答
0

你的 AddressAnnotation 也没有实现 MKAnnotation 协议

于 2011-09-03T23:22:01.283 回答