[ios, osx] Ignore invalid observation context
If a subclass of MGLMapView registers itself as an observer of a non-multipoint annotation’s coordinate key path but fails to handle the change in its -observeValueForKeyPath:ofObject:change:context: implementation, the context would likely be a value other than the annotation’s tag. In that case, avoid doing anything in response to the coordinate changing. Even if the context in that case happens to match the annotation’s tag – say, because the developer passed in NULL (which is equal to 0) for the first added annotation (which would have a tag of 0) – the result would be an unnecessary but harmless update.
This commit is contained in:
committed by
Minh Nguyễn
parent
72086edb6e
commit
2aaa8b9474
@@ -1612,12 +1612,21 @@ mbgl::Duration MGLDurationInSeconds(NSTimeInterval duration)
|
||||
{
|
||||
id <MGLAnnotation> annotation = object;
|
||||
MGLAnnotationTag annotationTag = (MGLAnnotationTag)(NSUInteger)context;
|
||||
const mbgl::LatLng latLng = MGLLatLngFromLocationCoordinate2D(annotation.coordinate);
|
||||
MGLAnnotationImage *annotationImage = [self imageOfAnnotationWithTag:annotationTag];
|
||||
_mbglMap->updatePointAnnotation(annotationTag, { latLng, annotationImage.styleIconIdentifier.UTF8String ?: "" });
|
||||
if (annotationTag == _selectedAnnotationTag)
|
||||
// We can get here because a subclass registered itself as an observer
|
||||
// of the coordinate key path of a non-multipoint annotation but failed
|
||||
// to handle the change. This check deters us from treating the
|
||||
// subclass’s context as an annotation tag. If the context happens to
|
||||
// match a valid annotation tag, the annotation will be unnecessarily
|
||||
// but safely updated.
|
||||
if (annotation == [self annotationWithTag:annotationTag])
|
||||
{
|
||||
[self deselectAnnotation:annotation animated:YES];
|
||||
const mbgl::LatLng latLng = MGLLatLngFromLocationCoordinate2D(annotation.coordinate);
|
||||
MGLAnnotationImage *annotationImage = [self imageOfAnnotationWithTag:annotationTag];
|
||||
_mbglMap->updatePointAnnotation(annotationTag, { latLng, annotationImage.styleIconIdentifier.UTF8String ?: "" });
|
||||
if (annotationTag == _selectedAnnotationTag)
|
||||
{
|
||||
[self deselectAnnotation:annotation animated:YES];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -469,11 +469,19 @@ public:
|
||||
![object isKindOfClass:[MGLMultiPoint class]]) {
|
||||
id <MGLAnnotation> annotation = object;
|
||||
MGLAnnotationTag annotationTag = (MGLAnnotationTag)(NSUInteger)context;
|
||||
const mbgl::LatLng latLng = MGLLatLngFromLocationCoordinate2D(annotation.coordinate);
|
||||
MGLAnnotationImage *annotationImage = [self imageOfAnnotationWithTag:annotationTag];
|
||||
_mbglMap->updatePointAnnotation(annotationTag, { latLng, annotationImage.styleIconIdentifier.UTF8String ?: "" });
|
||||
if (annotationTag == _selectedAnnotationTag) {
|
||||
[self deselectAnnotation:annotation];
|
||||
// We can get here because a subclass registered itself as an observer
|
||||
// of the coordinate key path of a non-multipoint annotation but failed
|
||||
// to handle the change. This check deters us from treating the
|
||||
// subclass’s context as an annotation tag. If the context happens to
|
||||
// match a valid annotation tag, the annotation will be unnecessarily
|
||||
// but safely updated.
|
||||
if (annotation == [self annotationWithTag:annotationTag]) {
|
||||
const mbgl::LatLng latLng = MGLLatLngFromLocationCoordinate2D(annotation.coordinate);
|
||||
MGLAnnotationImage *annotationImage = [self imageOfAnnotationWithTag:annotationTag];
|
||||
_mbglMap->updatePointAnnotation(annotationTag, { latLng, annotationImage.styleIconIdentifier.UTF8String ?: "" });
|
||||
if (annotationTag == _selectedAnnotationTag) {
|
||||
[self deselectAnnotation:annotation];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user