2010-02-03 18 views
1

嘿家伙!我在给一个MKAnnotationView而不是一个pin视图时遇到了一些麻烦。换句话说,我无法显示目标图像(target.png)而不是普通的针脚视图。这里是我的代码---我无法给MKAnnotation一张图片!


// .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编辑:谢谢你的回复!我的解决方案是我忘了说mapView.delegate = self。再见!

+0

我有完全相同的问题 - 忘记设置委托。为同样的问题投票! – Andrew 2012-07-14 19:55:07

回答

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

尝试修复那些东西先来看看它是否有助于:-)

+0

哈哈!我无法相信!最具讽刺意义的是,我从字面上复制并粘贴了教程中的代码。 – Flafla2 2010-02-03 20:38:02

+0

尽管如此,这并没有诀窍... – Flafla2 2010-02-03 20:42:44

0

我新的目标C,但我认为原型为您的委托功能应该是:

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

委托的名字是MapView的:viewForAnnotation :,不地图:viewForAnnotation:

+0

感谢您的回应!那么它不一定是mapView,因为它在这个动作中做的是创建注解视图。这可以是任何你想要的(名字)。 – Flafla2 2010-02-05 21:30:52

+0

不,我的意思是代表名称不是变量名称。 注解视图是在框架调用的委托函数中创建的。我很确定你的委托名称是错误的“map:viewForAnnotation:”而不是“mapView:viewForAnnotation:”。 如果我是对的,你的功能永远不会被调用。这就是为什么你最终得到默认的注释视图。您是否尝试在以下行上设置断点: “if(annView == nil)”。 – FKDev 2010-02-06 16:39:11

0

另外你AddressAnnotation没有实现MKAnnotation协议

相关问题