Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/118.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何使自定义注释可拖动_Ios_Objective C_Mkmapview_Mkannotation - Fatal编程技术网

Ios 如何使自定义注释可拖动

Ios 如何使自定义注释可拖动,ios,objective-c,mkmapview,mkannotation,Ios,Objective C,Mkmapview,Mkannotation,我需要具有不同pin图像的MKAnnotation。 因此,我创建了以下内容: @interface NavigationAnnotation : NSObject <MKAnnotation> - (id)initWithName:(NSString*)name address:(NSString*)address coordinate:(CLLocationCoordinate2D)coordinate; ... @interface NavigationAnnotation (

我需要具有不同pin图像的MKAnnotation。
因此,我创建了以下内容:

@interface NavigationAnnotation : NSObject <MKAnnotation>
- (id)initWithName:(NSString*)name address:(NSString*)address coordinate:(CLLocationCoordinate2D)coordinate;
...
@interface NavigationAnnotation ()
@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSString *address;
@property (nonatomic, assign) CLLocationCoordinate2D theCoordinate;
@end

@implementation NavigationAnnotation


- (id)initWithName:(NSString*)name address:(NSString*)address coordinate:(CLLocationCoordinate2D)coordinate {
    if ((self = [super init])) {
        if ([name isKindOfClass:[NSString class]]) {
            self.name = name;
        } else {
            self.name = @"Unknown charge";
        }
        self.address = address;
        self.theCoordinate = coordinate;
    }
    return self;
}

- (NSString *)title {
    return _name;
}

- (NSString *)subtitle {
    return _address;
}

- (CLLocationCoordinate2D)coordinate {
    return _theCoordinate;
}
@界面导航说明:NSObject
-(id)initWithName:(NSString*)名称地址:(NSString*)地址坐标:(CLLocationCoordinate2D)坐标;
...
@接口导航说明()
@属性(非原子,副本)NSString*名称;
@属性(非原子,副本)NSString*地址;
@属性(非原子,赋值)ClLocationCoordinated2和Coordinate;
@结束
@实现导航注释
-(id)initWithName:(NSString*)名称地址:(NSString*)地址坐标:(CLLocationCoordinate2D)坐标{
if((self=[super init])){
if([name iskindof类:[NSString类]]){
self.name=名称;
}否则{
self.name=@“未知费用”;
}
self.address=地址;
self.theCoordinate=坐标;
}
回归自我;
}
-(NSString*)标题{
返回_name;
}
-(NSString*)副标题{
返回地址;
}
-(CLLocationCoordinate2D)坐标{
返回协调;
}
并添加如下内容:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{

    static NSString *identifier_OrderAnnotation = @"NavigateAnnotation";

    if ([annotation isKindOfClass:[MKUserLocation class]])
        return nil;

    if ([annotation isKindOfClass:[NavigationAnnotation class]]) {

        MKAnnotationView *annotationView = (MKAnnotationView *) [_mapView dequeueReusableAnnotationViewWithIdentifier:identifier_OrderAnnotation];
        if (annotationView == nil) {
            annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier_OrderAnnotation];
            annotationView.enabled = YES;
            annotationView.canShowCallout = YES;
            annotationView.draggable = YES;
            annotationView.image = [UIImage imageNamed:@"myimage.png"];
        } else {
            annotationView.annotation = annotation;
        }

        return annotationView;
    }
}
-(MKAnnotationView*)地图视图:(MKMapView*)地图视图注释:(id)注释{
静态NSString*标识符_OrderAnnotation=@“NavigateAnotation”;
if([annotation isKindOfClass:[MKUserLocation类]])
返回零;
if([annotation IsKindof类:[NavigationAnnotation类]]){
MKAnnotationView*annotationView=(MKAnnotationView*)[\u地图视图排队可重用annotationView with identifier:identifier\u OrderAnnotation];
如果(注释视图==nil){
annotationView=[[MKAnnotationView alloc]initWithAnnotation:annotation重用标识符:标识符\u OrderAnnotation];
annotationView.enabled=是;
annotationView.canShowCallout=是;
annotationView.draggable=是;
annotationView.image=[UIImage ImageName:@“myimage.png”];
}否则{
annotationView.annotation=注释;
}
返回注释视图;
}
}

注释在地图上显示良好,但由于某些原因不可拖动:(

如前所述,要使注释可拖动,必须实现
设置坐标:
方法

此外,从iOS 7开始,您可能还需要实现
mapView:annotationView:didChangeDragState:
方法(请参阅和)

您可以自己显式地实现
setCoordinate:
方法,或者只声明一个可写的
coordinate
属性(命名与此完全相同)并合成它(getter和setter方法将自动为您实现)

(请注意,如果使用预定义的
MKPointAnnotation
类,则不需要这样做,因为该类已经实现了可设置的
coordinate
属性。)


NavigationAnnotation
类中,要实现显式的手动解决方案以使用现有的
Coordinate
属性,只需
setCoordinate:
方法添加到类实现中(保留现有的getter方法):


您可能还需要在实现地图视图委托的类中实现
didChangeDragState:
方法(与具有
viewForAnnotation
方法的类相同)否则,拖动后,注释视图将在地图上方的适当位置悬停,即使它在地图下方平移或缩放。该方法的示例实现如下所示:


刚刚经历了这个问题。。。 经过一些努力,原因不同了(标题、坐标都可以),所以在这里添加可能的原因

在我的注释视图中,我确实覆盖了
-(void)setSelected:(BOOL)selected

不调用
[super-setSelected:selected];

这就防止了拖动的发生…

添加setCoordinate方法的一个很好的地方。这就是我的问题。
-(void)setCoordinate:(CLLocationCoordinate2D)newCoordinate
{
    self.theCoordinate = newCoordinate;
}
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)annotationView didChangeDragState:(MKAnnotationViewDragState)newState fromOldState:(MKAnnotationViewDragState)oldState
{
    if (newState == MKAnnotationViewDragStateStarting)
    {
        annotationView.dragState = MKAnnotationViewDragStateDragging;
    }
    else if (newState == MKAnnotationViewDragStateEnding || newState == MKAnnotationViewDragStateCanceling)
    {
        annotationView.dragState = MKAnnotationViewDragStateNone;
    }
}