カスタムMKAnnotationViewの画像を同期的にロードしたいのですが。私はすでにEGOImageView-frameworkを使用しています(UITableViewsと非常によく合います)が、MKMapViewで機能させることができません。画像が読み込まれているようですが、地図上で画像を更新でき[myMap setNeedsDisplay]ません。何もしません。
7 に答える
私は自分で試したことはありませんが、うまくいくかもしれません:
MKMapView- (MKAnnotationView *)viewForAnnotation:(id <MKAnnotation>)annotationメソッドを使用して注釈用のビューを取得し、それに新しい画像を設定します。
編集:自分でやろうとしましたが、このアプローチはうまくいきました:
// Function where you got new image and want to set it to annotation view
for (MyAnnotationType* myAnnot in myAnnotationContainer){
MKAnnotationView* aView = [mapView viewForAnnotation: myAnnot];
aView.image = [UIImage imageNamed:@"myJustDownloadedImage.png"];
}
このメソッドを呼び出した後、すべての注釈画像が更新されました。
注釈を更新する信頼できる方法は1つしか見つかりませんでしたが、他の方法では機能しませんでした。注釈を削除して再度追加するだけです。
id <MKAnnotation> annotation;
// ...
[mapView removeAnnotation:annotation];
[mapView addAnnotation:annotation];
このコードにより、-[mapView:viewForAnnotation:]が再度呼び出されるため、正しいデータを使用して注釈ビューが再度作成されます(または、dequeueReusableAnnotationViewWithIdentifierを使用する場合は再利用されます)。
これは私のために働いた
#import "EGOImageView.h"
- (MKAnnotationView *)mapView:(MKMapView *)theMapView viewForAnnotation:(id <MKAnnotation>)annotation
{
//ENTER_METHOD;
if([annotation isKindOfClass:[MKUserLocation class]]) return nil;
MKPinAnnotationView *annView;
static NSString *reuseIdentifier = @"reusedAnnView";
annView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];
annView.canShowCallout = YES;
annView.calloutOffset = CGPointMake(-5.0f, 0.0f);
annView.opaque = NO;
annView.image = [[UIImage imageNamed:@"tempImage.png"] retain];
YourModel *p = annotation; // make this conform to <MKAnnotation>
EGOImageView *egoIV = [[EGOImageView alloc] initWithPlaceholderImage:[UIImage imageNamed:@"tempImage.png"]];
[egoIV setDelegate:self];
egoIV.imageURL = [NSURL URLWithString:p.theUrlToDownloadFrom];
[annView addSubview:egoIV];
[egoIV release];
return [annView autorelease];
}
これは、MKAnnotationViews の再描画を強制するために私が見つけた唯一の方法です。
// マップ ビューを強制的に更新します (そうしないと、注釈ビューは再描画されません)
CLLocationCoordinate2D センター = mapView.centerCoordinate;
mapView.centerCoordinate = センター;
役に立たないように見えますが、機能します。
(この投稿がzerodivの回答に関連していることを示す方法がわかりません。)ある種のキックスタートを追加し、無意味だが異なる座標を強制してから、以前の正しい座標を復元することで、zerodivの方法を機能させることができました。最初の場所のため、画面はまったく点滅しません。
CLLocationCoordinate2D center = mapView.centerCoordinate;
CLLocationCoordinate2D forceChg;
forceChg.latitude = 0;
forceChg.longitude = curLongNum;
mapView.centerCoordinate = forceChg;
mapView.centerCoordinate = center;