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

Fix #116, close #125

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