0

我对 XCode 和 Objective-c 很陌生。这个问题之前可能已经回答过,但我无法让它发挥作用。我的目标是在谷歌地图上显示多个注释。我有一堆 Lats 和 Longs,但是到目前为止,我只能显示一个注释。如何一次显示所有注释。我有下面的代码MKMapView-

- (void)viewDidLoad {

    // Set some coordinates for our position
    CLLocationCoordinate2D location;

    location.latitude = (double) 44.271745;
    location.longitude = (double) -88.453265;   
    // Add the annotation to our map view
    MapViewAnnotation *newAnnotation = [[MapViewAnnotation alloc] initWithTitle:@"Appleton" andCoordinate:location];
    [self.mapview addAnnotation:newAnnotation];

    [newAnnotation release];

    self.mapview.region = MKCoordinateRegionMakeWithDistance(location,100000,100000);
}

我知道我可以循环并实例化newAnnotation,然后用于addAnnotation添加注释。但我不知道该怎么做。这可能是非常基本的,但我对此很陌生。任何帮助将不胜感激。

//
//  MapViewAnnotation.h
//

#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>

@interface MapViewAnnotation : NSObject <MKAnnotation> {

    NSString *title;
    CLLocationCoordinate2D coordinate;

}

@property (nonatomic, copy) NSString *title;
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;

- (id)initWithTitle:(NSString *)ttl andCoordinate:(CLLocationCoordinate2D)c2d;

@end

//
//  MapViewAnnotation.m
//

#import "MapViewAnnotation.h"


@implementation MapViewAnnotation
@synthesize title, coordinate;

- (id)initWithTitle:(NSString *)ttl andCoordinate:(CLLocationCoordinate2D)c2d {
    [super init];
    title = ttl;
    coordinate = c2d;
    return self;
}

- (void)dealloc {
    [title release];
    [super dealloc];
}
@end
4

1 回答 1

1

看起来你只有一个位置。您应该有纬度和经度列表,然后遍历该列表并实例化 MapViewAnnotation。

- (void)viewDidLoad {
    NSArray *arrayOfLatLong = [NSArray arrayWithObjects: [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@"10.22", @"20.212", nil] forKeys:[NSArray arrayWithObjects:@"Lat",@"Long",nil]], 
                               [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@"10.22", @"24.5", nil] forKeys:[NSArray arrayWithObjects:@"Lat",@"Long",nil]], nil];


    for(NSDictionary *location in arrayOfLatLong) {
        CGFloat latitude = [[location valueForKey:@"Lat"] floatValue];
        CGFloat longitude = [[location valueForKey:@"Long"] floatValue];

        CLLocationCoordinate2D location;
        location.latitude = latitude;
        location.longitude = latitude;   
        MapViewAnnotation *newAnnotation = [[MapViewAnnotation alloc] initWithTitle:@"Appleton" andCoordinate:location];
        [self.mapview addAnnotation:newAnnotation];

        [newAnnotation release];
        self.mapview.region = MKCoordinateRegionMakeWithDistance(location,100000,100000);
    }
}
于 2012-08-16T03:53:01.590 回答