Mapkit添加新注释并删除旧注释

时间:2013-07-19 22:57:33

标签: ios objective-c mapkit

我想让地图获取用户的位置,他可以通过点击地图来更改位置。问题是当点击位置时方法(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];

}

1 个答案:

答案 0 :(得分:1)

首先,在每页上提出一个问题。如果我回答问题2并且其他人正确回答问题1,哪一个被标记为正确答案?

其次,当获取有关设备所在位置的新信息时,iOS会调用方法didUpdateUserLocation。您不需要在那里添加新注释,或者每次设备移动时都会有大量注释。如果您要显示用户的当前位置,请致电mapView.showsUserLocation = YES;。用户点击后您创建了新的注释,您可以通过将其设置为NO来关闭userLocation。