Skip to content

Commit

Permalink
Fix #116, close #125
Browse files Browse the repository at this point in the history
  • Loading branch information
qiuxiang committed Dec 22, 2017
1 parent 9e44e84 commit 7fb5e77
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 110 deletions.
8 changes: 0 additions & 8 deletions ios/maps/AMapInfoWindow.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,4 @@
#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
10 changes: 1 addition & 9 deletions ios/maps/AMapInfoWindow.m
Original file line number Diff line number Diff line change
@@ -1,12 +1,4 @@
#import <React/UIView+React.h>
#import "AMapInfoWindow.h"

@implementation AMapInfoWindow {
}

- (void)didUpdateReactSubviews {
[super didUpdateReactSubviews];
[self.delegate updateInfoWindow:self];
}

@implementation AMapInfoWindow
@end
7 changes: 3 additions & 4 deletions ios/maps/AMapMarker.h
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
#import "AMapView.h"
#import "AMapInfoWindow.h"

@interface AMapMarker : MAAnnotationView <MAAnnotation, AMapInfoWindowDelegate>
@interface AMapMarker : UIView

@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;

- (BOOL)active;
- (void)setActive:(BOOL)active;
- (MAAnnotationView *)annotationView;
- (MAPointAnnotation *)annotation;
- (void)setMapView:(AMapView *)mapView;
- (void)updateActive;
- (void)lockToScreen;

@end
118 changes: 56 additions & 62 deletions ios/maps/AMapMarker.m
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -41,14 +40,24 @@ - (void)setTitle:(NSString *)title {

- (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;
}
}

Expand All @@ -62,84 +71,69 @@ - (void)setCoordinate:(CLLocationCoordinate2D)coordinate {

- (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 {
_mapView = mapView;
}

- (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
4 changes: 4 additions & 0 deletions ios/maps/AMapView.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#import <MAMapKit/MAMapKit.h>

@class AMapMarker;

@interface AMapView : MAMapView

@property(nonatomic, copy) RCTBubblingEventBlock onLocation;
Expand All @@ -11,4 +13,6 @@
@property(nonatomic) BOOL loaded;
@property(nonatomic) MACoordinateRegion initialRegion;

- (AMapMarker *)getMarker:(id <MAAnnotation>)annotation;

@end
20 changes: 17 additions & 3 deletions ios/maps/AMapView.m
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -43,8 +50,10 @@ - (void)setRegion:(MACoordinateRegion)region {

- (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];
Expand All @@ -54,11 +63,16 @@ - (void)didAddSubview:(UIView *)subview {
- (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
36 changes: 13 additions & 23 deletions ios/maps/AMapViewManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -105,50 +103,42 @@ - (void)mapView:(AMapView *)mapView didUpdateUserLocation:(MAUserLocation *)user
}
}

- (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;
}

- (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);
}
Expand All @@ -159,8 +149,8 @@ - (void)mapView:(MAMapView *)mapView annotationView:(MAAnnotationView *)view did
}
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),
});
}
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-native-amap3d",
"version": "0.8.0",
"version": "0.8.1",
"description": "react-native 高德地图组件",
"author": "Qiu Xiang <[email protected]>",
"license": "MIT",
Expand Down

0 comments on commit 7fb5e77

Please sign in to comment.