From 89226adbb6f1cbad140be9fa7db9e5ce3ad262ce Mon Sep 17 00:00:00 2001 From: Christopher Dro Date: Wed, 6 Jan 2016 18:27:30 -0800 Subject: [PATCH] Expose method for adding single annotations. --- index.ios.js | 6 +- ios/API.md | 1 + ios/RCTMapboxGL/RCTMapboxGL.m | 1 - ios/RCTMapboxGL/RCTMapboxGLManager.m | 109 +++++++++++---------------- 4 files changed, 49 insertions(+), 68 deletions(-) diff --git a/index.ios.js b/index.ios.js index 92148ff..9c7f5fb 100644 --- a/index.ios.js +++ b/index.ios.js @@ -19,6 +19,9 @@ var MapMixins = { addAnnotations(mapRef, annotations) { NativeModules.MapboxGLManager.addAnnotations(React.findNodeHandle(this.refs[mapRef]), annotations); }, + updateAnnotation(mapRef, annotation) { + NativeModules.MapboxGLManager.updateAnnotation(React.findNodeHandle(this.refs[mapRef]), annotation); + }, selectAnnotationAnimated(mapRef, selectedIdentifier) { NativeModules.MapboxGLManager.selectAnnotationAnimated(React.findNodeHandle(this.refs[mapRef]), selectedIdentifier); }, @@ -28,9 +31,6 @@ var MapMixins = { removeAllAnnotations(mapRef) { NativeModules.MapboxGLManager.removeAllAnnotations(React.findNodeHandle(this.refs[mapRef])); }, - updateAnnotation(mapRef, annotation) { - NativeModules.MapboxGLManager.updateAnnotation(React.findNodeHandle(this.refs[mapRef]), annotation); - }, setVisibleCoordinateBoundsAnimated(mapRef, latitudeSW, longitudeSW, latitudeNE, longitudeNE, paddingTop, paddingRight, paddingBottom, paddingLeft) { NativeModules.MapboxGLManager.setVisibleCoordinateBoundsAnimated(React.findNodeHandle(this.refs[mapRef]), latitudeSW, longitudeSW, latitudeNE, longitudeNE, paddingTop, paddingRight, paddingBottom, paddingLeft); }, diff --git a/ios/API.md b/ios/API.md index e27bbbc..35442c6 100644 --- a/ios/API.md +++ b/ios/API.md @@ -49,6 +49,7 @@ These methods require you to use `MapboxGLMap.Mixin` to access the methods. Each | `setCenterCoordinateZoomLevelAnimated` | `mapViewRef`, `latitude`, `longitude`, `zoomLevel` | Moves the map to a new coordinate and zoom level | `addAnnotations` | `mapViewRef`, `` (array of annotation objects, see [#annotations](https://github.com/bsudekum/react-native-mapbox-gl/blob/master/API.md#annotations)) | Adds annotation(s) to the map without redrawing the map. Note, this will remove all previous annotations from the map. | `selectAnnotationAnimated` | `mapViewRef`, `marker id` | Open the callout of the selected annotation. This method requires that you supply an id to an annotation when creating. If 2 annotations have the same id, only the first annotation will be selected. Only works on annotation `type = 'point'``. +| `updateAnnotation` | `mapViewRef`, `annotation object` | Replace annotation if it exists on the map. This check happens based on the `id` of the object being passed in. The annotation will still be added if no previous one exists. | `removeAnnotation` | `mapViewRef`, `marker id` | Removes annotation from map. This method requires that you supply an id to an annotation when creating. If 2 annotations have the same id, only the first will be removed. | `removeAllAnnotations` | `mapViewRef`| Removes all annotations from the map. | `setVisibleCoordinateBoundsAnimated` | `mapViewRef`, `latitude1`, `longitude1`, `latitude2`, `longitude2`, `padding top`, `padding right`, `padding bottom`, `padding left` | Changes the viewport to fit the given coordinate bounds and some additional padding on each side. diff --git a/ios/RCTMapboxGL/RCTMapboxGL.m b/ios/RCTMapboxGL/RCTMapboxGL.m index a548044..445333a 100644 --- a/ios/RCTMapboxGL/RCTMapboxGL.m +++ b/ios/RCTMapboxGL/RCTMapboxGL.m @@ -366,7 +366,6 @@ RCT_EXPORT_MODULE(); if (keyCount > 0) { [_map removeAnnotation:[_annotations objectForKey:selectedIdentifier]]; [_annotations removeObjectForKey:selectedIdentifier]; - } } diff --git a/ios/RCTMapboxGL/RCTMapboxGLManager.m b/ios/RCTMapboxGL/RCTMapboxGLManager.m index a7ba2f6..845a4a4 100644 --- a/ios/RCTMapboxGL/RCTMapboxGLManager.m +++ b/ios/RCTMapboxGL/RCTMapboxGLManager.m @@ -68,7 +68,6 @@ RCT_EXPORT_MODULE(); }; - RCT_EXPORT_VIEW_PROPERTY(accessToken, NSString); RCT_EXPORT_VIEW_PROPERTY(centerCoordinate, CLLocationCoordinate2D); RCT_EXPORT_VIEW_PROPERTY(clipsToBounds, BOOL); @@ -76,12 +75,31 @@ RCT_EXPORT_VIEW_PROPERTY(debugActive, BOOL); RCT_EXPORT_VIEW_PROPERTY(direction, double); RCT_EXPORT_VIEW_PROPERTY(rotateEnabled, BOOL); RCT_EXPORT_VIEW_PROPERTY(scrollEnabled, BOOL); -RCT_EXPORT_VIEW_PROPERTY(zoomEnabled, BOOL); RCT_EXPORT_VIEW_PROPERTY(showsUserLocation, BOOL); RCT_EXPORT_VIEW_PROPERTY(styleURL, NSURL); RCT_EXPORT_VIEW_PROPERTY(userTrackingMode, int); +RCT_EXPORT_VIEW_PROPERTY(zoomEnabled, BOOL); RCT_EXPORT_VIEW_PROPERTY(zoomLevel, double); + +RCT_CUSTOM_VIEW_PROPERTY(annotations, CLLocationCoordinate2D, RCTMapboxGL) { + if ([json isKindOfClass:[NSArray class]]) { + NSMutableArray* annotations = [NSMutableArray array]; + id annotationObject; + NSEnumerator *enumerator = [json objectEnumerator]; + [view removeAllAnnotations]; + + while (annotationObject = [enumerator nextObject]) { + CLLocationCoordinate2D coordinate = [RCTConvert CLLocationCoordinate2D:annotationObject]; + if (CLLocationCoordinate2DIsValid(coordinate)){ + [annotations addObject:convertToMGLAnnotation(annotationObject)]; + } + } + + view.annotations = annotations; + } +} + RCT_CUSTOM_VIEW_PROPERTY(attributionButtonIsHidden, BOOL, RCTMapboxGL) { BOOL value = [json boolValue]; @@ -200,16 +218,14 @@ RCT_EXPORT_METHOD(removeAllAnnotations:(nonnull NSNumber *) reactTag) RCT_EXPORT_METHOD(updateAnnotation:(nonnull NSNumber *) reactTag annotation:(NSDictionary *) annotation) { - NSLog(@"Updating Annotation %@", convertObjectToPoint(annotation)); NSString *id = [annotation valueForKey:@"id"]; if ([id length] != 0) { [_bridge.uiManager addUIBlock:^(RCTUIManager *uiManager, NSDictionary *viewRegistry) { RCTMapboxGL *mapView = viewRegistry[reactTag]; if ([mapView isKindOfClass:[RCTMapboxGL class]]) { - NSLog(@"REMOVING %@", id); [mapView removeAnnotation:id]; - [mapView addAnnotation:convertObjectToPoint(annotation)]; + [mapView addAnnotation:convertToMGLAnnotation(annotation)]; } }]; } else { @@ -241,28 +257,7 @@ RCT_EXPORT_METHOD(addAnnotations:(nonnull NSNumber *)reactTag while (annotationObject = [enumerator nextObject]) { CLLocationCoordinate2D coordinate = [RCTConvert CLLocationCoordinate2D:annotationObject]; if (CLLocationCoordinate2DIsValid(coordinate)){ - - if (![annotationObject objectForKey:@"type"]) { - RCTLogError(@"type point, polyline or polygon required"); - return; - } - - NSString *type = [RCTConvert NSString:[annotationObject valueForKey:@"type"]]; - - if ([type isEqual: @"point"]) { - [annotationsArray addObject:convertObjectToPoint(annotationObject)]; - - } else if ([type isEqual: @"polyline"]) { - [annotationsArray addObject:convertObjectToPolyline(annotationObject)]; - - - } else if ([type isEqual: @"polygon"]) { - [annotationsArray addObject:convertObjectToPolygon(annotationObject)]; - - } else { - RCTLogError(@"type point, polyline or polygon required"); - return; - } + [annotationsArray addObject:convertToMGLAnnotation(annotationObject)]; } } mapView.annotations = annotationsArray; @@ -270,43 +265,30 @@ RCT_EXPORT_METHOD(addAnnotations:(nonnull NSNumber *)reactTag }]; } -RCT_CUSTOM_VIEW_PROPERTY(annotations, CLLocationCoordinate2D, RCTMapboxGL) { - if ([json isKindOfClass:[NSArray class]]) { - NSMutableArray* annotations = [NSMutableArray array]; - id annotationObject; - NSEnumerator *enumerator = [json objectEnumerator]; - [view removeAllAnnotations]; - - while (annotationObject = [enumerator nextObject]) { - CLLocationCoordinate2D coordinate = [RCTConvert CLLocationCoordinate2D:annotationObject]; - if (CLLocationCoordinate2DIsValid(coordinate)){ - - if (![annotationObject objectForKey:@"type"]) { - RCTLogError(@"type point, polyline or polygon required"); - return; - } - - NSString *type = [RCTConvert NSString:[annotationObject valueForKey:@"type"]]; - - if ([type isEqual: @"point"]) { - [annotations addObject:convertObjectToPoint(annotationObject)]; - - } else if ([type isEqual: @"polyline"]) { - [annotations addObject:convertObjectToPolyline(annotationObject)]; - - - } else if ([type isEqual: @"polygon"]) { - [annotations addObject:convertObjectToPolygon(annotationObject)]; - - } else { - RCTLogError(@"type point, polyline or polygon required"); - return; - } - } - } - - view.annotations = annotations; +NSObject *convertToMGLAnnotation (NSObject *annotationObject) +{ + if (![annotationObject valueForKey:@"type"]) { + RCTLogError(@"type point, polyline or polygon required"); + return nil; } + + NSString *type = [RCTConvert NSString:[annotationObject valueForKey:@"type"]]; + + if ([type isEqual: @"point"]) { + return convertObjectToPoint(annotationObject); + + } else if ([type isEqual: @"polyline"]) { + return convertObjectToPolyline(annotationObject); + + + } else if ([type isEqual: @"polygon"]) { + return convertObjectToPolygon(annotationObject); + + } else { + RCTLogError(@"type point, polyline or polygon required"); + return nil; + } + } NSObject *convertObjectToPoint (NSObject *annotationObject) @@ -454,7 +436,6 @@ NSObject *convertObjectToPolyline (NSObject *annotationObject) NSObject *convertObjectToPolygon (NSObject *annotationObject) { - NSString *title = @""; if ([annotationObject valueForKey:@"title"]) { title = [RCTConvert NSString:[annotationObject valueForKey:@"title"]];