111*_*110 4 objective-c mkmapview mkannotation ios
我需要具有不同引脚图像的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;
}
Run Code Online (Sandbox Code Playgroud)
并添加如下:
- (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;
}
}
Run Code Online (Sandbox Code Playgroud)
注释显示在地图上很好,但由于某种原因它不可拖动:(
小智 8
如前所述,对于可拖动的注释,它必须实现一个setCoordinate:方法.
此外,从iOS 7开始,您可能还需要实现该mapView:annotationView:didChangeDragState:方法(请参阅Draggable Pin在地图上没有固定位置,并且iOS MapKit拖动注释(MKAnnotationView)不再平移地图).
您可以setCoordinate:自己显式实现该方法,也可以只声明一个可写coordinate属性(命名完全相同)并合成它(并且将自动为您实现getter和setter方法).
(请注意,如果使用预定义的MKPointAnnotation类,则不需要执行此操作,因为该类已实现可设置的coordinate属性.)
在你的NavigationAnnotation班级,实行明确的,手动解决方案与现有的工作theCoordinate性质,只是添加的setCoordinate:方法,以你的类实现(保持现有的getter方法):
-(void)setCoordinate:(CLLocationCoordinate2D)newCoordinate
{
self.theCoordinate = newCoordinate;
}
Run Code Online (Sandbox Code Playgroud)
您可能还需要在实现didChangeDragState:地图视图委托的类中实现该方法(具有该viewForAnnotation方法的同一个),否则在拖动之后,注释视图将在地图上方就地悬停,即使在其下方进行平移或缩放时也是如此.Chris K.在他的回答中给出的方法的示例实现:
- (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;
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2909 次 |
| 最近记录: |