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

实现 iOS 自定义 marker

parent 06b46218
......@@ -2,6 +2,7 @@ import React, {PropTypes, Component} from 'react'
import {
View,
UIManager,
Platform,
NativeModules,
findNodeHandle,
requireNativeComponent,
......@@ -14,11 +15,18 @@ class Overlay extends Component {
componentDidUpdate() {
setTimeout(() => {
UIManager.dispatchViewManagerCommand(
findNodeHandle(this),
UIManager.AMapOverlay.Commands.update,
null,
)
switch (Platform.OS) {
case 'android':
UIManager.dispatchViewManagerCommand(
findNodeHandle(this),
UIManager.AMapOverlay.Commands.update,
null,
)
break;
case 'ios':
NativeModules.AMapOverlayManager.update(findNodeHandle(this))
break;
}
}, 0)
}
......
#import <Foundation/Foundation.h>
#import <MAMapKit/MAMapKit.h>
#import <React/RCTComponent.h>
#import "AMapView.h"
#import "AMapOverlay.h"
#pragma ide diagnostic ignored "OCUnusedPropertyInspection"
@class AMapView;
@class AMapOverlay;
@interface AMapMarker : MAAnnotationView <MAAnnotation>
@interface AMapMarker : MAAnnotationView <MAAnnotation, AMapOverlayDelegate>
@property(nonatomic, assign) CLLocationCoordinate2D coordinate;
@property(nonatomic, copy) NSString *title;
@property(nonatomic, copy) NSString *subtitle;
@property(nonatomic, strong) AMapView *mapView;
@property(nonatomic, assign) BOOL _active;
@property (nonatomic, copy) RCTBubblingEventBlock onPress;
@property (nonatomic, copy) RCTBubblingEventBlock onInfoWindowPress;
......
#import "AMapMarker.h"
#import "AMapView.h"
#pragma ide diagnostic ignored "OCUnusedMethodInspection"
@implementation AMapMarker {
MAAnnotationView *_pinView;
AMapOverlay *_iconView;
BOOL _active;
}
- (void)setActive:(BOOL)active {
self._active = active;
_active = active;
if (active) {
[self.mapView selectAnnotation:self animated:YES];
}
......@@ -27,6 +28,9 @@
_pinView = [[MAPinAnnotationView alloc] initWithAnnotation:self reuseIdentifier: nil];
_pinView.annotation = self;
}
if (_iconView != nil) {
_pinView.image = nil;
}
_pinView.zIndex = self.zIndex;
_pinView.draggable = self.draggable;
_pinView.canShowCallout = self.canShowCallout;
......@@ -34,7 +38,23 @@
}
- (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
#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