Commit 6b7e7214 authored by Qiu Xiang's avatar Qiu Xiang

实现 iOS 自定义 marker

parent 06b46218
...@@ -2,6 +2,7 @@ import React, {PropTypes, Component} from 'react' ...@@ -2,6 +2,7 @@ import React, {PropTypes, Component} from 'react'
import { import {
View, View,
UIManager, UIManager,
Platform,
NativeModules, NativeModules,
findNodeHandle, findNodeHandle,
requireNativeComponent, requireNativeComponent,
...@@ -14,11 +15,18 @@ class Overlay extends Component { ...@@ -14,11 +15,18 @@ class Overlay extends Component {
componentDidUpdate() { componentDidUpdate() {
setTimeout(() => { setTimeout(() => {
UIManager.dispatchViewManagerCommand( switch (Platform.OS) {
findNodeHandle(this), case 'android':
UIManager.AMapOverlay.Commands.update, UIManager.dispatchViewManagerCommand(
null, findNodeHandle(this),
) UIManager.AMapOverlay.Commands.update,
null,
)
break;
case 'ios':
NativeModules.AMapOverlayManager.update(findNodeHandle(this))
break;
}
}, 0) }, 0)
} }
......
#import <Foundation/Foundation.h> #import <Foundation/Foundation.h>
#import <MAMapKit/MAMapKit.h> #import <MAMapKit/MAMapKit.h>
#import <React/RCTComponent.h> #import <React/RCTComponent.h>
#import "AMapView.h"
#import "AMapOverlay.h"
#pragma ide diagnostic ignored "OCUnusedPropertyInspection" #pragma ide diagnostic ignored "OCUnusedPropertyInspection"
@class AMapView; @class AMapView;
@class AMapOverlay;
@interface AMapMarker : MAAnnotationView <MAAnnotation> @interface AMapMarker : MAAnnotationView <MAAnnotation, AMapOverlayDelegate>
@property(nonatomic, assign) CLLocationCoordinate2D coordinate; @property(nonatomic, assign) CLLocationCoordinate2D coordinate;
@property(nonatomic, copy) NSString *title; @property(nonatomic, copy) NSString *title;
@property(nonatomic, copy) NSString *subtitle; @property(nonatomic, copy) NSString *subtitle;
@property(nonatomic, strong) AMapView *mapView; @property(nonatomic, strong) AMapView *mapView;
@property(nonatomic, assign) BOOL _active;
@property (nonatomic, copy) RCTBubblingEventBlock onPress; @property (nonatomic, copy) RCTBubblingEventBlock onPress;
@property (nonatomic, copy) RCTBubblingEventBlock onInfoWindowPress; @property (nonatomic, copy) RCTBubblingEventBlock onInfoWindowPress;
......
#import "AMapMarker.h" #import "AMapMarker.h"
#import "AMapView.h"
#pragma ide diagnostic ignored "OCUnusedMethodInspection" #pragma ide diagnostic ignored "OCUnusedMethodInspection"
@implementation AMapMarker { @implementation AMapMarker {
MAAnnotationView *_pinView; MAAnnotationView *_pinView;
AMapOverlay *_iconView;
BOOL _active;
} }
- (void)setActive:(BOOL)active { - (void)setActive:(BOOL)active {
self._active = active; _active = active;
if (active) { if (active) {
[self.mapView selectAnnotation:self animated:YES]; [self.mapView selectAnnotation:self animated:YES];
} }
...@@ -27,6 +28,9 @@ ...@@ -27,6 +28,9 @@
_pinView = [[MAPinAnnotationView alloc] initWithAnnotation:self reuseIdentifier: nil]; _pinView = [[MAPinAnnotationView alloc] initWithAnnotation:self reuseIdentifier: nil];
_pinView.annotation = self; _pinView.annotation = self;
} }
if (_iconView != nil) {
_pinView.image = nil;
}
_pinView.zIndex = self.zIndex; _pinView.zIndex = self.zIndex;
_pinView.draggable = self.draggable; _pinView.draggable = self.draggable;
_pinView.canShowCallout = self.canShowCallout; _pinView.canShowCallout = self.canShowCallout;
...@@ -34,7 +38,23 @@ ...@@ -34,7 +38,23 @@
} }
- (BOOL)active { - (BOOL)active {
return self._active; return _active;
}
- (void)insertReactSubview:(id<RCTComponent>)subview atIndex:(NSInteger)atIndex {
if ([subview isKindOfClass:[AMapOverlay class]]) {
_iconView = (AMapOverlay *)subview;
_iconView.delegate = self;
}
}
#pragma mark AMapOverlayDelegate
- (void)update {
UIGraphicsBeginImageContextWithOptions([_iconView bounds].size, NO, 0.0f);
[_iconView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
_pinView.image = image;
} }
@end @end
#import <Foundation/Foundation.h>
#import <React/RCTView.h>
@protocol AMapOverlayDelegate <NSObject>
@optional
- (void)update;
@end
@interface AMapOverlay : RCTView
@property(nonatomic, strong) id delegate;
- (void)update;
@end
\ No newline at end of file
#import "AMapOverlay.h"
@implementation AMapOverlay {
}
- (void)update {
[self.delegate update];
}
@end
\ No newline at end of file
#import <React/RCTViewManager.h>
#import <React/RCTUIManager.h>
#import "AMapOverlay.h"
#pragma ide diagnostic ignored "OCUnusedClassInspection"
@interface AMapOverlayManager : RCTViewManager
@end
@implementation AMapOverlayManager {
}
RCT_EXPORT_MODULE()
- (UIView *)view {
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];
}];
}
@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