单个 MKMapView 地图上的多个不同大小的注释
我有一个 iPhone 应用程序,可以在地图上向用户显示位置,目标是标记的大小与位置的受欢迎程度成正比。更受欢迎的地方会得到稍大的注释。
我有一个自定义 MKAnnotation 和 MKAnnotationView 来显示标记。我尝试使用自定义 MKAnnotationView 来渲染不同大小的标记,但始终渲染相同大小的图像。
这是班级。
欢迎任何意见或建议:
#import "MapAnnotationView.h"
@implementation MapAnnotationView
- (id)initWithAnnotation:(id)annotation reuseIdentifier:(NSString *)reuseIdentifier
{
MapAnnotation * myAnnotation = (MapAnnotation *) annotation;
self = [super initWithAnnotation:myAnnotation reuseIdentifier:reuseIdentifier];
// Figure out if in 'normal' mode or 'compare' mode
// normal
if (myAnnotation.visited != nil) {
if ([myAnnotation.visited boolValue] == YES) {
self.image = [UIImage imageNamed:@"visited.png"];
} else if ([myAnnotation.visited boolValue] == NO) {
self.image = [UIImage imageNamed:@"unvisited.png"];
}
// compare
} else {
// On both maps
if ([myAnnotation.onLoggedInUsersMap boolValue] == YES && [myAnnotation.onComparisonMap boolValue] == YES) {
if ([myAnnotation.mapCount intValue] == 1) {
self.image = [UIImage imageNamed:@"both_small.png"];
} if ([myAnnotation.mapCount intValue] < 5) {
self.image = [UIImage imageNamed:@"both_medium.png"];
} else {
self.image = [UIImage imageNamed:@"both_large.png"];
}
// Only on comparison's map
} else if ([myAnnotation.onLoggedInUsersMap boolValue] == NO && [myAnnotation.onComparisonMap boolValue] == YES) {
if ([myAnnotation.mapCount intValue] == 1) {
self.image = [UIImage imageNamed:@"compare_small.png"];
} if ([myAnnotation.mapCount intValue] < 5) {
self.image = [UIImage imageNamed:@"compare_medium.png"];
} else {
self.image = [UIImage imageNamed:@"compare_large.png"];
}
// Only on owner's map
} else {
if ([myAnnotation.mapCount intValue] == 1) {
self.image = [UIImage imageNamed:@"owner_small.png"];
} if ([myAnnotation.mapCount intValue] < 5) {
self.image = [UIImage imageNamed:@"owner_medium.png"];
} else {
self.image = [UIImage imageNamed:@"owner_large.png"];
}
}
}
return self;
}
@end
这是 viewForAnnotation 方法:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation {
if ([annotation class] == MKUserLocation.class) {
return nil;
}
MapAnnotationView *aView = [[MapAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"location"];
[aView setEnabled:YES];
[aView setCanShowCallout:YES];
aView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
return aView;
}
I've an iPhone app that shows users locations on a map and the goal is that the size of the marker is proportional to the popularity of the location. More popular places get a slightly larger annotation.
I've a custom MKAnnotation and MKAnnotationView to display markers. I've tried to use the custom MKAnnotationView to render the different sized markers, but the same sized images are always rendered.
Here's the class.
Any advice or suggestions are welcome:
#import "MapAnnotationView.h"
@implementation MapAnnotationView
- (id)initWithAnnotation:(id)annotation reuseIdentifier:(NSString *)reuseIdentifier
{
MapAnnotation * myAnnotation = (MapAnnotation *) annotation;
self = [super initWithAnnotation:myAnnotation reuseIdentifier:reuseIdentifier];
// Figure out if in 'normal' mode or 'compare' mode
// normal
if (myAnnotation.visited != nil) {
if ([myAnnotation.visited boolValue] == YES) {
self.image = [UIImage imageNamed:@"visited.png"];
} else if ([myAnnotation.visited boolValue] == NO) {
self.image = [UIImage imageNamed:@"unvisited.png"];
}
// compare
} else {
// On both maps
if ([myAnnotation.onLoggedInUsersMap boolValue] == YES && [myAnnotation.onComparisonMap boolValue] == YES) {
if ([myAnnotation.mapCount intValue] == 1) {
self.image = [UIImage imageNamed:@"both_small.png"];
} if ([myAnnotation.mapCount intValue] < 5) {
self.image = [UIImage imageNamed:@"both_medium.png"];
} else {
self.image = [UIImage imageNamed:@"both_large.png"];
}
// Only on comparison's map
} else if ([myAnnotation.onLoggedInUsersMap boolValue] == NO && [myAnnotation.onComparisonMap boolValue] == YES) {
if ([myAnnotation.mapCount intValue] == 1) {
self.image = [UIImage imageNamed:@"compare_small.png"];
} if ([myAnnotation.mapCount intValue] < 5) {
self.image = [UIImage imageNamed:@"compare_medium.png"];
} else {
self.image = [UIImage imageNamed:@"compare_large.png"];
}
// Only on owner's map
} else {
if ([myAnnotation.mapCount intValue] == 1) {
self.image = [UIImage imageNamed:@"owner_small.png"];
} if ([myAnnotation.mapCount intValue] < 5) {
self.image = [UIImage imageNamed:@"owner_medium.png"];
} else {
self.image = [UIImage imageNamed:@"owner_large.png"];
}
}
}
return self;
}
@end
And here's the viewForAnnotation method:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation {
if ([annotation class] == MKUserLocation.class) {
return nil;
}
MapAnnotationView *aView = [[MapAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"location"];
[aView setEnabled:YES];
[aView setCanShowCallout:YES];
aView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
return aView;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是 init 方法的大条件中缺少三个
else
:Two separate, unrelated issues are:
viewForAnnotation
中的内存泄漏,因为它没有释放aView
viewForAnnotation
中使用dequeueReusableAnnotationViewWithIdentifier
所以
viewForAnnotation
方法应该如下所示:The problem is three missing
else
's in the big conditional in the init method:Two separate, unrelated issues are:
viewForAnnotation
because it is not releasingaView
dequeueReusableAnnotationViewWithIdentifier
inviewForAnnotation
So the
viewForAnnotation
method should look like this: