我遇到的问题是,当我点击该引脚时,它会提供与该引脚相关的错误信息。我认为引脚的索引可能与数组的索引不一样。
以下是代码:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
MKAnnotationView *pinView = nil;
if(annotation != mapView.userLocation)
{
static NSString *defaultPinID = @"com.invasivecode.pin";
pinView = (MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
if ( pinView == nil )
pinView = [[MKAnnotationView alloc]
initWithAnnotation:annotation reuseIdentifier:defaultPinID];
pinView.canShowCallout = YES;
if ((annotation.coordinate.latitude == mapLatitude) && (annotation.coordinate.longitude == mapLongitude)) {
if ([estadoUser isEqualToString:@"online"])
{
// NSLog(@"ONLINE");
pinView.image = [UIImage imageNamed:@"1352472516_speech_bubble_green.png"]; //as suggested by Squatch
}else{
//NSLog(@"OFFLINE");
pinView.image = [UIImage imageNamed:@"1352472468_speech_bubble_red.png"];
}
}
UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[rightButton setTitle:annotation.title forState:UIControlStateNormal];
[rightButton addTarget:self
action:@selector(showDetails)
forControlEvents:UIControlEventTouchUpInside];
pinView.rightCalloutAccessoryView = rightButton;
} else {
[mapView.userLocation setTitle:@"I am here"];
}
return pinView;
}
-(void)showDetails
{
UIStoryboard* storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle:nil];
DMChatRoomViewController *_controller = [storyboard instantiateViewControllerWithIdentifier:@"DmChat"];
[self presentViewController:_controller animated:YES completion:nil];
}
-(void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
{
if ([view.annotation isKindOfClass:[DisplayMap class]])
{
NSInteger index = [mapView.annotations indexOfObject:view.annotation]
DisplayMap *annotation = (DisplayMap *)view.annotation;
NSMutableDictionary *item = [allMapUsers objectAtIndex:index];
//HERE DOES NOT DISPLAY THE INFO ON THE CORRECT PLACE
NSUserDefaults * standardUserDefaults = [NSUserDefaults standardUserDefaults];
[standardUserDefaults setObject:[[allMapUsers objectAtIndex:index] objectId] forKey:@"userSelecionadoParaChat"];
[standardUserDefaults synchronize];
}
}
-(void)reloadMap
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
for (int i=0; i<allMapUsers.count; i++)
{
NSMutableDictionary *item = [allMapUsers objectAtIndex:i];
NSLog(@"index=%i para objectID=%@",i,[[allMapUsers objectAtIndex:i] objectId]);
if (([[item valueForKey:@"estado"] isEqualToString:@"offline"] && [[defaults stringForKey:@"showOfflineUsers"] isEqualToString:@"no"]) || [[item valueForKey:@"estado"] isEqualToString:@""]) {
}else{
estadoUser = [item valueForKey:@"estado"];
[outletMapView setMapType:MKMapTypeStandard];
[outletMapView setZoomEnabled:YES];
[outletMapView setScrollEnabled:YES];
MKCoordinateRegion region = { {0.0, 0.0 }, { 0.0, 0.0 } };
region.center.latitude = [[item valueForKey:@"Latitude"] floatValue];
region.center.longitude = [[item valueForKey:@"Longitude"] floatValue];
region.span.longitudeDelta = 81;
region.span.latitudeDelta = 80;
[outletMapView setRegion:region animated:YES];
/////
mapLatitude = [[item valueForKey:@"Latitude"] floatValue];
mapLongitude = [[item valueForKey:@"Longitude"] floatValue];
CLLocationCoordinate2D locationco = {mapLatitude,mapLongitude};
ann = [[DisplayMap alloc] init];
ann.coordinate = locationco;
ann.title = [item valueForKey:@"username1"];
NSLog(@"ann.title=%@ para objectID=%@",[item valueForKey:@"username1"],[[allMapUsers objectAtIndex:i] objectId]);
ann.subtitle = [item valueForKey:@"estado"];
ann.coordinate = region.center;
[outletMapView addAnnotation:ann];
}
}
}
抱歉我的英文不好,如果你不明白这个问题请不要低估,只要问,我总是在回答。
最好的问候
答案 0 :(得分:6)
在didSelectAnnotationView
中,此代码:
NSInteger index = [mapView.annotations indexOfObject:view.annotation]
DisplayMap *annotation = (DisplayMap *)view.annotation;
NSMutableDictionary *item = [allMapUsers objectAtIndex:index];
并不总是有效,因为地图视图的annotations
数组位于 无法保证 的注释顺序与添加它们的顺序相同。 (有关此问题的详细信息,请参阅MKMapView annotations changing/losing order?和How to reorder MKMapView annotations array。)
您 根本不能假设 index
数组中注释的mapView.annotations
与注释的源数据的索引相同你的allMapUsers
数组。
你可以做的是在注释本身中保留对源对象的引用。
例如,向NSMutableDictionary
类添加DisplayMap
属性:
@property (nonatomic, retain) NSMutableDictionary *sourceDictionary;
创建注释时,请设置属性:
ann = [[DisplayMap alloc] init];
ann.sourceDictionary = item; // <-- keep ref to source item
ann.coordinate = locationco;
然后在didSelectAnnotationView
:
DisplayMap *annotation = (DisplayMap *)view.annotation;
NSMutableDictionary *item = annotation.sourceDictionary;
另一个可能的问题是viewForAnnotation
:
pinView = (MKAnnotationView *)[mapView dequeueReusableAnnotation...
if ( pinView == nil )
pinView = [[MKAnnotationView alloc] ...
pinView.canShowCallout = YES;
如果dequeue返回先前使用的视图,则它的annotation
属性仍将指向以前用于的注释。使用出列视图时,必须将其annotation
属性更新为当前注释:
pinView = (MKAnnotationView *)[mapView dequeueReusableAnnotation...
if ( pinView == nil )
pinView = [[MKAnnotationView alloc] ...
else
pinView.annotation = annotation; // <-- add this
pinView.canShowCallout = YES;