gpt4 book ai didi

iOS - MKMapView - 可拖动注释

转载 作者:可可西里 更新时间:2023-11-01 03:27:01 24 4
gpt4 key购买 nike

我已经准备好注释了,但我想弄清楚如何用我的代码让它可以拖动:

-(IBAction) updateLocation:(id)sender{

MKCoordinateRegion newRegion;

newRegion.center.latitude = mapView.userLocation.location.coordinate.latitude;
newRegion.center.longitude = mapView.userLocation.location.coordinate.longitude;

newRegion.span.latitudeDelta = 0.0004f;
newRegion.span.longitudeDelta = 0.0004f;

[mapView setRegion: newRegion animated: YES];


CLLocationCoordinate2D coordinate;
coordinate.latitude = mapView.userLocation.location.coordinate.latitude;
coordinate.longitude = mapView.userLocation.location.coordinate.longitude;

MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];

[annotation setCoordinate: coordinate];
[annotation setTitle: @"Your Car is parked here"];
[annotation setSubtitle: @"Come here for pepsi"];


[mapView addAnnotation: annotation];
[mapView setZoomEnabled: YES];
[mapView setScrollEnabled: YES];
}

提前致谢!

最佳答案

要使注释可拖动,请将注释 View 的 draggable 属性设置为YES

这通常在 viewForAnnotation 委托(delegate)方法中完成。

例如:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
if ([annotation isKindOfClass:[MKUserLocation class]])
return nil;

static NSString *reuseId = @"pin";
MKPinAnnotationView *pav = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:reuseId];
if (pav == nil)
{
pav = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:reuseId];
pav.draggable = YES;
pav.canShowCallout = YES;
}
else
{
pav.annotation = annotation;
}

return pav;
}


如果需要在用户停止拖放注释时进行处理,请参见:
how to manage drag and drop for MKAnnotationView on IOS?


此外,您的注释对象(实现 MKAnnotation 的对象)应该有一个可设置的 coordinate 属性。您正在使用确实实现了 setCoordinateMKPointAnnotation 类,因此该部分已经得到处理。

关于iOS - MKMapView - 可拖动注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11927692/

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