Commit 7fb5e774 authored by 7c00's avatar 7c00

Fix #116, close #125

parent 9e44e843
#import <React/RCTView.h> #import <React/RCTView.h>
@class AMapInfoWindow;
@protocol AMapInfoWindowDelegate <NSObject>
@optional
- (void)updateInfoWindow:(AMapInfoWindow *)overlay;
@end
@interface AMapInfoWindow : RCTView @interface AMapInfoWindow : RCTView
@property(nonatomic, strong) id <AMapInfoWindowDelegate> delegate;
@end @end
#import <React/UIView+React.h>
#import "AMapInfoWindow.h" #import "AMapInfoWindow.h"
@implementation AMapInfoWindow { @implementation AMapInfoWindow
}
- (void)didUpdateReactSubviews {
[super didUpdateReactSubviews];
[self.delegate updateInfoWindow:self];
}
@end @end
#import "AMapView.h" #import "AMapView.h"
#import "AMapInfoWindow.h" #import "AMapInfoWindow.h"
@interface AMapMarker : MAAnnotationView <MAAnnotation, AMapInfoWindowDelegate> @interface AMapMarker : UIView
@property(nonatomic, copy) RCTBubblingEventBlock onPress; @property(nonatomic, copy) RCTBubblingEventBlock onPress;
@property(nonatomic, copy) RCTBubblingEventBlock onInfoWindowPress; @property(nonatomic, copy) RCTBubblingEventBlock onInfoWindowPress;
...@@ -9,10 +9,9 @@ ...@@ -9,10 +9,9 @@
@property(nonatomic, copy) RCTBubblingEventBlock onDrag; @property(nonatomic, copy) RCTBubblingEventBlock onDrag;
@property(nonatomic, copy) RCTBubblingEventBlock onDragEnd; @property(nonatomic, copy) RCTBubblingEventBlock onDragEnd;
- (BOOL)active;
- (void)setActive:(BOOL)active;
- (MAAnnotationView *)annotationView; - (MAAnnotationView *)annotationView;
- (MAPointAnnotation *)annotation;
- (void)setMapView:(AMapView *)mapView; - (void)setMapView:(AMapView *)mapView;
- (void)updateActive; - (void)lockToScreen;
@end @end
...@@ -5,21 +5,20 @@ ...@@ -5,21 +5,20 @@
@implementation AMapMarker { @implementation AMapMarker {
MAPointAnnotation *_annotation; MAPointAnnotation *_annotation;
MAPinAnnotationView *_pinView; MAAnnotationView *_annotationView;
MAPinAnnotationColor _pinColor;
MACustomCalloutView *_calloutView; MACustomCalloutView *_calloutView;
UIImage *_image; UIView *_customView;
AMapInfoWindow *_callout;
AMapView *_mapView; AMapView *_mapView;
MAPinAnnotationColor _pinColor;
UIImage *_image;
BOOL _draggable;
CGPoint _centerOffset;
BOOL _active; BOOL _active;
} }
- (instancetype)init { - (instancetype)init {
_annotation = [MAPointAnnotation new]; _annotation = [MAPointAnnotation new];
self = [super initWithAnnotation:_annotation reuseIdentifier:nil]; self = [super init];
self.canShowCallout = YES;
[self addGestureRecognizer:[
[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(_handleTap:)]];
return self; return self;
} }
...@@ -41,14 +40,24 @@ ...@@ -41,14 +40,24 @@
- (void)setColor:(MAPinAnnotationColor)color { - (void)setColor:(MAPinAnnotationColor)color {
_pinColor = color; _pinColor = color;
_pinView.pinColor = color; ((MAPinAnnotationView *)_annotationView).pinColor = color;
}
- (void)setDraggable:(BOOL)draggable {
_draggable = draggable;
_annotationView.draggable = draggable;
}
- (void)setCenterOffset:(CGPoint)centerOffset {
_centerOffset = centerOffset;
_annotationView.centerOffset = centerOffset;
} }
#pragma clang diagnostic ignored "-Woverriding-method-mismatch" #pragma clang diagnostic ignored "-Woverriding-method-mismatch"
- (void)setImage:(NSString *)name { - (void)setImage:(NSString *)name {
_image = [UIImage imageNamed:name]; _image = [UIImage imageNamed:name];
if (_image != nil) { if (_image != nil) {
_pinView.image = _image; _annotationView.image = _image;
} }
} }
...@@ -62,28 +71,23 @@ ...@@ -62,28 +71,23 @@
- (void)setActive:(BOOL)active { - (void)setActive:(BOOL)active {
_active = active; _active = active;
_pinView.selected = active; if (active) {
self.selected = active; [_mapView selectAnnotation:_annotation animated:YES];
[self updateActive]; } else {
[_mapView deselectAnnotation:_annotation animated:YES];
}
} }
- (void)setClickable:(BOOL)enabled { - (void)setClickable:(BOOL)enabled {
self.enabled = enabled; _annotationView.enabled = enabled;
} }
- (void)updateActive { - (void)setInfoWindowEnabled:(BOOL)enabled {
dispatch_async(dispatch_get_main_queue(), ^{ _annotationView.canShowCallout = enabled;
if (_active) {
[_mapView selectAnnotation:self animated:YES];
} else {
[_mapView deselectAnnotation:self animated:YES];
}
});
} }
- (void)setInfoWindowEnabled:(BOOL)enabled { - (MAPointAnnotation *)annotation {
_pinView.canShowCallout = enabled; return _annotation;
self.canShowCallout = enabled;
} }
- (void)setMapView:(AMapView *)mapView { - (void)setMapView:(AMapView *)mapView {
...@@ -91,55 +95,45 @@ ...@@ -91,55 +95,45 @@
} }
- (void)_handleTap:(UITapGestureRecognizer *)recognizer { - (void)_handleTap:(UITapGestureRecognizer *)recognizer {
_active = YES; [_mapView selectAnnotation:_annotation animated:YES];
[self updateActive];
}
- (BOOL)active {
return _active;
} }
- (MAAnnotationView *)annotationView { - (MAAnnotationView *)annotationView {
if (self.reactSubviews.count == 0) { if (_annotationView == nil) {
if (_pinView == nil) { if (_customView) {
_pinView = [[MAPinAnnotationView alloc] initWithAnnotation:_annotation reuseIdentifier:nil]; _annotationView = [[MAAnnotationView alloc] initWithAnnotation:_annotation reuseIdentifier:nil];
_pinView.canShowCallout = YES; _annotationView.bounds = _customView.bounds;
_pinView.draggable = self.draggable; [_annotationView addSubview:_customView];
_pinView.pinColor = _pinColor; [_annotationView addGestureRecognizer:[
_pinView.customCalloutView = _calloutView; [UITapGestureRecognizer alloc] initWithTarget:self action:@selector(_handleTap:)]];
_pinView.centerOffset = self.centerOffset; } else {
_annotationView = [[MAPinAnnotationView alloc] initWithAnnotation:_annotation reuseIdentifier:nil];
((MAPinAnnotationView *)_annotationView).pinColor = _pinColor;
}
_annotationView.canShowCallout = YES;
_annotationView.draggable = _draggable;
_annotationView.customCalloutView = _calloutView;
_annotationView.centerOffset = _centerOffset;
if (_image != nil) { if (_image != nil) {
_pinView.image = _image; _annotationView.image = _image;
} }
[self setActive:_active];
} }
return _pinView; return _annotationView;
} else {
return self;
}
} }
- (void)insertReactSubview:(id <RCTComponent>)subview atIndex:(NSInteger)atIndex { - (void)didAddSubview:(UIView *)subview {
if ([subview isKindOfClass:[AMapInfoWindow class]]) { if ([subview isKindOfClass:[AMapInfoWindow class]]) {
_callout = (AMapInfoWindow *) subview; _calloutView = [[MACustomCalloutView alloc] initWithCustomView:subview];
_callout.delegate = self; _annotationView.customCalloutView = _calloutView;
UIButton *button = [UIButton new];
[button addSubview:_callout];
_calloutView = [[MACustomCalloutView alloc] initWithCustomView:button];
self.customCalloutView = _calloutView;
} else { } else {
[super insertReactSubview:subview atIndex:atIndex]; _customView = subview;
} }
} }
- (void)didUpdateReactSubviews { - (void)lockToScreen {
[super didUpdateReactSubviews]; _annotation.lockedToScreen = YES;
self.bounds = self.reactSubviews[0].bounds; _annotation.lockedScreenPoint = CGPointMake(100, 100);
}
- (void)updateInfoWindow:(AMapInfoWindow *)overlay {
self.customCalloutView.bounds = overlay.bounds;
} }
@end @end
#import <MAMapKit/MAMapKit.h> #import <MAMapKit/MAMapKit.h>
@class AMapMarker;
@interface AMapView : MAMapView @interface AMapView : MAMapView
@property(nonatomic, copy) RCTBubblingEventBlock onLocation; @property(nonatomic, copy) RCTBubblingEventBlock onLocation;
...@@ -11,4 +13,6 @@ ...@@ -11,4 +13,6 @@
@property(nonatomic) BOOL loaded; @property(nonatomic) BOOL loaded;
@property(nonatomic) MACoordinateRegion initialRegion; @property(nonatomic) MACoordinateRegion initialRegion;
- (AMapMarker *)getMarker:(id <MAAnnotation>)annotation;
@end @end
\ No newline at end of file
...@@ -6,6 +6,13 @@ ...@@ -6,6 +6,13 @@
#pragma ide diagnostic ignored "OCUnusedMethodInspection" #pragma ide diagnostic ignored "OCUnusedMethodInspection"
@implementation AMapView { @implementation AMapView {
NSMutableDictionary *_markers;
}
- (instancetype)init {
_markers = [NSMutableDictionary new];
self = [super init];
return self;
} }
- (void)setShowsTraffic:(BOOL)shows { - (void)setShowsTraffic:(BOOL)shows {
...@@ -43,8 +50,10 @@ ...@@ -43,8 +50,10 @@
- (void)didAddSubview:(UIView *)subview { - (void)didAddSubview:(UIView *)subview {
if ([subview isKindOfClass:[AMapMarker class]]) { if ([subview isKindOfClass:[AMapMarker class]]) {
((AMapMarker *) subview).mapView = self; AMapMarker *marker = (AMapMarker *) subview;
[self addAnnotation:(id <MAAnnotation>) subview]; marker.mapView = self;
_markers[[@(marker.annotation.hash) stringValue]] = marker;
[self addAnnotation:marker.annotation];
} }
if ([subview isKindOfClass:[AMapModel class]]) { if ([subview isKindOfClass:[AMapModel class]]) {
[self addOverlay:(id <MAOverlay>) subview]; [self addOverlay:(id <MAOverlay>) subview];
...@@ -54,11 +63,16 @@ ...@@ -54,11 +63,16 @@
- (void)removeReactSubview:(id <RCTComponent>)subview { - (void)removeReactSubview:(id <RCTComponent>)subview {
[super removeReactSubview:subview]; [super removeReactSubview:subview];
if ([subview isKindOfClass:[AMapMarker class]]) { if ([subview isKindOfClass:[AMapMarker class]]) {
[self removeAnnotation:(id <MAAnnotation>) subview]; AMapMarker *marker = (AMapMarker *) subview;
[self removeAnnotation:marker.annotation];
} }
if ([subview isKindOfClass:[AMapModel class]]) { if ([subview isKindOfClass:[AMapModel class]]) {
[self removeOverlay:(id <MAOverlay>) subview]; [self removeOverlay:(id <MAOverlay>) subview];
} }
} }
- (AMapMarker *)getMarker:(id <MAAnnotation>)annotation {
return _markers[[@(annotation.hash) stringValue]];
}
@end @end
...@@ -9,14 +9,12 @@ ...@@ -9,14 +9,12 @@
@interface AMapViewManager : RCTViewManager <MAMapViewDelegate> @interface AMapViewManager : RCTViewManager <MAMapViewDelegate>
@end @end
@implementation AMapViewManager { @implementation AMapViewManager
}
RCT_EXPORT_MODULE() RCT_EXPORT_MODULE()
- (UIView *)view { - (UIView *)view {
AMapView *mapView = [AMapView new]; AMapView *mapView = [AMapView new];
mapView.runLoopMode = NSDefaultRunLoopMode;
mapView.centerCoordinate = CLLocationCoordinate2DMake(39.9042, 116.4074); mapView.centerCoordinate = CLLocationCoordinate2DMake(39.9042, 116.4074);
mapView.zoomLevel = 10; mapView.zoomLevel = 10;
mapView.delegate = self; mapView.delegate = self;
...@@ -105,10 +103,9 @@ RCT_EXPORT_METHOD(animateTo:(nonnull NSNumber *)reactTag params:(NSDictionary *) ...@@ -105,10 +103,9 @@ RCT_EXPORT_METHOD(animateTo:(nonnull NSNumber *)reactTag params:(NSDictionary *)
} }
} }
- (MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id <MAAnnotation>)annotation { - (MAAnnotationView *)mapView:(AMapView *)mapView viewForAnnotation:(id <MAAnnotation>)annotation {
if ([annotation isKindOfClass:[AMapMarker class]]) { if ([annotation isKindOfClass:[MAPointAnnotation class]]) {
AMapMarker *marker = (AMapMarker *) annotation; AMapMarker *marker = [mapView getMarker:annotation];
[marker updateActive];
return marker.annotationView; return marker.annotationView;
} }
return nil; return nil;
...@@ -116,39 +113,32 @@ RCT_EXPORT_METHOD(animateTo:(nonnull NSNumber *)reactTag params:(NSDictionary *) ...@@ -116,39 +113,32 @@ RCT_EXPORT_METHOD(animateTo:(nonnull NSNumber *)reactTag params:(NSDictionary *)
- (MAOverlayRenderer *)mapView:(MAMapView *)mapView rendererForOverlay:(id <MAOverlay>)overlay { - (MAOverlayRenderer *)mapView:(MAMapView *)mapView rendererForOverlay:(id <MAOverlay>)overlay {
if ([overlay isKindOfClass:[AMapModel class]]) { if ([overlay isKindOfClass:[AMapModel class]]) {
return ((AMapModel *)overlay).renderer; return ((AMapModel *) overlay).renderer;
} }
return nil; return nil;
} }
- (void)mapView:(MAMapView *)mapView didSelectAnnotationView:(MAAnnotationView *)view { - (void)mapView:(AMapView *)mapView didSelectAnnotationView:(MAAnnotationView *)view {
if ([view.annotation isKindOfClass:[AMapMarker class]]) { if ([view.annotation isKindOfClass:[AMapMarker class]]) {
AMapMarker *marker = (AMapMarker *) view.annotation; AMapMarker *marker = [mapView getMarker:view.annotation];
if (marker.onPress) { if (marker.onPress) {
marker.onPress(nil); marker.onPress(nil);
} }
} }
} }
- (void)mapView:(MAMapView *)mapView didDeselectAnnotationView:(MAAnnotationView *)view { - (void)mapView:(AMapView *)mapView didAnnotationViewCalloutTapped:(MAAnnotationView *)view {
if ([view.annotation isKindOfClass:[AMapMarker class]]) {
AMapMarker *marker = (AMapMarker *) view.annotation;
marker.active = NO;
}
}
- (void)mapView:(MAMapView *)mapView didAnnotationViewCalloutTapped:(MAAnnotationView *)view {
if ([view.annotation isKindOfClass:[AMapMarker class]]) { if ([view.annotation isKindOfClass:[AMapMarker class]]) {
AMapMarker *marker = (AMapMarker *) view.annotation; AMapMarker *marker = [mapView getMarker:view.annotation];
if (marker.onInfoWindowPress) { if (marker.onInfoWindowPress) {
marker.onInfoWindowPress(nil); marker.onInfoWindowPress(nil);
} }
} }
} }
- (void)mapView:(MAMapView *)mapView annotationView:(MAAnnotationView *)view didChangeDragState:(MAAnnotationViewDragState)newState - (void)mapView:(AMapView *)mapView annotationView:(MAAnnotationView *)view didChangeDragState:(MAAnnotationViewDragState)newState
fromOldState:(MAAnnotationViewDragState)oldState { fromOldState:(MAAnnotationViewDragState)oldState {
AMapMarker *marker = (AMapMarker *) view.annotation; AMapMarker *marker = [mapView getMarker:view.annotation];
if (newState == MAAnnotationViewDragStateStarting && marker.onDragStart) { if (newState == MAAnnotationViewDragStateStarting && marker.onDragStart) {
marker.onDragStart(nil); marker.onDragStart(nil);
} }
...@@ -159,8 +149,8 @@ RCT_EXPORT_METHOD(animateTo:(nonnull NSNumber *)reactTag params:(NSDictionary *) ...@@ -159,8 +149,8 @@ RCT_EXPORT_METHOD(animateTo:(nonnull NSNumber *)reactTag params:(NSDictionary *)
} }
if (newState == MAAnnotationViewDragStateEnding && marker.onDragEnd) { if (newState == MAAnnotationViewDragStateEnding && marker.onDragEnd) {
marker.onDragEnd(@{ marker.onDragEnd(@{
@"latitude": @(marker.coordinate.latitude), @"latitude": @(marker.annotation.coordinate.latitude),
@"longitude": @(marker.coordinate.longitude), @"longitude": @(marker.annotation.coordinate.longitude),
}); });
} }
} }
......
{ {
"name": "react-native-amap3d", "name": "react-native-amap3d",
"version": "0.8.0", "version": "0.8.1",
"description": "react-native 高德地图组件", "description": "react-native 高德地图组件",
"author": "Qiu Xiang <i@7c00.cc>", "author": "Qiu Xiang <i@7c00.cc>",
"license": "MIT", "license": "MIT",
......
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