1

マップでユーザーの位置を取得したいのですが、ユーザーはマップをテーピングすることで位置を変更できます。問題は、メソッド (void)mapView:(MKMapView *)mapView_ didUpdateUserLocation が呼び出された場所をタップすると、現在の場所と彼がタップした場所の両方がマップに表示されることです!! 私は1つの場所だけが欲しい>>>

ここにコードがあります

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.mapView.delegate = self;
locationManager = [[CLLocationManager alloc] init];
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
[mapView addGestureRecognizer:singleTap];

}

- (void)mapView:(MKMapView *)mapView_ didUpdateUserLocation:(MKUserLocation *)userLocation
{
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;

[locationManager startUpdatingLocation];
longitudeLabel.text = [NSString stringWithFormat:@"%.8f", userLocation.coordinate.longitude];
latitudeLabel.text = [NSString stringWithFormat:@"%.8f", userLocation.coordinate.latitude];

MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(userLocation.coordinate, 800, 800);
[mapView setRegion:[mapView regionThatFits:region] animated:YES];

// Add an annotation
MKPointAnnotation *point = [[MKPointAnnotation alloc] init];
point.coordinate = userLocation.coordinate;
point.title = @"Where am I?";
point.subtitle = @"I'm here!!!";

[mapView addAnnotation:point];
}

- (void)handleSingleTap:(UIGestureRecognizer *)sender
{
CLLocationCoordinate2D coord = [mapView convertPoint:[sender locationInView:mapView] toCoordinateFromView:mapView];
NSLog(@"Map touched %f, %f.", coord.latitude, coord.longitude);
longitudeLabel.text = [NSString stringWithFormat:@"%.8f", coord.longitude];
latitudeLabel.text = [NSString stringWithFormat:@"%.8f", coord.latitude];


MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(coord, 800, 800);
[mapView setRegion:[mapView regionThatFits:region] animated:YES];

// Add an annotation
MKPointAnnotation *point = [[MKPointAnnotation alloc] init];
point.coordinate = coord;
point.title = @"Where am I?";
point.subtitle = @"I'm here!!!";
[mapView removeAnnotations:[mapView annotations]];
[mapView addAnnotation:point];

}
4

1 に答える 1

1

まず、各ページで 1 つの質問をします。私が質問 2 に正解し、他の人が質問 1 に正解した場合、どちらが正解としてマークされますか?

次に、このメソッドdidUpdateUserLocationは、デバイスの場所に関する新しい情報を取得するときに iOS によって呼び出されます。そこに新しい注釈を追加する必要はありません。追加しないと、デバイスが移動するたびに大量の注釈が作成されます。ユーザーの現在地を表示したい場合はmapView.showsUserLocation = YES;. ユーザーがタップし、新しい注釈を作成したら、 userLocation を に設定してオフにすることができますNO

于 2013-07-21T21:02:43.187 に答える