3

ユーザーが地図に触れたときに注釈を追加する際に問題が発生します。

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方法です:

- (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方法:

- (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" ;    
}   
4

2 に答える 2

0

奇妙に思えることに気づきました:

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

注釈オブジェクトで init を 2 回呼び出しています。このようにするだけです:

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

編集: 失いたくない init メソッドにコードがある場合は、init を保持し、座標プロパティの値を変更します。

RdvAnnotation *rdvAnnotation = [[RdvAnnotation alloc] init];
rdvAnnotation.coordinate = touchMapCoordinate;
于 2012-05-02T12:25:59.320 に答える
0

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

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

次に、Interface Builder のビューに既にマップ ビュー オブジェクトが配置されている可能性があるため、新しいオブジェクトを作成する必要はありません。

したがって、アノテーションを追加すると、メモリ内のマップ ビュー オブジェクトに追加されますが、表示されているオブジェクト (IB で作成されたオブジェクト) には追加されない可能性があります。

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

于 2012-05-02T13:54:59.327 に答える