Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

[ios] Warn about using MGLFeature-conforming annotations #9540

Merged

Conversation

friedbunny
Copy link
Contributor

@friedbunny friedbunny commented Jul 18, 2017

Until #6178 is addressed, this adds two warnings about using MGLFeature-conforming objects as annotations:

  • Prints a warning to console when a developer does so.
  • Changes MGLFeature’s documentation to make developers aware of the limitations around using features-as-annotations with feature querying.

This also adds friendlier debug descriptions for various feature/shape types.

/cc @1ec5 @captainbarbosa @boundsj @fabian-guerra

@friedbunny friedbunny added annotations Annotations on iOS and macOS or markers on Android documentation iOS Mapbox Maps SDK for iOS labels Jul 18, 2017
@friedbunny friedbunny added this to the ios-v3.6.1 milestone Jul 18, 2017
@friedbunny friedbunny self-assigned this Jul 18, 2017
@friedbunny friedbunny requested a review from 1ec5 July 18, 2017 23:42
Copy link
Contributor

@1ec5 1ec5 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There’s some mention of features-as-annotations in the “Working with GeoJSON data” guide as well.

We should definitely warn in the feature querying documentation that those methods may return annotations as features – the other way around – but those annotations-as-features aren’t object-equal to the ones that were added to the map. So we should recommend against using feature querying to get annotations.

@@ -3187,6 +3187,11 @@ - (void)addAnnotations:(NS_ARRAY_OF(id <MGLAnnotation>) *)annotations
{
NSAssert([annotation conformsToProtocol:@protocol(MGLAnnotation)], @"annotation should conform to MGLAnnotation");

if ([annotation conformsToProtocol:@protocol(MGLFeature)])
{
NSLog(@"Warning: Annotations that conform to the MGLFeature protocol do not keep their `identifier` or `attributes` properties when added to the map as annotations. Add this feature using runtime styling if you intend to later query the map for it. %@", annotation.description);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let’s make this a one-time warning. It could get very spammy if you try to pass in a full array of features.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

032157a

annotations using `-[MGLMapView addAnnotations:]` and related methods, doing so
will convert feature objects into plain annotations. Features added as
annotations will not have `identifier` or `attributes` properties set and
should not be used be with feature querying.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think a more charitable way to put it is that when you add a feature to the map as an annotation, you give up some feature querying fidelity in exchange for built-in interactivity. It becomes possible to react to taps using -[MGLMapViewDelegate mapView:didSelectAnnotation:], so that in many cases it isn’t necessary to use feature querying at all. (The “annotation” passed into that delegate method does retain the attributes and ID.)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gave this a go in 5fabed1:

screen shot 2017-07-19 at 3 21 57 pm

@friedbunny friedbunny force-pushed the fb-features-as-annotations-trap branch from 64258db to 032157a Compare July 19, 2017 19:19

- Features added as annotations are converted into plain annotations. These
annotations will not have `identifier` or `attributes` properties and should
not be used be with feature querying.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The features do continue to have identifier and attributes properties, for example when they’re passed into -[MGLMapViewDelegate mapView:didSelectAnnotation:]. It’s only feature querying that drops this information, as the feature querying API returns a lossy reconstruction of the original data.

{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
NSLog(@"Annotations that conform to the MGLFeature protocol do not keep their `identifier` or `attributes` properties when added to the map as annotations. Add this feature using runtime styling if you intend to later query the map for it. This warning is only shown once. %@", annotation.description);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we could move this warning to when the developer attempts to query the map. Inside -visibleFeaturesAt:inStyleLayersWithIdentifiers:, warn if any of the values in _annotationContextsByAnnotationTag or keys in _annotationTagsByAnnotation conforms to MGLFeature. Or to make it more performant, set a flag here that enables the check later in -visibleFeaturesAt:inStyleLayersWithIdentifiers:.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coming back to this, I think this doesn’t need a warning — let’s leave it up to the developer to read the docs.

@friedbunny friedbunny force-pushed the fb-features-as-annotations-trap branch from 646e553 to 19e9315 Compare August 18, 2017 17:23
@friedbunny friedbunny force-pushed the fb-features-as-annotations-trap branch from 19e9315 to b5b84ef Compare August 18, 2017 17:25
@friedbunny
Copy link
Contributor Author

@1ec5 I’ve slimmed down the scope of this PR to remove the console warning and to focus specifically on the limitations of using features-as-annotations with feature querying.

Copy link
Contributor

@1ec5 1ec5 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A non-urgent suggestion: also advise against using feature querying to get annotations in each of the feature querying methods’ documentation comments.

@friedbunny friedbunny merged commit b5b84ef into release-ios-v3.6.0-android-v5.1.0 Aug 18, 2017
@friedbunny friedbunny deleted the fb-features-as-annotations-trap branch August 18, 2017 18:40
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
annotations Annotations on iOS and macOS or markers on Android documentation iOS Mapbox Maps SDK for iOS
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants