Я добавляю кучу аннотаций и объединяю их в массив, чтобы я мог вытащить их новые местоположения и соответствующим образом обновить карту БЕЗ предварительного удаления всех аннотаций и добавления их, что вызывает мерцание. К сожалению, после того, как их координаты изначально заданы и добавлены, любой вызов setCoordinate больше не работает. Есть идеи?
- (void)updateMap:(NSData *)responseData {
//parse out the JSON data
NSError* error;
vehicleData = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];
//find which Dictionary is for our bus.
for (NSDictionary* route in vehicleData) {
//find our bus in our vehicles dictionary. Key is by routeID.
BusPositionDot *busLocationDot;
if ([vehicles objectForKey:[route objectForKey:@"RouteID"]] == nil) {
busLocationDot = [[BusPositionDot alloc] init];
[vehicles setObject:busLocationDot forKey:[route objectForKey:@"RouteID"]];
[mapView addAnnotation:[vehicles objectForKey:[route objectForKey:@"RouteID"]]];
}
else {
busLocationDot = [vehicles objectForKey:@"RouteID"];
}
float latitude = [[route objectForKey:@"Latitude"] floatValue];
float longitude = [[route objectForKey:@"Longitude"] floatValue];
float groundSpeed = [[route objectForKey:@"GroundSpeed"] floatValue];
float direction = [[route objectForKey:@"Heading"] floatValue];
float roundedDirection=45 * round(direction/45);
if(groundSpeed<=3)
//get view for annotation
[mapView viewForAnnotation:busLocationDot].image=[UIImage imageNamed:@"buspositiondot.png"];
else if((roundedDirection==0)||(roundedDirection==360))
[mapView viewForAnnotation:busLocationDot].image=[UIImage imageNamed:@"buspositiondot0.png"];
else if(roundedDirection==45)
[mapView viewForAnnotation:busLocationDot].image=[UIImage imageNamed:@"buspositiondot45.png"];
else if(roundedDirection==90)
[mapView viewForAnnotation:busLocationDot].image=[UIImage imageNamed:@"buspositiondot90.png"];
else if(roundedDirection==135)
[mapView viewForAnnotation:busLocationDot].image=[UIImage imageNamed:@"buspositiondot135.png"];
else if(roundedDirection==180)
[mapView viewForAnnotation:busLocationDot].image=[UIImage imageNamed:@"buspositiondot180.png"];
else if(roundedDirection==225)
[mapView viewForAnnotation:busLocationDot].image=[UIImage imageNamed:@"buspositiondot225.png"];
else if(roundedDirection==270)
[mapView viewForAnnotation:busLocationDot].image=[UIImage imageNamed:@"buspositiondot270.png"];
else if(roundedDirection==315)
[mapView viewForAnnotation:busLocationDot].image=[UIImage imageNamed:@"buspositiondot315.png"];
CLLocationCoordinate2D currentBusLocation = CLLocationCoordinate2DMake(latitude, longitude);
NSLog(@"setting coord %f & %f",currentBusLocation.latitude,currentBusLocation.longitude);
[UIView animateWithDuration:0.5 animations:^{
[busLocationDot setCoordinate:currentBusLocation];
}];
}
}
addAnnotation
, вы передаете[vehicles objectForKey:[route objectForKey:@"RouteID"]]
, но если он уже существует, вы используете[vehicles objectForKey:@"RouteID"]
. Почему это? Что именно происходит при вызовеsetCoordinate
(ничего, сбой, другое)? - person   schedule 03.12.2012