Commit b18d4565 authored by Qiu Xiang's avatar Qiu Xiang

实现 iOS polygon 接口

parent 8608af77
...@@ -161,8 +161,8 @@ import MapView from 'react-native-amap3d' ...@@ -161,8 +161,8 @@ import MapView from 'react-native-amap3d'
- [x] 基本属性及事件 - [x] 基本属性及事件
- [x] 自定义信息窗体 - [x] 自定义信息窗体
- [x] 自定义图标 - [x] 自定义图标
- [ ] 折线绘制(Polyline)🚀 - [x] 折线绘制(Polyline)
- [ ] 多边形绘制(Polygon) - [x] 多边形绘制(Polygon)
- [ ] 圆形绘制(Circle) - [ ] 圆形绘制(Circle)🚀
- [ ] POI 检索 - [ ] POI 检索
- [ ] 地理编码转换 - [ ] 地理编码转换
import React, {PropTypes, Component} from 'react' import React, {PropTypes, Component} from 'react'
import {requireNativeComponent, View, PixelRatio} from 'react-native' import {requireNativeComponent, View, PixelRatio, Platform} from 'react-native'
import {LatLng} from './PropTypes' import {LatLng} from './PropTypes'
class Polygon extends Component { class Polygon extends Component {
...@@ -16,7 +16,11 @@ class Polygon extends Component { ...@@ -16,7 +16,11 @@ class Polygon extends Component {
render() { render() {
const props = { const props = {
...this.props, ...this.props,
strokeWidth: PixelRatio.getPixelSizeForLayoutSize(this.props.strokeWidth), ...Platform.select({
android: {
strokeWidth: PixelRatio.getPixelSizeForLayoutSize(this.props.strokeWidth),
},
}),
} }
return <AMapPolygon {...props}/> return <AMapPolygon {...props}/>
} }
......
#import <Foundation/Foundation.h>
#import <MAMapKit/MAMapKit.h>
#pragma ide diagnostic ignored "OCUnusedPropertyInspection"
@interface AMapPolygon : UIView <MAOverlay>
@property(nonatomic, readonly) CLLocationCoordinate2D coordinate;
@property(nonatomic, readonly) MAMapRect boundingMapRect;
- (MAOverlayRenderer *)renderer;
@end
#import "AMapPolygon.h"
#import "Coordinate.h"
#pragma ide diagnostic ignored "OCUnusedMethodInspection"
@implementation AMapPolygon {
MAPolygon *_polygon;
MAPolygonRenderer *_renderer;
CGFloat _strokeWidth;
UIColor *_strokeColor;
UIColor *_fillColor;
}
- (void)setCoordinates:(NSArray<Coordinate *> *)coordinates {
CLLocationCoordinate2D coords[coordinates.count];
for (NSUInteger i = 0; i < coordinates.count; i++) {
coords[i] = coordinates[i].coordinate;
}
if (_strokeColor == nil) {
_strokeColor = UIColor.blackColor;
}
_polygon = [MAPolygon polygonWithCoordinates:coords count:coordinates.count];
_renderer = [[MAPolygonRenderer alloc] initWithPolygon:_polygon];
_renderer.lineWidth = _strokeWidth;
_renderer.strokeColor = _strokeColor;
_renderer.fillColor = _fillColor;
}
- (void)setStrokeWidth:(CGFloat)strokeWidth {
_strokeWidth = strokeWidth;
_renderer.lineWidth = strokeWidth;
}
- (void)setStrokeColor:(UIColor *)strokeColor {
_strokeColor = strokeColor;
_renderer.strokeColor = strokeColor;
}
- (void)setFillColor:(UIColor *)fillColor {
_fillColor = fillColor;
_renderer.fillColor = fillColor;
}
- (CLLocationCoordinate2D)coordinate {
return _polygon.coordinate;
}
- (MAMapRect)boundingMapRect {
return _polygon.boundingMapRect;
}
- (MAOverlayRenderer *)renderer {
return _renderer;
}
@end
#import <MAMapKit/MAMapView.h>
#import <React/RCTViewManager.h>
#import "AMapPolygon.h"
#pragma ide diagnostic ignored "OCUnusedClassInspection"
@interface AMapPolygonManager : RCTViewManager <MAMapViewDelegate>
@end
@implementation AMapPolygonManager {
}
RCT_EXPORT_MODULE()
- (UIView *)view {
return [AMapPolygon new];
}
RCT_EXPORT_VIEW_PROPERTY(coordinates, CoordinateArray)
RCT_EXPORT_VIEW_PROPERTY(strokeWidth, CGFloat)
RCT_EXPORT_VIEW_PROPERTY(strokeColor, UIColor)
RCT_EXPORT_VIEW_PROPERTY(fillColor, UIColor)
@end
#import <Foundation/Foundation.h> #import <Foundation/Foundation.h>
#import <MAMapKit/MAMapKit.h> #import <MAMapKit/MAMapKit.h>
#pragma ide diagnostic ignored "OCUnusedPropertyInspection"
@interface AMapPolyline : UIView <MAOverlay> @interface AMapPolyline : UIView <MAOverlay>
@property(nonatomic, readonly) CLLocationCoordinate2D coordinate; @property(nonatomic, readonly) CLLocationCoordinate2D coordinate;
...@@ -8,4 +10,4 @@ ...@@ -8,4 +10,4 @@
- (MAOverlayRenderer *)renderer; - (MAOverlayRenderer *)renderer;
@end @end
\ No newline at end of file
...@@ -16,10 +16,10 @@ ...@@ -16,10 +16,10 @@
for (NSUInteger i = 0; i < coordinates.count; i++) { for (NSUInteger i = 0; i < coordinates.count; i++) {
coords[i] = coordinates[i].coordinate; coords[i] = coordinates[i].coordinate;
} }
_polyline = [MAPolyline polylineWithCoordinates:coords count:coordinates.count];
if (_color == nil) { if (_color == nil) {
_color = UIColor.blackColor; _color = UIColor.blackColor;
} }
_polyline = [MAPolyline polylineWithCoordinates:coords count:coordinates.count];
_renderer = [[MAPolylineRenderer alloc] initWithPolyline:_polyline]; _renderer = [[MAPolylineRenderer alloc] initWithPolyline:_polyline];
_renderer.lineWidth = _width; _renderer.lineWidth = _width;
_renderer.strokeColor = _color; _renderer.strokeColor = _color;
......
#import "AMapView.h" #import "AMapView.h"
#import "AMapMarker.h" #import "AMapMarker.h"
#import "AMapPolyline.h" #import "AMapPolyline.h"
#import "AMapPolygon.h"
#pragma ide diagnostic ignored "OCUnusedMethodInspection" #pragma ide diagnostic ignored "OCUnusedMethodInspection"
...@@ -32,7 +33,7 @@ ...@@ -32,7 +33,7 @@
((AMapMarker *) subview).mapView = self; ((AMapMarker *) subview).mapView = self;
[self addAnnotation:(id <MAAnnotation>) subview]; [self addAnnotation:(id <MAAnnotation>) subview];
} }
if ([subview isKindOfClass:[AMapPolyline class]]) {; if ([subview isKindOfClass:[AMapPolyline class]] || [subview isKindOfClass:[AMapPolygon class]]) {;
[self addOverlay:(id <MAOverlay>) subview]; [self addOverlay:(id <MAOverlay>) subview];
} }
} }
...@@ -41,6 +42,9 @@ ...@@ -41,6 +42,9 @@
if ([subview isKindOfClass:[AMapMarker class]]) { if ([subview isKindOfClass:[AMapMarker class]]) {
[self removeAnnotation:(id <MAAnnotation>) subview]; [self removeAnnotation:(id <MAAnnotation>) subview];
} }
if ([subview isKindOfClass:[AMapPolyline class]] || [subview isKindOfClass:[AMapPolygon class]]) {
[self removeOverlay:(id <MAOverlay>) subview];
}
} }
@end @end
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
#import "AMapView.h" #import "AMapView.h"
#import "AMapMarker.h" #import "AMapMarker.h"
#import "AMapPolyline.h" #import "AMapPolyline.h"
#import "AMapPolygon.h"
#pragma ide diagnostic ignored "OCUnusedClassInspection" #pragma ide diagnostic ignored "OCUnusedClassInspection"
#pragma ide diagnostic ignored "-Woverriding-method-mismatch" #pragma ide diagnostic ignored "-Woverriding-method-mismatch"
...@@ -88,6 +89,9 @@ RCT_EXPORT_VIEW_PROPERTY(onLocation, RCTBubblingEventBlock) ...@@ -88,6 +89,9 @@ RCT_EXPORT_VIEW_PROPERTY(onLocation, RCTBubblingEventBlock)
if ([overlay isKindOfClass:[AMapPolyline class]]) { if ([overlay isKindOfClass:[AMapPolyline class]]) {
return ((AMapPolyline *)overlay).renderer; return ((AMapPolyline *)overlay).renderer;
} }
if ([overlay isKindOfClass:[AMapPolygon class]]) {
return ((AMapPolygon *)overlay).renderer;
}
return nil; return nil;
} }
......
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