1

我希望地图能够获取用户的位置,他可以通过点击地图来更改位置。问题是当点击位置时,调用方法 (void)mapView:(MKMapView *)mapView_ didUpdateUserLocation 并且地图显示当前位置和他点击的位置!我只想要一个位置>>>

这是代码

- (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

首先,在每一页上问一个问题。如果我答对了第 2 题而其他人答对了第 1 题,那么哪一个会被标记为正确答案?

其次,didUpdateUserLocationiOS 在获取有关设备位置的新信息时调用该方法。您无需在此处添加新注释,否则每次设备移动时都会得到大量注释。如果要显示用户当前的位置 call mapView.showsUserLocation = YES;。一旦用户点击并且您创建了新注释,您可以通过将 userLocation 设置为 来关闭它NO

于 2013-07-21T21:02:43.187 回答