Commit 0ba4cf0c authored by Qiu Xiang's avatar Qiu Xiang

优化 iOS marker 的实现

parent faa051ac
......@@ -27,8 +27,9 @@ export default class MarkerExample extends Component {
render() {
return <MapView style={StyleSheet.absoluteFill}>
<Marker
active
draggable
title='一个可拖拽的 Marker'
title={'一个可拖拽的 Marker ' + this.state.time.toLocaleTimeString()}
onDragEnd={({nativeEvent}) =>
Alert.alert(`新坐标:${nativeEvent.latitude}, ${nativeEvent.longitude}`)}
onInfoWindowPress={() => Alert.alert('信息窗口点击事件')}
......@@ -38,8 +39,7 @@ export default class MarkerExample extends Component {
}}
/>
<Marker
active
icon='red'
icon='green'
infoWindowWidth={100}
coordinate={{
latitude: 39.806901,
......@@ -84,15 +84,19 @@ const styles = StyleSheet.create({
height: 40,
},
customInfoWindow: {
backgroundColor: '#fff',
backgroundColor: '#8bc34a',
position: 'absolute',
padding: 10,
borderRadius: 10,
elevation: 4,
borderWidth: 2,
borderColor: '#689F38',
},
customMarker: {
position: 'absolute',
backgroundColor: '#009688',
alignItems: 'center',
borderRadius: 5,
padding: 5,
},
markerText: {
......
......@@ -3,20 +3,10 @@
#import <React/RCTComponent.h>
#import "AMapView.h"
#import "AMapOverlay.h"
#import "AMapInfoWindow.h"
#pragma ide diagnostic ignored "OCUnusedPropertyInspection"
#pragma ide diagnostic ignored "OCUnusedMethodInspection"
@class AMapView;
@class AMapOverlay;
@class AMapInfoWindow;
@interface AMapMarker : MAPinAnnotationView <MAAnnotation, AMapOverlayDelegate>
@property(nonatomic, assign) CLLocationCoordinate2D coordinate;
@property(nonatomic, copy) NSString *title;
@property(nonatomic, copy) NSString *subtitle;
@property(nonatomic, strong) AMapView *mapView;
@interface AMapMarker : UIView <MAAnnotation, AMapOverlayDelegate>
@property (nonatomic, copy) RCTBubblingEventBlock onPress;
@property (nonatomic, copy) RCTBubblingEventBlock onInfoWindowPress;
......@@ -24,7 +14,14 @@
@property (nonatomic, copy) RCTBubblingEventBlock onDrag;
@property (nonatomic, copy) RCTBubblingEventBlock onDragEnd;
- (MAAnnotationView *)getAnnotationView;
- (BOOL)active;
- (CLLocationCoordinate2D)coordinate;
- (NSString *)title;
- (NSString *)subtitle;
- (MAAnnotationView *)annotationView;
- (void)setMapView:(AMapView *)mapView;
- (void)setCoordinate:(CLLocationCoordinate2D)coordinate;
@end
......@@ -3,67 +3,104 @@
#pragma ide diagnostic ignored "OCUnusedMethodInspection"
@implementation AMapMarker {
MAPinAnnotationView *_pinView;
AMapOverlay *_iconView;
MAPinAnnotationView *_annotationView;
MAPointAnnotation *_annotation;
AMapOverlay *_overlay;
AMapView *_mapView;
BOOL _active;
}
- (instancetype)init {
_annotation = [MAPointAnnotation new];
_annotationView = [[MAPinAnnotationView alloc] initWithAnnotation:_annotation reuseIdentifier:nil];
_annotationView.canShowCallout = YES;
self = [super init];
return self;
}
- (MAAnnotationView *)annotationView {
return _annotationView;
}
- (NSString *)title {
return _annotation.title;
}
- (NSString *)subtitle {
return _annotation.subtitle;
}
- (CLLocationCoordinate2D)coordinate {
return _annotation.coordinate;
}
- (void)setCoordinate:(CLLocationCoordinate2D)coordinate {
_annotation.coordinate = coordinate;
}
- (void)setTitle:(NSString *)title {
_annotation.title = title;
}
- (void)setSubtitle:(NSString *)subtitle {
_annotation.subtitle = subtitle;
}
- (void)setActive:(BOOL)active {
_active = active;
if (active) {
[self.mapView selectAnnotation:self animated:YES];
[_mapView selectAnnotation:self animated:YES];
} else {
[_mapView deselectAnnotation:self animated:YES];
}
}
- (void)setIcon:(MAPinAnnotationColor)color {
self.pinColor = color;
_annotationView.pinColor = color;
}
- (void)setDescription:(NSString *)description {
self.subtitle = description;
_annotationView.annotation.subtitle = description;
}
- (void)setDraggable:(BOOL)draggable {
_annotationView.draggable = draggable;
}
- (void)setInfoWindowEnabled:(BOOL)enabled {
self.canShowCallout = enabled;
}
- (MAAnnotationView *)getAnnotationView {
if (_pinView == nil) {
_pinView = [[MAPinAnnotationView alloc] initWithAnnotation:self reuseIdentifier: nil];
_pinView.annotation = self;
_pinView.zIndex = self.zIndex;
_pinView.pinColor = self.pinColor;
_pinView.draggable = self.draggable;
_pinView.canShowCallout = self.canShowCallout;
_pinView.customCalloutView = self.customCalloutView;
}
if (_iconView != nil) {
_pinView.image = nil;
}
return _pinView;
_annotationView.canShowCallout = enabled;
}
- (void)setZIndex:(NSInteger)zIndex {
_annotationView.zIndex = zIndex;
}
- (void)setMapView:(AMapView *)mapView {
_mapView = mapView;
}
- (BOOL)active {
return _active;
}
- (void)insertReactSubview:(id<RCTComponent>)subview atIndex:(NSInteger)atIndex {
- (void)insertReactSubview:(id <RCTComponent>)subview atIndex:(NSInteger)atIndex {
if ([subview isKindOfClass:[AMapOverlay class]]) {
_iconView = (AMapOverlay *)subview;
_iconView.delegate = self;
}
if ([subview isKindOfClass:[AMapInfoWindow class]]) {
self.customCalloutView = [[MACustomCalloutView alloc] initWithCustomView:(id) subview];
_overlay = (AMapOverlay *) subview;
_overlay.delegate = self;
_annotationView.image = nil;
} else {
_annotationView.customCalloutView = [[MACustomCalloutView alloc] initWithCustomView:(id) subview];
}
}
#pragma mark AMapOverlayDelegate
- (void)update {
UIGraphicsBeginImageContextWithOptions([_iconView bounds].size, NO, 0.0f);
[_iconView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIGraphicsBeginImageContextWithOptions([_overlay bounds].size, NO, 0.0f);
[_overlay.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
_pinView.image = image;
_annotationView.image = image;
}
@end
......@@ -13,9 +13,7 @@
RCT_EXPORT_MODULE()
- (UIView *)view {
AMapMarker *marker = [AMapMarker new];
marker.canShowCallout = YES;
return marker;
return [AMapMarker new];
}
RCT_EXPORT_VIEW_PROPERTY(coordinate, CLLocationCoordinate2D)
......
......@@ -7,6 +7,6 @@
@end
@interface AMapOverlay : RCTView
@property(nonatomic, strong) id delegate;
@property(nonatomic, strong) id<AMapOverlayDelegate> delegate;
- (void)update;
@end
\ No newline at end of file
......@@ -85,7 +85,7 @@ RCT_EXPORT_VIEW_PROPERTY(onLocation, RCTBubblingEventBlock)
if (marker.active) {
[mapView selectAnnotation:marker animated:YES];
}
return [marker getAnnotationView];
return marker.annotationView;
}
- (void)mapView:(MAMapView *)mapView didSelectAnnotationView:(MAAnnotationView *)view {
......
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