开发者

I am not able to give an MKAnnotation an image!

Hey guys! I am having some trouble with giving an MKAnnotationView an image instead of a pin view. In other words, I am having trouble displaying a target image (target.png) instead of the normal pin view. Here is my code---


// .h file
#import  //Here it s开发者_开发百科ays 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;
}

Please note that I only included the code that actually involves the mapview. Thanks in advance!

EDIT: I Changed the code in my Xcode document for the changes in answer 1. I am too lazy to transfer everything to the code block above, and still, the picture still doesn't work.

SHOOP DA EDIT: Thank you for replying! My solution was that I forgot to say mapView.delegate = self. Bye!


Wow so much wrong with this code :-)

First you are missing a @property declaration in your AddressAnnotation

@property (nonatomic,assign) CLLocationCoordinate2D coordinate;

In the subtitle method you do this:

return @"%@",stitle;

But this is Objective-C and not Python, so you might want to change this to:

return stitle;

Then your initWithCoordinate is completely wrong. You do not initialize super. This is better:

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

Try fixing that stuff first to see if it helps :-)


I'm new to Objective C but I think the prototype for your delegate function should be :

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

The delegate name is mapView:viewForAnnotation:, not map:viewForAnnotation:


Also you AddressAnnotation doesn't implement the MKAnnotation protocol

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜