iOS版 - 的MKMapView - 可拖动的注解 [英] iOS - MKMapView - Draggable Annotations

查看:731
本文介绍了iOS版 - 的MKMapView - 可拖动的注解的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有注释蓄势待发,但试图找出如何使它可拖动与我的code:

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

}

在此先感谢!

推荐答案

要做出注解拖动,设置注释的视图拖动属性

To make an annotation draggable, set the annotation view's draggable property to YES.

这是在 viewForAnnotation 委托方法正常​​完成。

This is normally done in the viewForAnnotation delegate method.

例如:

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

结果
如果您需要在用户停止拖动并删除批注处理,请参阅:结果
<一href=\"http://stackoverflow.com/questions/3998765/how-to-manage-drag-and-drop-for-mkannotationview-on-ios\">how管理拖放进行MKAnnotationView在IOS?

结果
此外,您的注释对象(一个实现 MKAnnotation )应该有一个可设置协调属性。您正在使用 MKPointAnnotation 类,它的确实现了 setCoordinate 所以这部分的已经照顾。


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.

这篇关于iOS版 - 的MKMapView - 可拖动的注解的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆