2009-07-27 9 views
9

カスタムAnnotationViewが新しい座標で更新されます。しかし問題は、MKMapViewを使っていくつかの操作を行った後にのみ視覚的に更新されることです。ズームまたは移動。 地図上の視覚的な位置を手動で更新するにはどうすればよいですか?ピン移動後のMKMapViewの更新

PS。私は地域を現在の地図の地域に変更しようとしました。しかし、それはズームを変更します。おかしいです。

[mapView setRegion:[mapView region] animated:YES]; 
+0

私の答えは次のリンクからご覧になれます。 http://stackoverflow.com/a/24564868/665961 –

答えて

19

私は数時間の研究の後にちょっとシャックになります。答えはちょうどです:

[mapView setCenterCoordinate:mapView.region.center animated:NO]; 

なぜ私に聞かないのですか?しかし、それはmapviewを更新し、それは私が必要としていたものです。ここで

+1

UPD:SDK 3.1ではサポートされていません。まだ研究中です。 – slatvick

+1

3.1.2でも動作しません。 – bradheintz

+0

これはちょうど私のバグを修正!ありがとう!!!!! –

-7

はMapAnnotationへのインタフェースです:

// CSMapAnnotation.h 
// mapLines 
// Created by Craig on 5/15/09. 
// Copyright 2009 Craig Spitzkoff. All rights reserved. 

#import <Foundation/Foundation.h> 
#import <MapKit/MapKit.h>  

// types of annotations for which we will provide annotation views. 
typedef enum { 
    MapAnnotationTypeStart = 0, 
    MapAnnotationTypeEnd = 1, 
    MapAnnotationTypeImage = 2 
} MapAnnotationType; 

@interface MapAnnotation : NSObject <MKAnnotation> 
{ 
    CLLocationCoordinate2D _coordinate; 
    MapAnnotationType  _annotationType; 
    NSString*    _title; 
    NSString*    _subtitle; 
    NSString*    _userData; 
    NSString*    speed; 
    NSString*    identifier; 
} 

@property (nonatomic, retain) NSString *speed; 
@property (nonatomic, retain) NSString *identifier; 

-(id) initWithCoordinate:(CLLocationCoordinate2D)coordinate 
    annotationType: (MapAnnotationType) annotationType 
    title: (NSString*) title 
    subtitle: (NSString*) subtitle 
    speed: (NSString *) speed 
    identifier: (NSString *) identifier;  
-(id) setWithCoordinate: (CLLocationCoordinate2D) coordinate 
    annotationType: (MapAnnotationType) annotationType 
    title: (NSString*) title 
    subtitle: (NSString*) subtitle 
    speed: (NSString*) speed 
    identifier: (NSString*) identifier;  
@property MapAnnotationType annotationType; 
@property (nonatomic, retain) NSString* userData;  
@end  

そして、ここでは実装です:

// CSMapAnnotation.m 
// mapLines 
// Created by Craig on 5/15/09. 
// Copyright 2009 Craig Spitzkoff. All rights reserved. 

#import "MapAnnotation.h"  

@implementation MapAnnotation 

@synthesize coordinate  = _coordinate; 
@synthesize annotationType = _annotationType; 
@synthesize userData  = _userData; 
@synthesize speed; 
@synthesize identifier; 

-(id) initWithCoordinate:(CLLocationCoordinate2D)coordinate 
    annotationType: (MapAnnotationType) annotationType 
    title: (NSString*)title 
    subtitle: (NSString*) subtitle 
    speed: (NSString *) speedz 
    identifier: (NSString *) identifierz 
{ 
    self = [super init]; 
    _coordinate = coordinate; 
    _title = [title retain]; 
    _subtitle = [subtitle retain]; 
    _annotationType = annotationType; 
    speed = speedz; 
    identifier = identifierz; 
    return self; 
}  
-(id) setWithCoordinate:(CLLocationCoordinate2D)coordinate 
    annotationType: (MapAnnotationType) annotationType 
    title: (NSString*) title 
    subtitle: (NSString*) subtitle 
    speed: (NSString*) speedz 
    identifier: (NSString*) identifierz 
{ 
    _coordinate = coordinate; 
    _title = [title retain]; 
    _subtitle = [subtitle retain]; 
    _annotationType = annotationType; 
    speed = speedz; 
    identifier = identifierz;  
    return self; 
}  
-(NSString*) title 
{ 
    return _title; 
}  
-(NSString*) subtitle 
{ 
    return _subtitle; 
}  
-(void) dealloc 
{ 
    [_title release]; 
    [_userData release]; 
    [super dealloc]; 
}  
@end 
12

MKMapViewKVOを経由して、注釈の座標プロパティを観察します。適切なKVOプロトコルを遵守して、座標を更新する前後に、willChangeValueForKey:didChangeValueForKey:のキーパスを@"coordinate"として送信するだけで済みます。

titleおよびsubtitleも、MKMapViewによって観察される。あなたがそれらを更新し、吹き出しの値があなたの部分の任意の努力もせずに自動的に変更したい場合はそう、ちょうど同じことを行います。call willChangeValueForKey:didChangeValueForKey:

+0

これは本当に知って良いことです、ありがとう。 – samvermette

+0

'setCenterCoordinate:animated:'は動作しますが、ここでもKVOのやり方が有効であり、より適切であり、 'MKMapView'が複数の観測からの更新をより良く調整することを可能にします。 –

-2

あなたは注釈を削除し、再度追加することはできません理由はありません。これはおそらく地図全体を動かすよりも、たとえ偽の動きであっても、もっとパフォーマンスが良いでしょう。

+0

誰かがこれがなぜ落とされたのか説明したいですか?これは少なくとももう一つの選択肢であり、私はそれがより効果的だと主張するでしょう。 –

+0

このスレッドを発見したほとんどの人は、すでにこれを試していたと確信しています。私は知っていた。これは奇妙な動作を引き起こします。 –

2

ここでの答えは、MapViewまたは注釈を更新しないことです!

MKAnnotationの座標プロパティにはKVOがあります。マップ上に必要なオブジェクトのidポインタをマップビューに追加し、座標プロパティを新しい位置に更新するだけで、MKMapViewがあなたのために残ります。

無料で昼食を取ることができます。

9

あなたの注釈をスレッドから追加すると、うまくいきません。 私は同じ問題を抱えていたし、ちょうど次のように注釈を追加して、私の機能を包むが

[self performSelectorOnMainThread:@selector(addCameraIconOnMain:) obj waitUntilDone:true]; 

-(void) addCameraIconOnMain:(myobjecttype*)obj 
{ 
    // this isnt the entire function, customize for your own purpose..... 
    [mapView addAnnotation:annotation]; 
} 
0

を働いていた私は、非同期呼び出し、少なくとも0.5遅延でこのエラーを解決しました。

例えば:「redrawPins」が追加され、ピンを除去する機能である[self performSelector:@selector(redrawPins) withObject:nil afterDelay:0.5];

関連する問題