我很难弄清楚我在这里做错了什么......
我通过循环向我的 map View 添加注释,但注释图像每次都是完全随机的...当我 NSLog
顺序时,它是随机的 - 我不确定这是否是问题所在。
- (MKAnnotationView *)mapView:(MKMapView *)mapView1 viewForAnnotation:(id<MKAnnotation>)annotation {
if ([annotation isKindOfClass:[MKUserLocation class]])
return nil;
if ([[annotation subtitle] isEqual:@"Bar"]) {
MKAnnotationView *view = nil;
view = [self.mapView dequeueReusableAnnotationViewWithIdentifier:@"myAnnotationIdentifier"];
if (!view) {
// Could not reuse a view ...
// Creating a new annotation view
view = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"myAnnotationIdentifier"];
view.enabled = YES;
view.canShowCallout = YES;
view.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
view.image = [UIImage imageNamed:@"beer.png"];
}
return view;
}
else if ([[annotation subtitle] isEqual:@"Club"]) {
MKAnnotationView *view = nil;
view = [self.mapView dequeueReusableAnnotationViewWithIdentifier:@"myAnnotationIdentifier"];
if (!view) {
// Could not reuse a view ...
// Creating a new annotation view
view = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"myAnnotationIdentifier"];
view.enabled = YES;
view.canShowCallout = YES;
view.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
view.image = [UIImage imageNamed:@"clubs.png"];
}
return view;
}
}
view.image 是完全随机的...clubs.png
或 beer.png
..
如何正确制作?
这就是我添加注释的方式:
- (void)barLoop {
for (int i = 0; i<barArray.count; i++) {
int index = [[barArray objectAtIndex:i]intValue];
NSString *lati = [[[self.usersLocationArray objectAtIndex:index]
valueForKeyPath:@"items.Latitude"]componentsJoinedByString:@""];
NSString *longi = [[[self.usersLocationArray objectAtIndex:index]
valueForKeyPath:@"items.Longitude"]componentsJoinedByString:@""];
NSString *barNavn = [[[self.usersLocationArray objectAtIndex:index] valueForKeyPath:@"items.Navn"]componentsJoinedByString:@""];
float latitude = [lati floatValue];
float longitude = [longi floatValue];
MKCoordinateRegion region = { {0.0, 0.0} , {0.0, 0.0} };
region.center.latitude = latitude;
region.center.longitude = longitude;
region.span.longitudeDelta = 0.20f;
region.span.latitudeDelta = 0.20f;
[mapView setRegion:region animated:NO];
CLLocationCoordinate2D location;
location.latitude = latitude;
location.longitude = longitude;
Annotation *ann = [[Annotation alloc]initWithPosition:location];
ann.title = barNavn;
ann.subtitle = @"Bar";
[self.mapView addAnnotation:ann];
}
}
提前致谢:)
最佳答案
您有两种类型的注释,但您仅在最初创建注释时设置图像。因此,如果酒吧的注释滚动离开 map View ,而俱乐部的另一个注释滚动,则可能会重用酒吧的俱乐部注释 View 。
有两种方法可以解决此问题:
对两种类型的注释 View 使用不同的
reuseIdentifier
参数;或无论您调用
dequeueReusableAnnotationViewWithIdentifier
是否成功返回值,都设置/重置注释 View 的图像
。
不相关,但您的 viewForAnnotation
方法应该:
在检查
@"Bar"
与@"Club 时,您可能需要使用
。isEqualToString
而不是isEqual
“如果这些
if
子句都不返回 true,请确保返回nil
(这种情况永远不会发生,但尽管如此,您在此例程中仍有潜在路径)您忽略返回任何值)。我猜想,如果您通过静态分析器(Xcode 的“产品”菜单上的“分析”)运行代码,您会注意到这一点。更微妙的观察,但我可能不会依赖注释的
subtitle
来确定要使用哪个注释 View ,而是依赖其中之一有两个注释子类,一个用于酒吧,一个用于俱乐部;或
在现有的
Annotation
类中有一个自定义属性,指示它是酒吧还是俱乐部(我可能会使用enum
来表示)。
在未来的某个日期,您可能希望将标注的副标题用于“酒吧”与“俱乐部”之外的其他内容(例如,也许是酒吧/俱乐部的地址?),并且当前模型似乎将 UI 混为一谈属性(即标注中显示的内容)和状态属性(即控制要使用哪种注释 View 类型的变量)。没什么大不了的,只是一个建议。
关于ios - MapView随机标注图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18946010/