注释不能拖 [英] Annotation not dragging
问题描述
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
and here's the 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:
In the ViewController.h i've adopted the MKMapViewDelegate protocol. and here's the 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。
当我运行我的应用程序,我应该preSS的添加位置按钮ORD ER拿到注释针,我preSS按钮,注释销出现在地图上,但问题是,当我尝试拖动它,它似乎是取消拖动。
请人一些帮助。
谢谢:)
///// I've also imported the MapPin.h into the ViewController. When I run my app, I should press the Add Location button in ord er to get the annotation pin, I press the button, the annotation pin appears on the map, but the problem is that when i try to drag it, it seems to be un-draggable. Please people some help. Thank you :)
推荐答案
由MapPin.m更改标题的方法解决
让它返回任何东西,但无所获。
Solved by changing the title method in the MapPin.m Make it return anything but nil.
这篇关于注释不能拖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!