Expose method for adding single annotations.

This commit is contained in:
Christopher Dro
2016-01-08 11:53:47 -08:00
parent 937e17a97b
commit 89226adbb6
4 changed files with 49 additions and 68 deletions
+3 -3
View File
@@ -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);
},
+1
View File
@@ -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.
-1
View File
@@ -366,7 +366,6 @@ RCT_EXPORT_MODULE();
if (keyCount > 0) {
[_map removeAnnotation:[_annotations objectForKey:selectedIdentifier]];
[_annotations removeObjectForKey:selectedIdentifier];
}
}
+45 -64
View File
@@ -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<NSNumber *, RCTMapboxGL *> *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"]];