У меня эта проблема уже пару недель, и я до сих пор не нашел ответа. на моем MapView у меня есть пользовательские аннотации, и когда я нажимаю кнопку «перезагрузки», вся информация верна, как в аннотации «title, subtitle». но аннотация изменилась. аннотации находятся в NSMutableArray
, и я уверен, что проблема, с которой я вращаюсь вокруг этого. вот код, который я использую, чтобы перезагрузить аннотации.custom Аннотации, которые переключаются при перезагрузке на MKMapView
так что просто не путайте, мои пользовательские аннотации работают нормально, когда я сначала загружаю mapView. Но как только я нажму кнопку перезагрузки, все данные аннотации, такие как «местоположение, название, подзаголовок», все правильно, только фактическая аннотация изменилась. Как и все аннотации, были изменены. , если кто-нибудь может помочь, он будет очень признателен! благодаря!
- (IBAction)refreshMap:(id)sender {
NSArray *annotationsOnMap = myMapView.annotations;
[myMapView removeAnnotations:annotationsOnMap];
[locations removeAllObjects];
[citiesArray removeAllObjects];
[self retrieveData];
}
-(void) retrieveData {
userLAT = [NSString stringWithFormat:@"%f", myMapView.userLocation.coordinate.latitude];
userLNG = [NSString stringWithFormat:@"%f", myMapView.userLocation.coordinate.longitude];
NSString *fullPath = [mainUrl stringByAppendingFormat:@"map_json.php?userID=%@&lat=%@&lng=%@",theUserID,userLAT,userLNG];
NSURL * url =[NSURL URLWithString:fullPath];
NSData *data = [NSData dataWithContentsOfURL:url];
json =[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
citiesArray =[[NSMutableArray alloc]init];
for (int i = 0; i < json.count; i++)
{
//create city object
NSString * eID =[[json objectAtIndex:i] objectForKey:@"userid"];
NSString * eAddress =[[json objectAtIndex:i] objectForKey:@"full_address"];
NSString * eHost =[[json objectAtIndex:i] objectForKey:@"username"];
NSString * eLat =[[json objectAtIndex:i] objectForKey:@"lat"];
NSString * eLong =[[json objectAtIndex:i] objectForKey:@"lng"];
NSString * eName =[[json objectAtIndex:i] objectForKey:@"Restaurant_name"];
NSString * eState = [[json objectAtIndex:i] objectForKey:@"type"];
NSString * annotationPic = [[json objectAtIndex:i] objectForKey:@"Annotation"];
NSString * eventID = [[json objectAtIndex:i] objectForKey:@"id"];
//convert lat and long from strings
float floatLat = [eLat floatValue];
float floatLONG = [eLong floatValue];
City * myCity =[[City alloc] initWithRestaurantID: (NSString *) eID andRestaurantName: (NSString *) eName andRestaurantState: (NSString *) eState andRestaurantAddress: (NSString *) eAddress andRestaurantHost: eHost andRestaurantLat: (NSString *) eLat andRestaurantLong: (NSString *) eLong];
//Add our city object to our cities array
// Do any additional setup after loading the view.
[citiesArray addObject:myCity];
//Annotation
locations =[[NSMutableArray alloc]init];
CLLocationCoordinate2D location;
Annotation * myAnn;
//event1 annotation
myAnn =[[Annotation alloc]init];
location.latitude = floatLat;
location.longitude = floatLONG;
myAnn.coordinate = location;
myAnn.title = eName;
myAnn.subtitle = eHost;
myAnn.type = eState;
myAnn.AnnotationPicture = annotationPic;
myAnn.passEventID = eventID;
myAnn.hotZoneLevel = hotZone;
[locations addObject:myAnn];
[self.myMapView addAnnotations:locations];
}
}
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
if([annotation isKindOfClass:[MKUserLocation class]])
return nil;
static NSString *annotationIdentifier = @"AnnotationIdentifier";
MKAnnotationView *annotationView = (MKAnnotationView *) [self.myMapView
dequeueReusableAnnotationViewWithIdentifier:annotationIdentifier];
if (!annotationView)
{
annotationView = [[MKAnnotationView alloc]
initWithAnnotation:annotation
reuseIdentifier:annotationIdentifier];
NSString *restaurant_Icon = ((Annotation *)annotation).AnnotationPicture;
NSString *restaurant_Callout = [NSString stringWithFormat:@"mini.%@",restaurant_Icon];
UIImage *oldImage = [UIImage imageNamed:restaurant_Icon];
UIImage *newImage;
CGSize newSize = CGSizeMake(75, 75);
newImage = [oldImage imageScaledToFitSize:newSize]; // uses MGImageResizeScale
annotationView.image= newImage;
annotationView.canShowCallout = YES;
UIImage *Mini_oldImage = [UIImage imageNamed:event_Callout];
UIImage *Mini_newImage;
CGSize Mini_newSize = CGSizeMake(30,30);
Mini_newImage = [Mini_oldImage imageScaledToFitSize:Mini_newSize]; // uses MGImageResizeScale
UIImageView *finalMini_callOut = [[UIImageView alloc] initWithImage:Mini_newImage];
annotationView.leftCalloutAccessoryView = finalMini_callOut;
UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
annotationView.rightCalloutAccessoryView = rightButton;
}
else
{
annotationView.annotation = annotation;
}
return annotationView;
}