mirror of
https://github.com/zhigang1992/react-native.git
synced 2026-04-29 04:35:36 +08:00
Add Polyline support to MapView
Summary: Per issue #1925, add support for Polyline to MapView. Briefly, if you have a MapView declared as: <MapView annotations={this.state.annotations} overlays={this.state.overlays} style={styles.map} region={this.state.region} ref="mapView" /> then setting this.state.overlays = [{ coordinates: [ { latitude: 35.5, longitude: -5.5 }, { latitude: 35.6, longitude: -5.6 }, ... ], strokeColor: 'rgba(255, 0, 0, 0.5)', lineWidth: 3, }]; will draw a red line between the points in locations with a width of 3 and equally blended with the background. Closes https://github.com/facebook/react-native/pull/4153 Reviewed By: svcscm Differential Revision: D2697347 Pulled By: nicklockwood fb-gh-sync-id: a436e4ed8d4e43f2872b39b4694fad7c02de8fe5
This commit is contained in:
committed by
facebook-github-bot-3
parent
a636ddd9f0
commit
8911b72d9e
@@ -9,21 +9,24 @@
|
||||
|
||||
#import <MapKit/MapKit.h>
|
||||
|
||||
#import "RCTPointAnnotation.h"
|
||||
#import "RCTConvert.h"
|
||||
|
||||
@class RCTMapAnnotation;
|
||||
@class RCTMapOverlay;
|
||||
|
||||
@interface RCTConvert (MapKit)
|
||||
|
||||
+ (MKCoordinateSpan)MKCoordinateSpan:(id)json;
|
||||
+ (MKCoordinateRegion)MKCoordinateRegion:(id)json;
|
||||
+ (MKShape *)MKShape:(id)json;
|
||||
+ (MKMapType)MKMapType:(id)json;
|
||||
+ (RCTPointAnnotation *)RCTPointAnnotation:(id)json;
|
||||
|
||||
typedef NSArray MKShapeArray;
|
||||
+ (MKShapeArray *)MKShapeArray:(id)json;
|
||||
+ (RCTMapAnnotation *)RCTMapAnnotation:(id)json;
|
||||
+ (RCTMapOverlay *)RCTMapOverlay:(id)json;
|
||||
|
||||
typedef NSArray RCTPointAnnotationArray;
|
||||
+ (RCTPointAnnotationArray *)RCTPointAnnotationArray:(id)json;
|
||||
typedef NSArray RCTMapAnnotationArray;
|
||||
+ (NSArray<RCTMapAnnotation *> *)RCTMapAnnotationArray:(id)json;
|
||||
|
||||
typedef NSArray RCTMapOverlayArray;
|
||||
+ (NSArray<RCTMapOverlay *> *)RCTMapOverlayArray:(id)json;
|
||||
|
||||
@end
|
||||
|
||||
@@ -9,7 +9,8 @@
|
||||
|
||||
#import "RCTConvert+MapKit.h"
|
||||
#import "RCTConvert+CoreLocation.h"
|
||||
#import "RCTPointAnnotation.h"
|
||||
#import "RCTMapAnnotation.h"
|
||||
#import "RCTMapOverlay.h"
|
||||
|
||||
@implementation RCTConvert(MapKit)
|
||||
|
||||
@@ -30,45 +31,52 @@
|
||||
};
|
||||
}
|
||||
|
||||
+ (MKShape *)MKShape:(id)json
|
||||
{
|
||||
json = [self NSDictionary:json];
|
||||
|
||||
// TODO: more shape types
|
||||
MKShape *shape = [MKPointAnnotation new];
|
||||
shape.coordinate = [self CLLocationCoordinate2D:json];
|
||||
shape.title = [RCTConvert NSString:json[@"title"]];
|
||||
shape.subtitle = [RCTConvert NSString:json[@"subtitle"]];
|
||||
return shape;
|
||||
}
|
||||
|
||||
RCT_ARRAY_CONVERTER(MKShape)
|
||||
|
||||
RCT_ENUM_CONVERTER(MKMapType, (@{
|
||||
@"standard": @(MKMapTypeStandard),
|
||||
@"satellite": @(MKMapTypeSatellite),
|
||||
@"hybrid": @(MKMapTypeHybrid),
|
||||
}), MKMapTypeStandard, integerValue)
|
||||
|
||||
+ (RCTPointAnnotation *)RCTPointAnnotation:(id)json
|
||||
+ (RCTMapAnnotation *)RCTMapAnnotation:(id)json
|
||||
{
|
||||
json = [self NSDictionary:json];
|
||||
RCTPointAnnotation *shape = [RCTPointAnnotation new];
|
||||
shape.coordinate = [self CLLocationCoordinate2D:json];
|
||||
shape.title = [RCTConvert NSString:json[@"title"]];
|
||||
shape.subtitle = [RCTConvert NSString:json[@"subtitle"]];
|
||||
shape.identifier = [RCTConvert NSString:json[@"id"]];
|
||||
shape.hasLeftCallout = [RCTConvert BOOL:json[@"hasLeftCallout"]];
|
||||
shape.hasRightCallout = [RCTConvert BOOL:json[@"hasRightCallout"]];
|
||||
shape.animateDrop = [RCTConvert BOOL:json[@"animateDrop"]];
|
||||
shape.tintColor = [RCTConvert UIColor:json[@"tintColor"]];
|
||||
shape.image = [RCTConvert UIImage:json[@"image"]];
|
||||
if (shape.tintColor && shape.image) {
|
||||
shape.image = [shape.image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
|
||||
RCTMapAnnotation *annotation = [RCTMapAnnotation new];
|
||||
annotation.coordinate = [self CLLocationCoordinate2D:json];
|
||||
annotation.title = [RCTConvert NSString:json[@"title"]];
|
||||
annotation.subtitle = [RCTConvert NSString:json[@"subtitle"]];
|
||||
annotation.identifier = [RCTConvert NSString:json[@"id"]];
|
||||
annotation.hasLeftCallout = [RCTConvert BOOL:json[@"hasLeftCallout"]];
|
||||
annotation.hasRightCallout = [RCTConvert BOOL:json[@"hasRightCallout"]];
|
||||
annotation.animateDrop = [RCTConvert BOOL:json[@"animateDrop"]];
|
||||
annotation.tintColor = [RCTConvert UIColor:json[@"tintColor"]];
|
||||
annotation.image = [RCTConvert UIImage:json[@"image"]];
|
||||
if (annotation.tintColor && annotation.image) {
|
||||
annotation.image = [annotation.image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
|
||||
}
|
||||
return shape;
|
||||
return annotation;
|
||||
}
|
||||
|
||||
RCT_ARRAY_CONVERTER(RCTPointAnnotation)
|
||||
RCT_ARRAY_CONVERTER(RCTMapAnnotation)
|
||||
|
||||
+ (RCTMapOverlay *)RCTMapOverlay:(id)json
|
||||
{
|
||||
json = [self NSDictionary:json];
|
||||
NSArray<NSDictionary *> *locations = [RCTConvert NSDictionaryArray:json[@"coordinates"]];
|
||||
CLLocationCoordinate2D coordinates[locations.count];
|
||||
NSUInteger index = 0;
|
||||
for (NSDictionary *location in locations) {
|
||||
coordinates[index++] = [RCTConvert CLLocationCoordinate2D:location];
|
||||
}
|
||||
|
||||
RCTMapOverlay *overlay = [RCTMapOverlay polylineWithCoordinates:coordinates
|
||||
count:locations.count];
|
||||
|
||||
overlay.strokeColor = [RCTConvert UIColor:json[@"strokeColor"]];
|
||||
overlay.identifier = [RCTConvert NSString:json[@"id"]];
|
||||
overlay.lineWidth = [RCTConvert CGFloat:json[@"lineWidth"] ?: @1];
|
||||
return overlay;
|
||||
}
|
||||
|
||||
RCT_ARRAY_CONVERTER(RCTMapOverlay)
|
||||
|
||||
@end
|
||||
|
||||
@@ -13,9 +13,9 @@
|
||||
#import "RCTConvert+MapKit.h"
|
||||
#import "RCTComponent.h"
|
||||
|
||||
extern const CLLocationDegrees RCTMapDefaultSpan;
|
||||
extern const NSTimeInterval RCTMapRegionChangeObserveInterval;
|
||||
extern const CGFloat RCTMapZoomBoundBuffer;
|
||||
RCT_EXTERN const CLLocationDegrees RCTMapDefaultSpan;
|
||||
RCT_EXTERN const NSTimeInterval RCTMapRegionChangeObserveInterval;
|
||||
RCT_EXTERN const CGFloat RCTMapZoomBoundBuffer;
|
||||
|
||||
@interface RCTMap: MKMapView
|
||||
|
||||
@@ -25,11 +25,13 @@ extern const CGFloat RCTMapZoomBoundBuffer;
|
||||
@property (nonatomic, assign) CGFloat maxDelta;
|
||||
@property (nonatomic, assign) UIEdgeInsets legalLabelInsets;
|
||||
@property (nonatomic, strong) NSTimer *regionChangeObserveTimer;
|
||||
@property (nonatomic, copy) NSArray<NSString *> *annotationIds;
|
||||
@property (nonatomic, copy) NSArray<NSString *> *annotationIDs;
|
||||
@property (nonatomic, copy) NSArray<NSString *> *overlayIDs;
|
||||
|
||||
@property (nonatomic, copy) RCTBubblingEventBlock onChange;
|
||||
@property (nonatomic, copy) RCTBubblingEventBlock onPress;
|
||||
|
||||
- (void)setAnnotations:(RCTPointAnnotationArray *)annotations;
|
||||
- (void)setAnnotations:(RCTMapAnnotationArray *)annotations;
|
||||
- (void)setOverlays:(RCTMapOverlayArray *)overlays;
|
||||
|
||||
@end
|
||||
|
||||
@@ -11,6 +11,8 @@
|
||||
|
||||
#import "RCTEventDispatcher.h"
|
||||
#import "RCTLog.h"
|
||||
#import "RCTMapAnnotation.h"
|
||||
#import "RCTMapOverlay.h"
|
||||
#import "RCTUtils.h"
|
||||
|
||||
const CLLocationDegrees RCTMapDefaultSpan = 0.005;
|
||||
@@ -107,32 +109,34 @@ const CGFloat RCTMapZoomBoundBuffer = 0.01;
|
||||
[super setRegion:region animated:animated];
|
||||
}
|
||||
|
||||
- (void)setAnnotations:(RCTPointAnnotationArray *)annotations
|
||||
// TODO: this doesn't preserve order. Should it? If so we should change the
|
||||
// algorithm. If not, it would be more efficient to use an NSSet
|
||||
- (void)setAnnotations:(RCTMapAnnotationArray *)annotations
|
||||
{
|
||||
NSMutableArray<NSString *> *newAnnotationIds = [NSMutableArray new];
|
||||
NSMutableArray<RCTPointAnnotation *> *annotationsToDelete = [NSMutableArray new];
|
||||
NSMutableArray<RCTPointAnnotation *> *annotationsToAdd = [NSMutableArray new];
|
||||
NSMutableArray<NSString *> *newAnnotationIDs = [NSMutableArray new];
|
||||
NSMutableArray<RCTMapAnnotation *> *annotationsToDelete = [NSMutableArray new];
|
||||
NSMutableArray<RCTMapAnnotation *> *annotationsToAdd = [NSMutableArray new];
|
||||
|
||||
for (RCTPointAnnotation *annotation in annotations) {
|
||||
if (![annotation isKindOfClass:[RCTPointAnnotation class]]) {
|
||||
for (RCTMapAnnotation *annotation in annotations) {
|
||||
if (![annotation isKindOfClass:[RCTMapAnnotation class]]) {
|
||||
continue;
|
||||
}
|
||||
|
||||
[newAnnotationIds addObject:annotation.identifier];
|
||||
[newAnnotationIDs addObject:annotation.identifier];
|
||||
|
||||
// If the current set does not contain the new annotation, mark it as add
|
||||
if (![self.annotationIds containsObject:annotation.identifier]) {
|
||||
// If the current set does not contain the new annotation, mark it to add
|
||||
if (![_annotationIDs containsObject:annotation.identifier]) {
|
||||
[annotationsToAdd addObject:annotation];
|
||||
}
|
||||
}
|
||||
|
||||
for (RCTPointAnnotation *annotation in self.annotations) {
|
||||
if (![annotation isKindOfClass:[RCTPointAnnotation class]]) {
|
||||
for (RCTMapAnnotation *annotation in self.annotations) {
|
||||
if (![annotation isKindOfClass:[RCTMapAnnotation class]]) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// If the new set does not contain an existing annotation, mark it as delete
|
||||
if (![newAnnotationIds containsObject:annotation.identifier]) {
|
||||
// If the new set does not contain an existing annotation, mark it to delete
|
||||
if (![newAnnotationIDs containsObject:annotation.identifier]) {
|
||||
[annotationsToDelete addObject:annotation];
|
||||
}
|
||||
}
|
||||
@@ -145,14 +149,51 @@ const CGFloat RCTMapZoomBoundBuffer = 0.01;
|
||||
[self addAnnotations:(NSArray<id<MKAnnotation>> *)annotationsToAdd];
|
||||
}
|
||||
|
||||
NSMutableArray<NSString *> *newIds = [NSMutableArray new];
|
||||
for (RCTPointAnnotation *annotation in self.annotations) {
|
||||
if ([annotation isKindOfClass:[MKUserLocation class]]) {
|
||||
self.annotationIDs = newAnnotationIDs;
|
||||
}
|
||||
|
||||
// TODO: this doesn't preserve order. Should it? If so we should change the
|
||||
// algorithm. If not, it would be more efficient to use an NSSet
|
||||
- (void)setOverlays:(RCTMapOverlayArray *)overlays
|
||||
{
|
||||
NSMutableArray *newOverlayIDs = [NSMutableArray new];
|
||||
NSMutableArray *overlaysToDelete = [NSMutableArray new];
|
||||
NSMutableArray *overlaysToAdd = [NSMutableArray new];
|
||||
|
||||
for (RCTMapOverlay *overlay in overlays) {
|
||||
if (![overlay isKindOfClass:[RCTMapOverlay class]]) {
|
||||
continue;
|
||||
}
|
||||
[newIds addObject:annotation.identifier];
|
||||
|
||||
[newOverlayIDs addObject:overlay.identifier];
|
||||
|
||||
// If the current set does not contain the new annotation, mark it to add
|
||||
if (![_annotationIDs containsObject:overlay.identifier]) {
|
||||
[overlaysToAdd addObject:overlay];
|
||||
}
|
||||
}
|
||||
self.annotationIds = newIds;
|
||||
|
||||
for (RCTMapOverlay *overlay in self.overlays) {
|
||||
if (![overlay isKindOfClass:[RCTMapOverlay class]]) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// If the new set does not contain an existing annotation, mark it to delete
|
||||
if (![newOverlayIDs containsObject:overlay.identifier]) {
|
||||
[overlaysToDelete addObject:overlay];
|
||||
}
|
||||
}
|
||||
|
||||
if (overlaysToDelete.count) {
|
||||
[self removeOverlays:(NSArray<id<MKOverlay>> *)overlaysToDelete];
|
||||
}
|
||||
|
||||
if (overlaysToAdd.count) {
|
||||
[self addOverlays:(NSArray<id<MKOverlay>> *)overlaysToAdd
|
||||
level:MKOverlayLevelAboveRoads];
|
||||
}
|
||||
|
||||
self.overlayIDs = newOverlayIDs;
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
|
||||
#import <MapKit/MapKit.h>
|
||||
|
||||
@interface RCTPointAnnotation : MKPointAnnotation <MKAnnotation>
|
||||
@interface RCTMapAnnotation : MKPointAnnotation <MKAnnotation>
|
||||
|
||||
@property (nonatomic, copy) NSString *identifier;
|
||||
@property (nonatomic, assign) BOOL hasLeftCallout;
|
||||
@@ -7,8 +7,8 @@
|
||||
* of patent rights can be found in the PATENTS file in the same directory.
|
||||
*/
|
||||
|
||||
#import "RCTPointAnnotation.h"
|
||||
#import "RCTMapAnnotation.h"
|
||||
|
||||
@implementation RCTPointAnnotation
|
||||
@implementation RCTMapAnnotation
|
||||
|
||||
@end
|
||||
@@ -16,7 +16,8 @@
|
||||
#import "RCTMap.h"
|
||||
#import "RCTUtils.h"
|
||||
#import "UIView+React.h"
|
||||
#import "RCTPointAnnotation.h"
|
||||
#import "RCTMapAnnotation.h"
|
||||
#import "RCTMapOverlay.h"
|
||||
|
||||
#import <MapKit/MapKit.h>
|
||||
|
||||
@@ -48,7 +49,8 @@ RCT_EXPORT_VIEW_PROPERTY(maxDelta, CGFloat)
|
||||
RCT_EXPORT_VIEW_PROPERTY(minDelta, CGFloat)
|
||||
RCT_EXPORT_VIEW_PROPERTY(legalLabelInsets, UIEdgeInsets)
|
||||
RCT_EXPORT_VIEW_PROPERTY(mapType, MKMapType)
|
||||
RCT_EXPORT_VIEW_PROPERTY(annotations, RCTPointAnnotationArray)
|
||||
RCT_EXPORT_VIEW_PROPERTY(annotations, RCTMapAnnotationArray)
|
||||
RCT_EXPORT_VIEW_PROPERTY(overlays, RCTMapOverlayArray)
|
||||
RCT_EXPORT_VIEW_PROPERTY(onChange, RCTBubblingEventBlock)
|
||||
RCT_EXPORT_VIEW_PROPERTY(onPress, RCTBubblingEventBlock)
|
||||
RCT_CUSTOM_VIEW_PROPERTY(region, MKCoordinateRegion, RCTMap)
|
||||
@@ -71,9 +73,9 @@ RCT_CUSTOM_VIEW_PROPERTY(region, MKCoordinateRegion, RCTMap)
|
||||
|
||||
- (void)mapView:(RCTMap *)mapView didSelectAnnotationView:(MKAnnotationView *)view
|
||||
{
|
||||
if (mapView.onPress && [view.annotation isKindOfClass:[RCTPointAnnotation class]]) {
|
||||
if (mapView.onPress && [view.annotation isKindOfClass:[RCTMapAnnotation class]]) {
|
||||
|
||||
RCTPointAnnotation *annotation = (RCTPointAnnotation *)view.annotation;
|
||||
RCTMapAnnotation *annotation = (RCTMapAnnotation *)view.annotation;
|
||||
mapView.onPress(@{
|
||||
@"action": @"annotation-click",
|
||||
@"annotation": @{
|
||||
@@ -87,9 +89,9 @@ RCT_CUSTOM_VIEW_PROPERTY(region, MKCoordinateRegion, RCTMap)
|
||||
}
|
||||
}
|
||||
|
||||
- (MKAnnotationView *)mapView:(__unused MKMapView *)mapView viewForAnnotation:(RCTPointAnnotation *)annotation
|
||||
- (MKAnnotationView *)mapView:(__unused MKMapView *)mapView viewForAnnotation:(RCTMapAnnotation *)annotation
|
||||
{
|
||||
if (![annotation isKindOfClass:[RCTPointAnnotation class]]) {
|
||||
if (![annotation isKindOfClass:[RCTMapAnnotation class]]) {
|
||||
return nil;
|
||||
}
|
||||
|
||||
@@ -142,12 +144,24 @@ RCT_CUSTOM_VIEW_PROPERTY(region, MKCoordinateRegion, RCTMap)
|
||||
return annotationView;
|
||||
}
|
||||
|
||||
- (MKOverlayRenderer *)mapView:(__unused MKMapView *)mapView rendererForOverlay:(RCTMapOverlay *)overlay
|
||||
{
|
||||
if ([overlay isKindOfClass:[RCTMapOverlay class]]) {
|
||||
MKPolylineRenderer *polylineRenderer = [[MKPolylineRenderer alloc] initWithPolyline:overlay];
|
||||
polylineRenderer.strokeColor = overlay.strokeColor;
|
||||
polylineRenderer.lineWidth = overlay.lineWidth;
|
||||
return polylineRenderer;
|
||||
} else {
|
||||
return nil;
|
||||
}
|
||||
}
|
||||
|
||||
- (void)mapView:(RCTMap *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
|
||||
{
|
||||
if (mapView.onPress) {
|
||||
|
||||
// Pass to js
|
||||
RCTPointAnnotation *annotation = (RCTPointAnnotation *)view.annotation;
|
||||
RCTMapAnnotation *annotation = (RCTMapAnnotation *)view.annotation;
|
||||
mapView.onPress(@{
|
||||
@"side": (control == view.leftCalloutAccessoryView) ? @"left" : @"right",
|
||||
@"action": @"callout-click",
|
||||
|
||||
18
React/Views/RCTMapOverlay.h
Normal file
18
React/Views/RCTMapOverlay.h
Normal file
@@ -0,0 +1,18 @@
|
||||
/**
|
||||
* Copyright (c) 2015-present, Facebook, Inc.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This source code is licensed under the BSD-style license found in the
|
||||
* LICENSE file in the root directory of this source tree. An additional grant
|
||||
* of patent rights can be found in the PATENTS file in the same directory.
|
||||
*/
|
||||
|
||||
#import <MapKit/MapKit.h>
|
||||
|
||||
@interface RCTMapOverlay : MKPolyline <MKAnnotation>
|
||||
|
||||
@property (nonatomic, copy) NSString *identifier;
|
||||
@property (nonatomic, strong) UIColor *strokeColor;
|
||||
@property (nonatomic, assign) CGFloat lineWidth;
|
||||
|
||||
@end
|
||||
14
React/Views/RCTMapOverlay.m
Normal file
14
React/Views/RCTMapOverlay.m
Normal file
@@ -0,0 +1,14 @@
|
||||
/**
|
||||
* Copyright (c) 2015-present, Facebook, Inc.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This source code is licensed under the BSD-style license found in the
|
||||
* LICENSE file in the root directory of this source tree. An additional grant
|
||||
* of patent rights can be found in the PATENTS file in the same directory.
|
||||
*/
|
||||
|
||||
#import "RCTMapOverlay.h"
|
||||
|
||||
@implementation RCTMapOverlay
|
||||
|
||||
@end
|
||||
Reference in New Issue
Block a user