Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for polygon holes for Apple Maps and Google Maps on iOS #801

Merged
merged 4 commits into from
Nov 30, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions components/MapPolygon.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,17 @@ const propTypes = {
longitude: PropTypes.number.isRequired,
})),

/**
* An array of array of coordinates to describe the polygon holes
*/
holes: PropTypes.arrayOf(PropTypes.arrayOf(PropTypes.shape({
/**
* Latitude/Longitude coordinates
*/
latitude: PropTypes.number.isRequired,
longitude: PropTypes.number.isRequired,
}))),

/**
* Callback that is called when the user presses on the polygon
*/
Expand Down
62 changes: 60 additions & 2 deletions example/examples/PolygonCreator.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class PolygonCreator extends React.Component {
},
polygons: [],
editing: null,
creatingHole: false,
};
}

Expand All @@ -39,19 +40,49 @@ class PolygonCreator extends React.Component {
this.setState({
polygons: [...polygons, editing],
editing: null,
creatingHole: false,
});
}

createHole() {
const { editing, creatingHole } = this.state;
if (!creatingHole) {
this.setState({
creatingHole: true,
editing: {
...editing,
holes: [
...editing.holes,
[],
],
},
});
} else {
const holes = [...editing.holes];
if (holes[holes.length - 1].length === 0) {
holes.pop();
this.setState({
editing: {
...editing,
holes,
},
});
}
this.setState({ creatingHole: false });
}
}

onPress(e) {
const { editing } = this.state;
const { editing, creatingHole } = this.state;
if (!editing) {
this.setState({
editing: {
id: id++,
coordinates: [e.nativeEvent.coordinate],
holes: [],
},
});
} else {
} else if (!creatingHole) {
this.setState({
editing: {
...editing,
Expand All @@ -61,6 +92,22 @@ class PolygonCreator extends React.Component {
],
},
});
} else {
const holes = [...editing.holes];
holes[holes.length - 1] = [
...holes[holes.length - 1],
e.nativeEvent.coordinate,
];
this.setState({
editing: {
...editing,
id: id++, // keep incrementing id to trigger display refresh
coordinates: [
...editing.coordinates,
],
holes,
},
});
}
}

Expand Down Expand Up @@ -88,21 +135,32 @@ class PolygonCreator extends React.Component {
<MapView.Polygon
key={polygon.id}
coordinates={polygon.coordinates}
holes={polygon.holes}
strokeColor="#F00"
fillColor="rgba(255,0,0,0.5)"
strokeWidth={1}
/>
))}
{this.state.editing && (
<MapView.Polygon
key={this.state.editing.id}
coordinates={this.state.editing.coordinates}
holes={this.state.editing.holes}
strokeColor="#000"
fillColor="rgba(255,0,0,0.5)"
strokeWidth={1}
/>
)}
</MapView>
<View style={styles.buttonContainer}>
{this.state.editing && (
<TouchableOpacity
onPress={() => this.createHole()}
style={[styles.bubble, styles.button]}
>
<Text>{this.state.creatingHole ? 'Finish Hole' : 'Create Hole'}</Text>
</TouchableOpacity>
)}
{this.state.editing && (
<TouchableOpacity
onPress={() => this.finish()}
Expand Down
1 change: 1 addition & 0 deletions ios/AirGoogleMaps/AIRGoogleMapPolygon.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

@property (nonatomic, strong) GMSPolygon *polygon;
@property (nonatomic, strong) NSArray<AIRMapCoordinate *> *coordinates;
@property (nonatomic, strong) NSArray<NSArray<AIRMapCoordinate *> *> *holes;

@property (nonatomic, assign) UIColor *fillColor;
@property (nonatomic, assign) double strokeWidth;
Expand Down
21 changes: 21 additions & 0 deletions ios/AirGoogleMaps/AIRGoogleMapPolygon.m
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,27 @@ - (void)setCoordinates:(NSArray<AIRMapCoordinate *> *)coordinates
_polygon.path = path;
}

- (void)setHoles:(NSArray<NSArray<AIRMapCoordinate *> *> *)holes
{
_holes = holes;

if (holes.count)
{
NSMutableArray<GMSMutablePath *> *interiorPolygons = [NSMutableArray array];
for(int h = 0; h < holes.count; h++)
{
GMSMutablePath *path = [GMSMutablePath path];
for(int i = 0; i < holes[h].count; i++)
{
[path addCoordinate:holes[h][i].coordinate];
}
[interiorPolygons addObject:path];
}

_polygon.holes = interiorPolygons;
}
}

-(void)setFillColor:(UIColor *)fillColor
{
_fillColor = fillColor;
Expand Down
1 change: 1 addition & 0 deletions ios/AirGoogleMaps/AIRGoogleMapPolygonManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ - (UIView *)view
}

RCT_EXPORT_VIEW_PROPERTY(coordinates, AIRMapCoordinateArray)
RCT_EXPORT_VIEW_PROPERTY(holes, AIRMapCoordinateArrayArray)
RCT_EXPORT_VIEW_PROPERTY(fillColor, UIColor)
RCT_EXPORT_VIEW_PROPERTY(strokeWidth, double)
RCT_EXPORT_VIEW_PROPERTY(strokeColor, UIColor)
Expand Down
4 changes: 3 additions & 1 deletion ios/AirMaps/AIRMapPolygon.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@

@property (nonatomic, strong) MKPolygon *polygon;
@property (nonatomic, strong) MKPolygonRenderer *renderer;
@property (nonatomic, strong) NSArray<MKPolygon *> *interiorPolygons;

@property (nonatomic, strong) NSArray<AIRMapCoordinate *> *coordinates;
@property (nonatomic, strong) NSArray<NSArray<AIRMapCoordinate *> *> *holes;
@property (nonatomic, strong) UIColor *fillColor;
@property (nonatomic, strong) UIColor *strokeColor;
@property (nonatomic, assign) CGFloat strokeWidth;
Expand All @@ -41,4 +43,4 @@
- (BOOL)intersectsMapRect:(MKMapRect)mapRect;
- (BOOL)canReplaceMapContent;

@end
@end
22 changes: 20 additions & 2 deletions ios/AirMaps/AIRMapPolygon.m
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,31 @@ - (void)setCoordinates:(NSArray<AIRMapCoordinate *> *)coordinates {
{
coords[i] = coordinates[i].coordinate;
}
self.polygon = [MKPolygon polygonWithCoordinates:coords count:coordinates.count];
self.polygon = [MKPolygon polygonWithCoordinates:coords count:coordinates.count interiorPolygons:_interiorPolygons];
// TODO: we could lazy-initialize the polygon, since we don't need it until the
// polygon is in view.
self.renderer = [[MKPolygonRenderer alloc] initWithPolygon:self.polygon];
[self update];
}

- (void)setHoles:(NSArray<NSArray<AIRMapCoordinate *> *> *)holes {
_holes = holes;
if (holes.count)
{
NSMutableArray<MKPolygon *> *polygons = [NSMutableArray array];
for(int h = 0; h < holes.count; h++)
{
CLLocationCoordinate2D coords[holes[h].count];
for(int i = 0; i < holes[h].count; i++)
{
coords[i] = holes[h][i].coordinate;
}
[polygons addObject:[MKPolygon polygonWithCoordinates:coords count:holes[h].count]];
}
_interiorPolygons = polygons;
}
}

- (void) update
{
if (!_renderer) return;
Expand Down Expand Up @@ -157,4 +175,4 @@ - (BOOL)canReplaceMapContent



@end
@end
1 change: 1 addition & 0 deletions ios/AirMaps/AIRMapPolygonManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ - (UIView *)view
}

RCT_EXPORT_VIEW_PROPERTY(coordinates, AIRMapCoordinateArray)
RCT_EXPORT_VIEW_PROPERTY(holes, AIRMapCoordinateArrayArray)
RCT_EXPORT_VIEW_PROPERTY(fillColor, UIColor)
RCT_EXPORT_VIEW_PROPERTY(strokeColor, UIColor)
RCT_EXPORT_VIEW_PROPERTY(strokeWidth, CGFloat)
Expand Down
7 changes: 6 additions & 1 deletion ios/AirMaps/RCTConvert+MoreMapKit.m
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,9 @@ + (AIRMapCoordinate *)AIRMapCoordinate:(id)json

RCT_ARRAY_CONVERTER(AIRMapCoordinate)

@end
+ (NSArray<NSArray<AIRMapCoordinate *> *> *)AIRMapCoordinateArrayArray:(id)json
{
return RCTConvertArrayValue(@selector(AIRMapCoordinateArray:), json);
}

@end