This is the first time I am using a plist. I have been browsing around and I can’t find any other help.
I have the code below in my viewDidLoad. How come my annotations are not showing?
mapView.showsUserLocation = YES;
mapView.delegate=self;
NSMutableArray *annotations = [[NSMutableArray alloc]init];
NSString *path = [[NSBundle mainBundle] pathForResource:@"StationAddress" ofType:@"plist"];
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:path];
NSArray *anns = [dict objectForKey:@"GasStation"];
NSLog(@" MapView is %@",mapView);
NSLog(@"anns is: %@", anns);
for(int i = 0; i < [anns count]; i++) {
NSLog(@"read2");
double realLatitude = [[[anns objectAtIndex:i] objectForKey:@"Latitude"] doubleValue];
NSLog(@" double lat value is %f",realLatitude);
double realLongitude = [[[anns objectAtIndex:i] objectForKey:@"Longitude"] doubleValue];
NSLog(@" double lot value is %f",realLatitude);
NSLog(@"read3");
MyAnnotation *myAnnotation = [[MyAnnotation alloc] init];
CLLocationCoordinate2D theCoordinate;
theCoordinate.latitude = realLatitude;
theCoordinate.longitude = realLongitude;
myAnnotation.coordinate=CLLocationCoordinate2DMake(realLatitude,realLongitude);
myAnnotation.title = [[anns objectAtIndex:i] objectForKey:@"Name"];
myAnnotation.subtitle = [[anns objectAtIndex:i] objectForKey:@"Address"];
[mapView addAnnotation:myAnnotation];
[annotations addObject:myAnnotation];
[myAnnotation release];
EDIT MyAnnotation.h
@interface MyAnnotation : NSObject<MKAnnotation> {
CLLocationCoordinate2D coordinate;
NSString* title;
NSString* subtitle;
}
@property (nonatomic, assign) CLLocationCoordinate2D coordinate;
@property (nonatomic, copy) NSString* title;
@property (nonatomic, copy) NSString* subtitle;
MyAnnotation.m
@implementation MyAnnotation
@synthesize title;
@synthesize subtitle;
@synthesize coordinate;
Hard Code for annotations, (this is inside viewdidload)
[mapView setShowsUserLocation:YES];
CLLocation *userLoc = mapView.userLocation.location;
CLLocationCoordinate2D userCoordinate = userLoc.coordinate;
NSLog(@"user latitude = %f",userCoordinate.latitude);
NSLog(@"user longitude = %f",userCoordinate.longitude);
mapView.delegate=self;
NSMutableArray* annotations=[[NSMutableArray alloc] init];
MKPointAnnotation *annot1 = [[MKPointAnnotation alloc] init];
annot1.title = @"TItle1";
annot1.subtitle=@"Address of title";
annot1.coordinate = CLLocationCoordinate2DMake(21.946114, 120.792778);
[mapView addAnnotation:annot1];
and I did this for 3000 of them, but they ran slow, so I am trying to find a better way
Try this.
Change:
To this:
(do longitude also).
Also hint: check for your annotations in the southern Atlantic Ocean, south west of Equatorial Guinea – that’s where Lat/Lon of {0,0} is, and if your data isn’t converting correctly, that’s where your annotations might be.