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

Commit

Permalink
refs #1784: basic, naive iOS annotation views
Browse files Browse the repository at this point in the history
 - default to no GL-side symbol by default
 - query delegate for point annotation views
   - only valid for initial viewport load
   - no queueing/reuse
 - update views (pending improvements in #1125) during viewport changes
   - no viewport efficiencies
  • Loading branch information
incanus committed Jun 24, 2015
1 parent 3396bca commit 369b336
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 9 deletions.
2 changes: 2 additions & 0 deletions include/mbgl/ios/MGLMapView.h
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,8 @@ IB_DESIGNABLE
* @return The marker symbol to display for the specified annotation or `nil` if you want to display the default symbol. */
- (nullable NSString *)mapView:(MGLMapView *)mapView symbolNameForAnnotation:(id <MGLAnnotation>)annotation;

- (nullable UIView *)mapView:(MGLMapView *)mapView viewForAnnotation:(id <MGLAnnotation>)annotation;

/** Returns the alpha value to use when rendering a shape annotation. Defaults to `1.0`.
* @param mapView The map view rendering the shape annotation.
* @param annotation The annotation being rendered.
Expand Down
13 changes: 13 additions & 0 deletions ios/app/MBXViewController.mm
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,19 @@ - (void)dealloc

#pragma mark - MGLMapViewDelegate

- (UIView *)mapView:(__unused MGLMapView * __nonnull)mapView viewForAnnotation:(__unused id <MGLAnnotation> __nonnull)annotation
{
// FIXME: user location dot

UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
view.backgroundColor = [UIColor redColor];
view.layer.borderColor = [[UIColor blackColor] CGColor];
view.layer.borderWidth = 1;
view.layer.cornerRadius = 10;

return view;
}

- (BOOL)mapView:(__unused MGLMapView *)mapView annotationCanShowCallout:(__unused id <MGLAnnotation>)annotation
{
return YES;
Expand Down
46 changes: 39 additions & 7 deletions platform/ios/MGLMapView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
const CGFloat MGLMinimumZoom = 3;

NSString *const MGLAnnotationIDKey = @"MGLAnnotationIDKey";
NSString *const MGLAnnotationViewKey = @"MGLAnnotationViewKey";

static NSURL *MGLURLForBundledStyleNamed(NSString *styleName)
{
Expand Down Expand Up @@ -1691,11 +1692,14 @@ - (void)addAnnotations:(NS_ARRAY_OF(id <MGLAnnotation>) *)annotations
std::vector<mbgl::StyleProperties> shapesProperties;

BOOL delegateImplementsSymbolLookup = [self.delegate respondsToSelector:@selector(mapView:symbolNameForAnnotation:)];
BOOL delegateImplementsViewForAnnotation = [self.delegate respondsToSelector:@selector(mapView:viewForAnnotation:)];
BOOL delegateImplementsAlphaForShape = [self.delegate respondsToSelector:@selector(mapView:alphaForShapeAnnotation:)];
BOOL delegateImplementsStrokeColorForShape = [self.delegate respondsToSelector:@selector(mapView:strokeColorForShapeAnnotation:)];
BOOL delegateImplementsFillColorForPolygon = [self.delegate respondsToSelector:@selector(mapView:fillColorForPolygonAnnotation:)];
BOOL delegateImplementsLineWidthForPolyline = [self.delegate respondsToSelector:@selector(mapView:lineWidthForPolylineAnnotation:)];

NSMapTable *annotationViews = [NSMapTable mapTableWithKeyOptions:NSMapTableWeakMemory valueOptions:NSMapTableWeakMemory];

for (id <MGLAnnotation> annotation in annotations)
{
assert([annotation conformsToProtocol:@protocol(MGLAnnotation)]);
Expand Down Expand Up @@ -1778,9 +1782,23 @@ - (void)addAnnotations:(NS_ARRAY_OF(id <MGLAnnotation>) *)annotations
{
points.push_back(MGLLatLngFromLocationCoordinate2D(annotation.coordinate));

UIView *annotationView;

mbgl::LatLngBounds viewportBounds = [self viewportBounds];

if (delegateImplementsViewForAnnotation && viewportBounds.contains(points.back()))
{
annotationView = [self.delegate mapView:self viewForAnnotation:annotation];
annotationView.center = [self convertCoordinate:annotation.coordinate toPointToView:self];

[self addSubview:annotationView];

[annotationViews setObject:annotationView forKey:annotation];
}

NSString *symbolName = nil;

if (delegateImplementsSymbolLookup)
if (annotationView == nil && delegateImplementsSymbolLookup)
{
symbolName = [self.delegate mapView:self symbolNameForAnnotation:annotation];
}
Expand All @@ -1795,7 +1813,10 @@ - (void)addAnnotations:(NS_ARRAY_OF(id <MGLAnnotation>) *)annotations

for (size_t i = 0; i < pointAnnotationIDs.size(); ++i)
{
[self.annotationIDsByAnnotation setObject:@{ MGLAnnotationIDKey : @(pointAnnotationIDs[i]) }
[self.annotationIDsByAnnotation setObject:@{
MGLAnnotationIDKey : @(pointAnnotationIDs[i]),
MGLAnnotationViewKey : [annotationViews objectForKey:annotations[i]]
}
forKey:annotations[i]];
}
}
Expand Down Expand Up @@ -2221,7 +2242,7 @@ - (void)locationManager:(__unused CLLocationManager *)manager didUpdateToLocatio
self.userLocationAnnotationView.haloLayer.hidden = ! CLLocationCoordinate2DIsValid(self.userLocation.coordinate) ||
newLocation.horizontalAccuracy > 10;

[self updateUserLocationAnnotationView];
[self updateAnnotationViews];
}

- (BOOL)locationManagerShouldDisplayHeadingCalibration:(CLLocationManager *)manager
Expand Down Expand Up @@ -2382,7 +2403,7 @@ - (void)notifyMapChange:(NSNumber *)change
case mbgl::MapChangeRegionWillChange:
case mbgl::MapChangeRegionWillChangeAnimated:
{
[self updateUserLocationAnnotationView];
[self updateAnnotationViews];
[self updateCompass];

[self deselectAnnotation:self.selectedAnnotation animated:NO];
Expand Down Expand Up @@ -2419,7 +2440,7 @@ - (void)notifyMapChange:(NSNumber *)change
}
case mbgl::MapChangeRegionIsChanging:
{
[self updateUserLocationAnnotationView];
[self updateAnnotationViews];
[self updateCompass];

if ([self.delegate respondsToSelector:@selector(mapViewRegionIsChanging:)])
Expand All @@ -2430,7 +2451,7 @@ - (void)notifyMapChange:(NSNumber *)change
case mbgl::MapChangeRegionDidChange:
case mbgl::MapChangeRegionDidChangeAnimated:
{
[self updateUserLocationAnnotationView];
[self updateAnnotationViews];
[self updateCompass];

if (self.pan.state == UIGestureRecognizerStateChanged ||
Expand Down Expand Up @@ -2497,8 +2518,19 @@ - (void)notifyMapChange:(NSNumber *)change
}
}

- (void)updateUserLocationAnnotationView
- (void)updateAnnotationViews
{
NSEnumerator *enumerator = self.annotationIDsByAnnotation.keyEnumerator;

while (id <MGLAnnotation> annotation = enumerator.nextObject)
{
CGPoint viewPoint = [self convertCoordinate:annotation.coordinate toPointToView:self];

UIView *view = [self.annotationIDsByAnnotation objectForKey:annotation][MGLAnnotationViewKey];

view.center = viewPoint;
}

if ( ! CLLocationCoordinate2DIsValid(self.userLocation.coordinate)) {
self.userLocationAnnotationView.layer.hidden = YES;
return;
Expand Down
2 changes: 0 additions & 2 deletions src/mbgl/map/annotation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,6 @@ AnnotationManager::addAnnotations(const AnnotationType type,
const std::string& symbol = annotationsProperties.at("symbols")[p];
if (symbol.length()) {
pointFeatureProperties.emplace("sprite", symbol);
} else {
pointFeatureProperties.emplace("sprite", defaultPointAnnotationSymbol);
}

// add individual point tile feature
Expand Down

0 comments on commit 369b336

Please sign in to comment.