2012-05-02 24 views
3

ユーザーが地図に触れるとアノテーションを追加する際にいくつかの問題があります。MapKitとUIGestureRecognizerを使用して注釈ピンを追加する

私はUIGestureRecognizerを使用してユーザーの接触を検出しています。

長押しが検出されると、私はこの関数を呼んでいる:

- (void)handleLongPressGesture:(UIGestureRecognizer*)gestureRecognizer{ 
if (gestureRecognizer.state == UIGestureRecognizerStateEnded) return; 

NSLog(@"long press"); 

CGPoint touchPoint = [gestureRecognizer locationInView:mapView]; 
CLLocationCoordinate2D touchMapCoordinate = 
[mapView convertPoint:touchPoint toCoordinateFromView:mapView]; 

RdvAnnotation *rdvAnnotation = [[RdvAnnotation alloc] init]; 

[rdvAnnotation initWithCoordinate:touchMapCoordinate]; 

[mapView removeAnnotations:mapView.annotations]; 

[mapView addAnnotation:rdvAnnotation]; 

[rdvAnnotation release]; } 

私は缶は、コンソールでのNSLogを参照してrdvAnnotationは座標良いで初期化されます。

マップ上に注釈が表示されない理由がわかりません。

ここに私の- (MKAnnotationView *)mapView:(MKMapView *)theMapView viewForAnnotation:(id <MKAnnotation>)annotation方法です:あなたは上の二回のinitを呼び出している

RdvAnnotation *rdvAnnotation = [[RdvAnnotation alloc] init]; 
[rdvAnnotation initWithCoordinate:touchMapCoordinate]; 

:私はちょうど奇妙なようで何かに気づいた

- (void)viewDidLoad { 
[super viewDidLoad]; 

mapView = [[MKMapView alloc] initWithFrame:self.view.frame]; 
[mapView setShowsUserLocation:TRUE]; 
[mapView setMapType:MKMapTypeStandard]; 
[mapView setDelegate:self]; 

CLLocationManager *locationManager=[[CLLocationManager alloc] init]; 

[locationManager setDelegate:self]; 

[locationManager setDesiredAccuracy:kCLLocationAccuracyNearestTenMeters]; 

[locationManager startUpdatingLocation]; 

self.navigationItem.title = @"Rendez-vous" ;  
} 
+0

' - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id )注釈' MapViewデリゲートを実装しましたか?これは、実際のビューが作成される場所です。 – adig

+1

はい、私は質問を更新しました。あなたは間違ったことを見ますか? – Serviet

+0

大丈夫です。ジェスチャの場所に関連したもので、コンバージョンを調整できますか?座標がマップビューの可視領域にあるかどうか確認できますか? – adig

答えて

0

- (MKAnnotationView *)mapView:(MKMapView *)theMapView viewForAnnotation:(id <MKAnnotation>)annotation{ 
if ([annotation isKindOfClass:[MKUserLocation class]]) 
    return nil; 

if ([annotation isKindOfClass:[RdvAnnotation class]]) 
{ 
    static NSString* RdvAnnotationIdentifier = @"rdvAnnotationIdentifier"; 
    MKPinAnnotationView* pinView = (MKPinAnnotationView *) 
    [mapView dequeueReusableAnnotationViewWithIdentifier:RdvAnnotationIdentifier]; 

    if (!pinView) 
    { 
     MKPinAnnotationView* customPinView = [[[MKPinAnnotationView alloc] 
               initWithAnnotation:annotation reuseIdentifier:RdvAnnotationIdentifier] autorelease]; 
     customPinView.pinColor = MKPinAnnotationColorPurple; 
     customPinView.animatesDrop = YES; 
     customPinView.canShowCallout = YES; 

     return customPinView; 

    } 
    else 
    { 
     pinView.annotation = annotation; 
    } 
    return pinView; 
} 
return nil;} 

マイviewDidLoad方法注釈オブジェクト。

RdvAnnotation *rdvAnnotation = [[RdvAnnotation alloc] initWithCoordinate:touchMapCoordinate]]; 

EDIT: あなたは座標プロパティの値を、失うのinitを維持し、変更したくないあなたのinitメソッド内のコードをお持ちの場合:

あなたはそれをこの方法で行う必要があります
RdvAnnotation *rdvAnnotation = [[RdvAnnotation alloc] init]; 
rdvAnnotation.coordinate = touchMapCoordinate; 
+1

このエラーは修正されましたが、私の問題は解決されませんでした。まだ新しい注釈はありません。実際、最初のinitはオブジェクトを初期化し、2番目のinitはオブジェクト内に正しい座標を配置します。 – Serviet

+0

オーバーロードされたinitで重要なコードがあった場合は、私の編集で示唆した答えを答えてみてください。 – adig

+1

それでも試しましたが、まだ何もありません。しかし、私はあなたの助けに非常に感謝します。 – Serviet

0

viewDidLoadでは、新しいマップビューオブジェクトを作成しています。

最初に、この新しいマップビューオブジェクトはサブビューとしてself.viewに追加されていないため、メモリには存在していますが表示されません。

第2に、マップビューオブジェクトをInterface Builderのビューに配置しているため、新しいマップビューオブジェクトを作成する必要はありません。

おそらく起こっているのは、アノテーションを追加すると、アノテーションはメモリ内のマップビューオブジェクトに追加されますが、表示されているもの(IBで作成されたもの)ではありません。

viewDidLoadに新しいマップビューを作成する代わりに、mapView IBOutletをInterface Builderのマップビューに接続します。

+1

これで、ViewDidLoadでmapViewの宣言を削除しました。 mapViewはIBでうまく接続されていますが、新しい注釈はまだ表示されません。 – Serviet

+0

.hでは、mapViewは '@property(retain)IBOutlet MKMapView * mapView;と定義されています。IBではマップビューに接続されたFile's Ownerの' mapView'コンセントですか?ジェスチャ認識機能のNSLogがまだ表示されていますか?カウントが表示されていますか? – Anna

+1

.h mapViewは@property(retain)として定義されています。IBOutlet MKMapView * mapView; IBではmapViewアウトレットがマップビューに接続されます。マップ上のプレス(私はremoveAnnotationを削除しました)が増加すると、カウントが増加します。 – Serviet

関連する問題