由於某些原因,下面的代碼沒有添加註釋到地圖視圖。當我註銷一個計數時,它返回0,儘管它經歷了數組近50次。 Attraction對象實現MKAnnotation協議。MKAnnotations沒有被添加到mapview
-(void)forwardGeocoderFoundLocation
{
int searchResults = [attractions count];
for(int i = 0; i < searchResults; i++){
Attraction *a = [[Attraction alloc] init];
a.address = [(Attraction *)[attractions objectAtIndex:i] address];
a.city = [(Attraction *)[attractions objectAtIndex:i] city];
a.state = [(Attraction *)[attractions objectAtIndex:i] state];
NSString *address = [NSString stringWithFormat:@"%@,%@,%@", a.address, a.city, a.state];
NSString *theAddress = [address stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
NSString *urlString = [NSString stringWithFormat:@"http://maps.google.com/maps/geo?q=%@&output=csv", theAddress];
NSString *locationString = [[NSString alloc] initWithContentsOfURL:[NSURL URLWithString:urlString] usedEncoding:nil error:nil];
//NSLog(@"%@", locationString);
[[attractions objectAtIndex:i] setLocation:[[CLLocation alloc] initWithLatitude:43.1330340 longitude:-77.6376329]];
NSLog(@"Attraction location = %@", [[attractions objectAtIndex:i] location]);
[self.mv addAnnotation:[self.attractions objectAtIndex:i]];
NSLog(@"Mapview has %i annotations.", self.mv.annotations.count);
}
}
這裏的Attraction.h文件:
#import <Foundation/Foundation.h>
#import <CoreLocation/CoreLocation.h>
#import <MapKit/MapKit.h>
@interface Attraction : NSObject<MKAnnotation>
@property(nonatomic,assign)int attractionID;
@property(nonatomic,copy)NSString *name;
@property(nonatomic,copy)NSString *address;
@property(nonatomic,copy)NSString *description;
@property(nonatomic,copy)NSString *city;
@property(nonatomic,copy)NSString *state;
@property(nonatomic,copy)CLLocation *location;
+ (id)attraction;
- (id)initWithID:(int)a_id name:(NSString *)a_name address:(NSString *)a_address description:(NSString*)a_description city:(NSString*)a_city state:(NSString *)a_state;
@end
而這裏的Attraction.m文件:
#import "Attraction.h"
@implementation Attraction
@synthesize attractionID, name, address, description, city, state, location;
- (CLLocationCoordinate2D)coordinate{
return self.location.coordinate;
}
- (NSString *)title{
return self.name;
}
- (NSString *)subtitle{
return self.name;
}
#pragma mark -
+ (id)attraction {
return [[Attraction alloc] init ];
}
- (id)initWithID:(int)a_id name:(NSString *)a_name address:(NSString *)a_address description:(NSString*)a_description city:(NSString*)a_city state:(NSString *)a_state {
self = [super init];
self.attractionID = a_id;
self.address = a_address;
self.name = a_name;
self.description = a_description;
self.city = a_city;
self.state = a_state;
return self;
}
- (id)init
{
return [self initWithID:0 name:@"TBD" address:@"TBD" description:@"TBD" city:@"TBD" state:@"TBD"];
}
@end
任何幫助,將不勝感激。謝謝。
self.attractions是否與景點相同?你確定self.mv不是零嗎? – sch 2012-02-15 22:04:42
是的self.attractions是一樣的景點,我改變它,看看它有沒有影響,它沒有。我試圖找出它,但它看起來像self.mv是零,我不知道爲什麼。我把它掛在故事板上,所以我不知道爲什麼它是零。 – 2012-02-15 23:04:42
我設法弄清楚了。我有一個協議來更新地圖視圖的吸引力數據,以及它調用forwardGeoocoderFoundLocation方法的方法,該方法在viewDidLoad之前。我在viewDidLoad中調用了該方法,現在工作正常。謝謝! – 2012-02-15 23:08:43