gpt4 book ai didi

iphone - 点击 map 图钉给出错误信息

转载 作者:行者123 更新时间:2023-12-01 17:42:47 25 4
gpt4 key购买 nike

我遇到的问题是,当我单击图钉时,它会给出与同一图钉相关的错误信息。我认为引脚的索引可能与数组的索引不同。

这是代码:

- (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];

}
}
}

对不起我的英语不好,如果你不明白这个问题,请不要贬低,只是问,我总是在附近回答。

最好的祝福

最佳答案

didSelectAnnotationView ,这段代码:

NSInteger index = [mapView.annotations indexOfObject:view.annotation]      
DisplayMap *annotation = (DisplayMap *)view.annotation;
NSMutableDictionary *item = [allMapUsers objectAtIndex:index];

并不总是有效,因为 map View 的 annotations数组位于 没有办法保证使注释的顺序与您添加它们的顺序相同。 (有关这一点的详细信息,请参见 MKMapView annotations changing/losing order?How to reorder MKMapView annotations array。)

根本无法假设 index mapView.annotations 中的注释数组与 allMapUsers 中注释源数据的索引相同。大批。

相反,您可以做的是在注释本身中保留对源对象的引用。

例如,添加 NSMutableDictionary属性(property)到您的 DisplayMap类(class):
@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;

如果出队返回一个以前使用过的 View ,它是 annotation属性仍将指向它之前使用的注解。使用出队 View 时,必须更新其 annotation当前注释的属性:
pinView = (MKAnnotationView *)[mapView dequeueReusableAnnotation...
if ( pinView == nil )
pinView = [[MKAnnotationView alloc] ...
else
pinView.annotation = annotation; // <-- add this
pinView.canShowCallout = YES;

MKMapView Off Screen Annotation Image Incorrect更多细节。

关于iphone - 点击 map 图钉给出错误信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13956642/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com