注释不拖动

时间:2013-04-06 12:51:22

标签: map drag-and-drop annotations mapkit viewcontroller

MapPin.h:

#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>

@interface MapPin : NSObject<MKAnnotation> {
CLLocationCoordinate2D coordinate;
NSString *title;
NSString *subtitle;
}
@property (nonatomic,readwrite,assign)CLLocationCoordinate2D coordinate;
- (id)initWithCoordinates:(CLLocationCoordinate2D)location placeName:(NSString  *)placeName description:(NSString *)description;
-(void)setCoordinate:(CLLocationCoordinate2D)newCoordinate;
@end

这是MapPin.m

#import "MapPin.h"

@implementation MapPin
@synthesize coordinate;

-(NSString *)subtitle{
return nil;
}
-(NSString *)title{
     return nil;
    }
-(id)initWithCoordinates:(CLLocationCoordinate2D)location placeName:(NSString *)placeName description:(NSString *)description{
    coordinate=location;
    return self;

}
-(CLLocationCoordinate2D)location{
    return coordinate;
}
-(void)setCoordinate:(CLLocationCoordinate2D)newCoordinate{
     coordinate=newCoordinate;
}

@end

在ViewController.h中,我采用了MKMapViewDelegate协议。 这是ViewController.m:

-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{
    MKPinAnnotationView *pin = (MKPinAnnotationView *)[self.MyMap dequeueReusableAnnotationViewWithIdentifier:@"myPin"];
    if(pin == nil){
        pin=[[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:@"myPin"];

    }
     else{
        pin.annotation=annotation;
    }
    pin.draggable=YES;
return  pin;
}
//Add Location button
 - (IBAction)AddPin:(id)sender {
    CLLocationCoordinate2D center;
    center.latitude=Locate.location.coordinate.latitude;
    center.longitude=Locate.location.coordinate.longitude;
    MKCoordinateRegion region;
    region.center=center;
    region.span.latitudeDelta=0.2f;
    region.span.longitudeDelta=0.2f;
    MapPin *myPin = [[MapPin alloc]initWithCoordinates:self.MyMap.centerCoordinate placeName:@"LoMinder!" description:@"Set Location for LoMinder!"];
    [_MyMap addAnnotation:myPin];
 }
 //Dropping the pin:
    - (void)mapView:(MKMapView *)mapView
 annotationView:(MKAnnotationView *)annotationView
 didChangeDragState:(MKAnnotationViewDragState)newState
   fromOldState:(MKAnnotationViewDragState)oldState
{
    //Region
    MKCoordinateRegion myRegion;
    //Center
    CLLocationCoordinate2D center;
    if (newState == MKAnnotationViewDragStateStarting)
    {
         [_MyMap setRegion:myRegion animated:YES];
    }
    if(newState == MKAnnotationViewDragStateDragging)
    {
        myRegion.center=annotationView.annotation.coordinate;
        myRegion.span.latitudeDelta=0.2f;
        myRegion.span.longitudeDelta=0.2f;
        [_MyMap setRegion:myRegion animated:YES];
    }
    if(newState==MKAnnotationViewDragStateEnding)
    {
        arrived=self.MyMap.centerCoordinate;
        center=annotationView.annotation.coordinate;
        myRegion.center=center;
        myRegion.span.latitudeDelta=0.2f;
        myRegion.span.longitudeDelta=0.2f;
        [_MyMap setRegion:myRegion animated:YES];
     }
  }

///// 我还将MapPin.h导入ViewController。 当我运行我的应用程序时,我应按下ord er中的Add Location按钮获取注释引脚,我按下按钮,注释引脚出现在地图上,但问题是当我尝试拖动它时,它似乎是不可拖的。 请大家帮忙。 谢谢:))

1 个答案:

答案 0 :(得分:1)

通过更改MapPin.m中的title方法解决 让它返回任何东西,但没有。