我已经有这个问题好几个星期了,但我仍然没有找到答案。在我的MapView上,我有自定义注释,当我点击"重新加载按钮"时,所有信息都是正确的,就像注释"标题,副标题"中一样。但是注释已经改变了。注释在NSMutableArray
中,我确信我的问题围绕着这个。这是我用来重新加载注释的代码。
因此,为了防止混淆,当我第一次加载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;
}
如果没有其他内容,则根据注释设置图标和标注,但仅在注释未出列的情况下在viewForAnnotation
中进行设置。您确实希望在if块之外执行任何特定于注释的自定义,以防重用注释视图。
与您报告的问题无关,还有一些其他观察结果:
-
您可能应该异步执行
retrieveData
,这样就不会将主线程与数据检索/解析联系在一起。继续执行将条目添加到数组的调度,并将注释添加到主队列中的映射,但网络方面的工作肯定应该异步完成。 -
您可能应该检查以确保
data
不是nil
(例如,没有网络连接或其他网络错误),因为如果您传递nil
值,JSONObjectWithData
将崩溃。 -
您似乎没有必要使用
locations
,因为您要为JSON中的每个条目重置它。您可以(a)完全撤销locations
,只需将myAnn
对象添加到地图的注释中;或者(b)在CCD_ 12循环之前初始化CCD_。但是,维护这个ivar,但只使用最后一个注释填充它,可能会产生误导。