gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 13:09:21 24 4
gpt4 key购买 nike

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

这是代码

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

}

最佳答案

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

其次,方法 didUpdateUserLocation 会在 iOS 获取有关设备位置的新信息时调用。您不需要在那里添加新的注释,否则每次设备移动时您都会得到很多注释。如果您想显示用户的当前位置,请调用 mapView.showsUserLocation = YES;。一旦用户点击并且您创建了新注释,您可以通过将其设置为 NO 来关闭 userLocation。

关于ios - Mapkit 添加新注释并删除旧注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17756757/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com