1

みんな!ピンビューの代わりに MKAnnotationView に画像を与えることに問題があります。つまり、通常のピン ビューではなく、ターゲット イメージ (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 に関係するコードのみを含めたことに注意してください。前もって感謝します!

編集: 回答 1 の変更に合わせて Xcode ドキュメントのコードを変更しました。上記のコード ブロックにすべてを転送するのが面倒ですが、それでも画像は機能しません。

SHOOP DA EDIT: 返信ありがとうございます。私の解決策は、私が言い忘れたことでしたmapView.delegate = self。さよなら!

4

3 に答える 3

1

うわー、このコードはとても間違っています:-)

まず、@property宣言がありませんAddressAnnotation

@property (nonatomic,assign) CLLocationCoordinate2D coordinate;

サブタイトル メソッドでは、次のようにします。

return @"%@",stitle;

ただし、これは Python ではなく Objective-C であるため、次のように変更することをお勧めします。

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 に答える