リフレッシュMKAnnotationViewにMKMapView
-
18-09-2019 - |
質問
思a-synchronically負荷の画像のための私のカスタムMKAnnotationView;私は既にEGOImageView-枠組みにとてもよく合いますUITableViews)が失敗するでMKMapView.画像の見込みができないのでリフレッシュして地図の上に- [myMap setNeedsDisplay]
は何も行いません。
解決
私はそれを自分自身を試していないが、それがうまくいくかもしれません。
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"];
}
すべての注釈の画像が更新されました。このメソッドを呼び出した後。
他のヒント
この私は他の方法が私のために動作しませんでした、注釈を更新するための唯一の信頼できる方法を見つけました
id <MKAnnotation> annotation;
// ...
[mapView removeAnnotation:annotation];
[mapView addAnnotation:annotation];
このコードは行います - [MapViewの:viewForAnnotationは:]再び呼び出されると(あなたがdequeueReusableAnnotationViewWithIdentifierを使用している場合または多分再利用)ので、あなたの注釈ビューが再び作成され、正しいデータと
。これは私がMKAnnotationViewsの再描画を強制的に見つけた唯一の方法である。
//マップビューの力更新(別段の注釈ビューが再描画されません)
CLLocationCoordinate2Dセンター= mapView.centerCoordinate;
mapView.centerCoordinate =センター
役に立たないようだが、それが動作します。
これは私のために働いた。
#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];
}
(私はこの記事がの答えをzerodivに関係することを示すためにどのように表示されません。)私は、前に、正しいを復元し、無意味が異なる座標を強制的に、種類のキックスタートを追加することで動作するようにする方法をzerodivの取得することができました1。画面がまったくので、最初の場所で点滅しない。
CLLocationCoordinate2D center = mapView.centerCoordinate;
CLLocationCoordinate2D forceChg;
forceChg.latitude = 0;
forceChg.longitude = curLongNum;
mapView.centerCoordinate = forceChg;
mapView.centerCoordinate = center;
たかったのでリフレッシュユーザのロケートピンとの新たな選択したアバターに表れます。また別のための高速解法る場合があります。私は2つの画面があります。の選定アバターのためのユーザー表示に関す。
迅速かつ5 変更ができますユーザーの位置の画像の作成 MKAnnotationView
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
if annotation is MKUserLocation {
let userIdentifier = "UserLocation"
var userView = mapView.dequeueReusableAnnotationView(withIdentifier: userIdentifier)
if userView == nil {
userView = MKAnnotationView(annotation: annotation, reuseIdentifier: userIdentifier)
} else {
userView?.annotation = annotation
}
userView!.image = UIImage(named: "avatar1") //your image
return userView
}
return nil
}
なユーザーの変更アバターになっても、他のものとは、例えば、"avatar2"いのでリフレッシュ。
するためのリフレッシュユーザーの位置イメージを追加このコード viewWillAppear()
self.mapView.showsUserLocation = false
self.mapView.showsUserLocation = true