Commit d68065ec authored by Qiu Xiang's avatar Qiu Xiang

调整 iOS 自定义 Marker 的实现

1. Overlay 使用原有的实现,didUpdateReactSubviews 并不能带来实际作用
2. 拖拽时变回大头针,是因为 MAPinAnnotationView 内部机制问题,resetImage 并不能真正的解决问题。对于自定义 marker,应该使用 MAAnnotationView。
parent af952675
...@@ -8,11 +8,11 @@ ...@@ -8,11 +8,11 @@
@property(nonatomic, copy) RCTBubblingEventBlock onDragStart; @property(nonatomic, copy) RCTBubblingEventBlock onDragStart;
@property(nonatomic, copy) RCTBubblingEventBlock onDrag; @property(nonatomic, copy) RCTBubblingEventBlock onDrag;
@property(nonatomic, copy) RCTBubblingEventBlock onDragEnd; @property(nonatomic, copy) RCTBubblingEventBlock onDragEnd;
@property(nonatomic, assign) BOOL dragging;
- (CLLocationCoordinate2D)coordinate; - (CLLocationCoordinate2D)coordinate;
- (NSString *)title; - (NSString *)title;
- (NSString *)subtitle; - (NSString *)subtitle;
- (void)resetImage;
- (BOOL)active; - (BOOL)active;
- (MAAnnotationView *)annotationView; - (MAAnnotationView *)annotationView;
......
...@@ -3,24 +3,18 @@ ...@@ -3,24 +3,18 @@
#pragma ide diagnostic ignored "OCUnusedMethodInspection" #pragma ide diagnostic ignored "OCUnusedMethodInspection"
@implementation AMapMarker { @implementation AMapMarker {
MAPinAnnotationView *_annotationView; MAAnnotationView *_annotationView;
MAPointAnnotation *_annotation; MAPointAnnotation *_annotation;
AMapOverlay *_overlay; AMapOverlay *_marker;
AMapOverlay *_callout; AMapOverlay *_callout;
AMapView *_mapView; AMapView *_mapView;
BOOL _active; BOOL _active;
UIImage *_image; NSString *_title;
CGPoint _centerOffset; NSString *_subtitle;
CGPoint _calloutOffset; CLLocationCoordinate2D _coordinate;
} MAPinAnnotationColor _pinColor;
BOOL _draggable;
- (instancetype)init { NSInteger _zIndex;
self = [super init];
_annotation = [MAPointAnnotation new];
_annotationView = [[MAPinAnnotationView alloc] initWithAnnotation:_annotation reuseIdentifier:nil];
_annotationView.canShowCallout = YES;
return self;
} }
- (MAAnnotationView *)annotationView { - (MAAnnotationView *)annotationView {
...@@ -28,29 +22,27 @@ ...@@ -28,29 +22,27 @@
} }
- (NSString *)title { - (NSString *)title {
return _annotation.title; return _title;
} }
- (NSString *)subtitle { - (NSString *)subtitle {
return _annotation.subtitle; return _subtitle;
} }
- (CLLocationCoordinate2D)coordinate { - (CLLocationCoordinate2D)coordinate {
return _annotation.coordinate; return _coordinate;
} }
- (void)setCoordinate:(CLLocationCoordinate2D)coordinate { - (void)setCoordinate:(CLLocationCoordinate2D)coordinate {
_coordinate = coordinate;
_annotation.coordinate = coordinate; _annotation.coordinate = coordinate;
} }
- (void)setTitle:(NSString *)title { - (void)setTitle:(NSString *)title {
_title = title;
_annotation.title = title; _annotation.title = title;
} }
- (void)setSubtitle:(NSString *)subtitle {
_annotation.subtitle = subtitle;
}
- (void)setActive:(BOOL)active { - (void)setActive:(BOOL)active {
_active = active; _active = active;
if (active) { if (active) {
...@@ -60,22 +52,18 @@ ...@@ -60,22 +52,18 @@
} }
} }
- (void)resetImage {
if (_image) {
_annotationView.image = _image;
_annotationView.centerOffset = _centerOffset;
}
}
- (void)setIcon:(MAPinAnnotationColor)color { - (void)setIcon:(MAPinAnnotationColor)color {
_annotationView.pinColor = color; _pinColor = color;
((MAPinAnnotationView *) _annotationView).pinColor = color;
} }
- (void)setDescription:(NSString *)description { - (void)setDescription:(NSString *)description {
_annotationView.annotation.subtitle = description; _subtitle = description;
_annotation.subtitle = description;
} }
- (void)setDraggable:(BOOL)draggable { - (void)setDraggable:(BOOL)draggable {
_draggable = draggable;
_annotationView.draggable = draggable; _annotationView.draggable = draggable;
} }
...@@ -84,6 +72,7 @@ ...@@ -84,6 +72,7 @@
} }
- (void)setZIndex:(NSInteger)zIndex { - (void)setZIndex:(NSInteger)zIndex {
_zIndex = zIndex;
_annotationView.zIndex = zIndex; _annotationView.zIndex = zIndex;
} }
...@@ -96,54 +85,54 @@ ...@@ -96,54 +85,54 @@
} }
- (void)insertReactSubview:(id <RCTComponent>)subview atIndex:(NSInteger)atIndex { - (void)insertReactSubview:(id <RCTComponent>)subview atIndex:(NSInteger)atIndex {
if ([subview isKindOfClass:[AMapOverlay class]]) { if (atIndex == 0) {
if (atIndex == 0) { _annotation = [MAPointAnnotation new];
_overlay = (AMapOverlay *) subview; _annotation.coordinate = _coordinate;
_overlay.delegate = self; _annotation.title = _title;
_annotationView.image = nil; _annotation.subtitle = _subtitle;
_image = nil;
} if ([subview isKindOfClass:[AMapOverlay class]]) {
if (atIndex == 1) { _marker = (AMapOverlay *) subview;
// TODO: customCalloutView 的位置不太对 _marker.delegate = self;
_callout = (AMapOverlay *) subview; _annotationView = [[MAAnnotationView alloc] initWithAnnotation:_annotation reuseIdentifier:nil];
} else {
// f**king amap only support button as callout, so below it is. _annotationView = [[MAPinAnnotationView alloc] initWithAnnotation:_annotation reuseIdentifier:nil];
// more funny thing is callout offset shown right when using button. unbelievable!!! ((MAPinAnnotationView *) _annotationView).pinColor = _pinColor;
_callout.delegate = self;
UIButton *button = [[UIButton alloc] initWithFrame:CGRectZero];
[button addSubview:(UIView *) subview];
_annotationView.customCalloutView = [[MACustomCalloutView alloc] initWithCustomView:button];
} }
_annotationView.canShowCallout = YES;
_annotationView.draggable = _draggable;
_annotationView.zIndex = _zIndex;
} else if (atIndex == 1 && [subview isKindOfClass:[AMapOverlay class]]) {
_callout = (AMapOverlay *) subview;
_callout.delegate = self;
UIButton *button = [[UIButton alloc] initWithFrame:CGRectZero];
[button addSubview:_callout];
_annotationView.customCalloutView = [[MACustomCalloutView alloc] initWithCustomView:button];
} }
} }
#pragma mark AMapOverlayDelegate #pragma mark AMapOverlayDelegate
- (void)update:(AMapOverlay *)overlay { - (void)update:(AMapOverlay *)overlay {
if (overlay == _overlay) { if (self.dragging) {
dispatch_async(dispatch_get_main_queue(), ^{ return;
UIGraphicsBeginImageContextWithOptions([_overlay bounds].size, NO, 0.0f);
[_overlay.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
_annotationView.image = image;
_image = image;
});
} }
}
- (void)updateLayout:(AMapOverlay *)overlay {
CGFloat width = CGRectGetWidth(overlay.bounds); CGFloat width = CGRectGetWidth(overlay.bounds);
CGFloat height = CGRectGetHeight(overlay.bounds); CGFloat height = CGRectGetHeight(overlay.bounds);
if (overlay == _overlay) { if (overlay == _marker) {
// change default image center UIGraphicsBeginImageContextWithOptions([_marker bounds].size, NO, 0.0f);
_centerOffset = CGPointMake(0, -height / 2); [_marker.layer renderInContext:UIGraphicsGetCurrentContext()];
_annotationView.centerOffset = _centerOffset; UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
_annotationView.image = image;
_annotationView.centerOffset = CGPointMake(0, -height / 2);
} else if (overlay == _callout) { } else if (overlay == _callout) {
// change default callout offset
_annotationView.customCalloutView.bounds = CGRectMake(0, 0, width, height); _annotationView.customCalloutView.bounds = CGRectMake(0, 0, width, height);
} }
} }
......
...@@ -5,10 +5,9 @@ ...@@ -5,10 +5,9 @@
@protocol AMapOverlayDelegate <NSObject> @protocol AMapOverlayDelegate <NSObject>
@optional @optional
- (void)update:(AMapOverlay *)overlay; - (void)update:(AMapOverlay *)overlay;
- (void)updateLayout:(AMapOverlay *)overlay;
@end @end
@interface AMapOverlay : RCTView @interface AMapOverlay : RCTView
@property(nonatomic, strong) id<AMapOverlayDelegate> delegate; @property(nonatomic, strong) id <AMapOverlayDelegate> delegate;
- (void)update; - (void)update;
@end @end
#import "AMapOverlay.h" #import "AMapOverlay.h"
#import <React/UIView+React.h>
@implementation AMapOverlay { @implementation AMapOverlay {
} }
- (void)update { - (void)update {
[self.delegate update:self]; [self.delegate update:self];
[self.delegate updateLayout:self];
}
- (void)didUpdateReactSubviews {
[super didUpdateReactSubviews];
[self update];
} }
@end @end
...@@ -9,7 +9,7 @@ ...@@ -9,7 +9,7 @@
@implementation AMapOverlayManager { @implementation AMapOverlayManager {
} }
RCT_EXPORT_MODULE(AMapOverlay) RCT_EXPORT_MODULE()
- (UIView *)view { - (UIView *)view {
return [AMapOverlay new]; return [AMapOverlay new];
...@@ -20,7 +20,6 @@ RCT_EXPORT_METHOD(update:(nonnull NSNumber *)reactTag) { ...@@ -20,7 +20,6 @@ RCT_EXPORT_METHOD(update:(nonnull NSNumber *)reactTag) {
id view = viewRegistry[reactTag]; id view = viewRegistry[reactTag];
[(AMapOverlay *) view update]; [(AMapOverlay *) view update];
}]; }];
} }
@end @end
#import <React/RCTViewManager.h>
#import <React/RCTUIManager.h> #import <React/RCTUIManager.h>
#import "AMapView.h" #import "AMapView.h"
#import "AMapMarker.h" #import "AMapMarker.h"
...@@ -135,11 +134,15 @@ RCT_EXPORT_METHOD(animateTo:(nonnull NSNumber *)reactTag data:(NSArray *)data) { ...@@ -135,11 +134,15 @@ RCT_EXPORT_METHOD(animateTo:(nonnull NSNumber *)reactTag data:(NSArray *)data) {
- (void)mapView:(MAMapView *)mapView annotationView:(MAAnnotationView *)view didChangeDragState:(MAAnnotationViewDragState)newState - (void)mapView:(MAMapView *)mapView annotationView:(MAAnnotationView *)view didChangeDragState:(MAAnnotationViewDragState)newState
fromOldState:(MAAnnotationViewDragState)oldState { fromOldState:(MAAnnotationViewDragState)oldState {
AMapMarker *marker = (AMapMarker *) view.annotation; AMapMarker *marker = (AMapMarker *) view.annotation;
marker.dragging = NO;
if (newState == MAAnnotationViewDragStateStarting && marker.onDragStart) { if (newState == MAAnnotationViewDragStateStarting && marker.onDragStart) {
marker.onDragStart(@{}); marker.onDragStart(@{});
} }
if (newState == MAAnnotationViewDragStateDragging && marker.onDrag) { if (newState == MAAnnotationViewDragStateDragging) {
marker.onDrag(@{}); marker.dragging = YES;
if (marker.onDrag) {
marker.onDrag(@{});
}
} }
if (newState == MAAnnotationViewDragStateEnding && marker.onDragEnd) { if (newState == MAAnnotationViewDragStateEnding && marker.onDragEnd) {
marker.onDragEnd(@{ marker.onDragEnd(@{
...@@ -147,9 +150,6 @@ RCT_EXPORT_METHOD(animateTo:(nonnull NSNumber *)reactTag data:(NSArray *)data) { ...@@ -147,9 +150,6 @@ RCT_EXPORT_METHOD(animateTo:(nonnull NSNumber *)reactTag data:(NSArray *)data) {
@"longitude": @(marker.coordinate.longitude), @"longitude": @(marker.coordinate.longitude),
}); });
} }
if (newState == MAAnnotationViewDragStateCanceling || newState == MAAnnotationViewDragStateEnding) {
[marker resetImage];
}
} }
@end @end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment