私は地図上にユーザーの場所を表示する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;
}