Commit b0bda04f authored by Qiu Xiang's avatar Qiu Xiang

重构 iOS Marker

ref #7
parent 0a1c8064
......@@ -70,6 +70,7 @@ export default class MarkerExample extends Component {
/>
<Marker
icon='green'
onInfoWindowPress={this._handleCustomInfoWindowPress}
coordinate={{
latitude: 39.806901,
longitude: 116.297972,
......
#import "AMapView.h"
#import "AMapOverlay.h"
@interface AMapMarker : UIView <MAAnnotation, AMapOverlayDelegate>
@interface AMapMarker : MAAnnotationView <MAAnnotation, AMapOverlayDelegate>
@property(nonatomic, copy) RCTBubblingEventBlock onPress;
@property(nonatomic, copy) RCTBubblingEventBlock onInfoWindowPress;
@property(nonatomic, copy) RCTBubblingEventBlock onDragStart;
@property(nonatomic, copy) RCTBubblingEventBlock onDrag;
@property(nonatomic, copy) RCTBubblingEventBlock onDragEnd;
@property(nonatomic, assign) BOOL dragging;
- (CLLocationCoordinate2D)coordinate;
- (NSString *)title;
- (NSString *)subtitle;
- (BOOL)active;
- (MAAnnotationView *)annotationView;
- (void)setMapView:(AMapView *)mapView;
- (void)setCoordinate:(CLLocationCoordinate2D)coordinate;
- (void)updateActive;
@end
#import <React/UIView+React.h>
#import "AMapMarker.h"
#pragma ide diagnostic ignored "OCUnusedMethodInspection"
@implementation AMapMarker {
MAAnnotationView *_annotationView;
MAPointAnnotation *_annotation;
AMapOverlay *_marker;
MAPinAnnotationView *_pinView;
MAPinAnnotationColor _pinColor;
MACustomCalloutView *_calloutView;
AMapOverlay *_callout;
AMapView *_mapView;
BOOL _active;
NSString *_title;
NSString *_subtitle;
CLLocationCoordinate2D _coordinate;
MAPinAnnotationColor _pinColor;
BOOL _draggable;
NSInteger _zIndex;
}
- (MAAnnotationView *)annotationView {
return _annotationView;
- (instancetype)init {
_annotation = [MAPointAnnotation new];
self = [super initWithAnnotation:_annotation reuseIdentifier:nil];
self.canShowCallout = YES;
[self addGestureRecognizer:[
[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(_handleTap:)]];
return self;
}
- (NSString *)title {
return _title;
return _annotation.title;
}
- (NSString *)subtitle {
return _subtitle;
return _annotation.subtitle;
}
- (CLLocationCoordinate2D)coordinate {
return _coordinate;
}
- (void)setCoordinate:(CLLocationCoordinate2D)coordinate {
_coordinate = coordinate;
_annotation.coordinate = coordinate;
return _annotation.coordinate;
}
- (void)setTitle:(NSString *)title {
_title = title;
_annotation.title = title;
}
- (void)setActive:(BOOL)active {
_active = active;
if (active) {
[_mapView selectAnnotation:self animated:YES];
} else {
[_mapView deselectAnnotation:self animated:YES];
}
}
- (void)setIcon:(MAPinAnnotationColor)color {
_pinColor = color;
((MAPinAnnotationView *) _annotationView).pinColor = color;
_pinView.pinColor = color;
}
- (void)setDescription:(NSString *)description {
_subtitle = description;
_annotation.subtitle = description;
}
- (void)setDraggable:(BOOL)draggable {
_draggable = draggable;
_annotationView.draggable = draggable;
- (void)setCoordinate:(CLLocationCoordinate2D)coordinate {
_annotation.coordinate = coordinate;
}
- (void)setInfoWindowEnabled:(BOOL)enabled {
_annotationView.canShowCallout = enabled;
- (void)setActive:(BOOL)active {
_active = active;
_pinView.selected = active;
self.selected = active;
[self updateActive];
}
- (void)updateActive {
dispatch_async(dispatch_get_main_queue(), ^{
if (_active) {
[_mapView selectAnnotation:self animated:YES];
} else {
[_mapView deselectAnnotation:self animated:YES];
}
});
}
- (void)setZIndex:(NSInteger)zIndex {
_zIndex = zIndex;
_annotationView.zIndex = zIndex;
- (void)setInfoWindowEnabled:(BOOL)enabled {
_pinView.canShowCallout = enabled;
self.canShowCallout = enabled;
}
- (void)setMapView:(AMapView *)mapView {
_mapView = mapView;
}
- (void)_handleTap:(UITapGestureRecognizer *)recognizer {
_active = YES;
[self updateActive];
if (self.onInfoWindowPress) {
self.onInfoWindowPress(@{});
}
}
- (BOOL)active {
return _active;
}
- (void)insertReactSubview:(id <RCTComponent>)subview atIndex:(NSInteger)atIndex {
if (atIndex == 0) {
_annotation = [MAPointAnnotation new];
_annotation.coordinate = _coordinate;
_annotation.title = _title;
_annotation.subtitle = _subtitle;
if ([subview isKindOfClass:[AMapOverlay class]]) {
_marker = (AMapOverlay *) subview;
_marker.delegate = self;
_annotationView = [[MAAnnotationView alloc] initWithAnnotation:_annotation reuseIdentifier:nil];
- (MAAnnotationView *)annotationView {
if (self.reactSubviews.count == 0) {
if (_pinView == nil) {
_pinView = [[MAPinAnnotationView alloc] initWithAnnotation:_annotation reuseIdentifier:nil];
_pinView.canShowCallout = YES;
_pinView.draggable = self.draggable;
_pinView.pinColor = _pinColor;
_pinView.customCalloutView = _calloutView;
}
return _pinView;
} else {
_annotationView = [[MAPinAnnotationView alloc] initWithAnnotation:_annotation reuseIdentifier:nil];
((MAPinAnnotationView *) _annotationView).pinColor = _pinColor;
return self;
}
}
- (void)insertReactSubview:(id <RCTComponent>)subview atIndex:(NSInteger)atIndex {
if (atIndex == 0 && subview.reactSubviews.count > 0) {
[super insertReactSubview:subview atIndex:atIndex];
}
_annotationView.canShowCallout = YES;
_annotationView.draggable = _draggable;
_annotationView.zIndex = _zIndex;
} else if (atIndex == 1 && [subview isKindOfClass:[AMapOverlay class]]) {
if (atIndex == 1 && [subview isKindOfClass:[AMapOverlay class]]) {
_callout = (AMapOverlay *) subview;
_callout.delegate = self;
UIButton *button = [UIButton new];
[button addSubview:_callout];
_annotationView.customCalloutView = [[MACustomCalloutView alloc] initWithCustomView:button];
_calloutView = [[MACustomCalloutView alloc] initWithCustomView:button];
self.customCalloutView = _calloutView;
}
}
- (void)didUpdateReactSubviews {
[super didUpdateReactSubviews];
self.bounds = self.reactSubviews[0].bounds;
}
#pragma mark AMapOverlayDelegate
- (void)update:(AMapOverlay *)overlay {
if (self.dragging) {
return;
}
CGFloat width = CGRectGetWidth(overlay.bounds);
CGFloat height = CGRectGetHeight(overlay.bounds);
if (overlay == _marker) {
UIGraphicsBeginImageContextWithOptions([_marker bounds].size, NO, 0.0f);
[_marker.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
_annotationView.image = image;
_annotationView.centerOffset = CGPointMake(0, -height / 2);
} else if (overlay == _callout) {
_annotationView.customCalloutView.bounds = CGRectMake(0, 0, width, height);
}
self.customCalloutView.bounds = overlay.bounds;
}
@end
......@@ -9,5 +9,4 @@
@interface AMapOverlay : RCTView
@property(nonatomic, strong) id <AMapOverlayDelegate> delegate;
- (void)update;
@end
#import <React/UIView+React.h>
#import "AMapOverlay.h"
@implementation AMapOverlay {
}
- (void)update {
- (void)didUpdateReactSubviews {
[super didUpdateReactSubviews];
[self.delegate update:self];
}
......
......@@ -15,11 +15,6 @@ RCT_EXPORT_MODULE()
return [AMapOverlay new];
}
RCT_EXPORT_METHOD(update:(nonnull NSNumber *)reactTag) {
[self.bridge.uiManager addUIBlock:^(__unused RCTUIManager *uiManager, NSDictionary<NSNumber *, UIView *> *viewRegistry) {
id view = viewRegistry[reactTag];
[(AMapOverlay *) view update];
}];
}
RCT_EXPORT_METHOD(update:(nonnull NSNumber *)reactTag) {}
@end
......@@ -16,7 +16,6 @@ RCT_EXPORT_MODULE()
- (UIView *)view {
AMapView *mapView = [AMapView new];
// when scroll view contains Map maybe we need stop Map Rending Cost.
mapView.runLoopMode = NSDefaultRunLoopMode;
mapView.allowsAnnotationViewSorting = YES;
mapView.centerCoordinate = CLLocationCoordinate2DMake(39.9042, 116.4074);
......@@ -105,13 +104,7 @@ RCT_EXPORT_METHOD(animateTo:(nonnull NSNumber *)reactTag params:(NSDictionary *)
- (MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id <MAAnnotation>)annotation {
if ([annotation isKindOfClass:[AMapMarker class]]) {
AMapMarker *marker = (AMapMarker *) annotation;
if (marker.active) {
dispatch_async(dispatch_get_main_queue(), ^{
// directly call selectAnnotation not work, because of there has no current AnnotationView presentation.
// use RUNLOOP
[mapView selectAnnotation:marker animated:YES];
});
}
[marker updateActive];
return marker.annotationView;
}
return nil;
......@@ -141,12 +134,10 @@ RCT_EXPORT_METHOD(animateTo:(nonnull NSNumber *)reactTag params:(NSDictionary *)
- (void)mapView:(MAMapView *)mapView annotationView:(MAAnnotationView *)view didChangeDragState:(MAAnnotationViewDragState)newState
fromOldState:(MAAnnotationViewDragState)oldState {
AMapMarker *marker = (AMapMarker *) view.annotation;
marker.dragging = NO;
if (newState == MAAnnotationViewDragStateStarting && marker.onDragStart) {
marker.onDragStart(@{});
}
if (newState == MAAnnotationViewDragStateDragging) {
marker.dragging = YES;
if (marker.onDrag) {
marker.onDrag(@{});
}
......
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