问题
In my app, I download a set of point from a web service using json. Actualizing the app to iOS7, I am experimenting that problem: the locations are downloaded but pins on the map are not painted until user touchs and "moves" the map. Then they appear and all work as in iOS6.
How can I correct that behavior?
EDIT: AddAnnotation is called at the end of a method that receive data, parse the json and pass them to mylocaction object:
- (void)plotBarPosition:(NSString *)data_string {
// Parse the string into JSON
NSDictionary *json = [(NSDictionary*)[datos_string1 JSONValue]objectForKey:@"features"];
for (int i = 0; i < [json count]; i++){
/*
PARSING EACH POINT
*/
MyLocation *location =[[MyLocation alloc] initWithName:nameLoc coordinate:coordinate estado:status antenaId:antenaId];
[_mapView addAnnotation:location];
}
}
I tryed also:
[_mapView performSelectorOnMainThread: @selector(addAnnotations:) withObject: location waitUntilDone: NO];
but in this case, annotations do not appear at all.
回答1:
As this answer and the comment by @RAJA suggest, call addAnnotation:
or addAnnotations:
on the main thread. You could do this using GCD or performSelectorOnMainThread
.
The option with the least changes to your existing code is to call addAnnotation:
(singular):
- (void)plotBarPosition:(NSString *)data_string {
// Parse the string into JSON
NSDictionary *json = [(NSDictionary*)[datos_string1 JSONValue]objectForKey:@"features"];
for (int i = 0; i < [json count]; i++){
/*
PARSING EACH POINT
*/
MyLocation *location =[[MyLocation alloc] initWithName:nameLoc coordinate:coordinate estado:status antenaId:antenaId];
//[_mapView addAnnotation:location];
[_mapView performSelectorOnMainThread:@selector(addAnnotation:)
withObject:location
waitUntilDone:YES];
}
}
Alternatively, to use addAnnotations:
(plural), you first add your annotations to a local array and give them to the map view all together in a single call. Changes below are marked with >>>
:
- (void)plotBarPosition:(NSString *)data_string {
// Parse the string into JSON
NSDictionary *json = [(NSDictionary*)[datos_string1 JSONValue]objectForKey:@"features"];
//>>> initialize array to hold annotations...
NSMutableArray *annotationsToAdd = [NSMutableArray array];
for (int i = 0; i < [json count]; i++){
/*
PARSING EACH POINT
*/
MyLocation *location =[[MyLocation alloc] initWithName:nameLoc coordinate:coordinate estado:status antenaId:antenaId];
//>>> comment out direct addAnnotation...
//[_mapView addAnnotation:location];
//>>> add annotation to local array...
[annotationsToAdd addObject:location];
}
//>>> call addAnnotations: (plural) on main thread...
[_mapView performSelectorOnMainThread:@selector(addAnnotations:)
withObject:annotationsToAdd
waitUntilDone:YES];
}
回答2:
There is a protocol MKAnnotation - you have to determine you own class for annotation with realizing of this protocol:
@interface MyAnnotation : NSObject <MKAnnotation> {
CLLocationCoordinate2D coordinate;
}
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
- (id)initWithCoordinate:(CLLocationCoordinate2D)coordinate;
Then you have to init MyAnnotation and add it on map:
MyAnnotation *annotation = [[MyAnnotation alloc] initWithCoordinate:CLLocationCoordinate2DMake(latitude, longitude)];
[myMap addAnnotation:annotation];
Also try to call
[mapView setNeedsDisplay];
or
[mapView setNeedsLayout];
after adding annotations
来源:https://stackoverflow.com/questions/21841225/ios7-map-view-pin-do-not-appear-until-screen-is-touched