xcode iOS - MKMapView - 可拖动注释
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11927692/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
iOS - MKMapView - Draggable Annotations
提问by Kenny L.
I have the annotation ready to go, but trying to figure out on how to make it draggable with my code:
我已经准备好了注释,但我试图弄清楚如何使用我的代码使其可拖动:
-(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];
}
Thanks in advance!
提前致谢!
回答by
To make an annotation draggable, set the annotation view'sdraggable
property to YES
.
要使注释可拖动,请将注释视图的draggable
属性设置为YES
。
This is normally done in the viewForAnnotation
delegate method.
这通常在viewForAnnotation
委托方法中完成。
For example:
例如:
- (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;
}
If you need to handle when the user stops dragging and drops the annotation, see:
how to manage drag and drop for MKAnnotationView on IOS?
如果需要在用户停止拖放注解时进行处理,请参阅:
如何在IOS上管理MKAnnotationView的拖放?
In addition, your annotation object (the one that implements MKAnnotation
) should have a settable coordinate
property. You are using the MKPointAnnotation
class which does implement setCoordinate
so that part's already taken care of.
此外,您的注释对象(实现 的对象MKAnnotation
)应该有一个可设置的coordinate
属性。您正在使用MKPointAnnotation
确实实现的类,setCoordinate
因此该部分已经得到处理。