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

#2151 MGLAnnotation centerOffset and calloutOffset properties #3220

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions include/mbgl/ios/MGLAnnotationImage.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,17 @@ NS_ASSUME_NONNULL_BEGIN
*/
@property (nonatomic, getter=isEnabled) BOOL enabled;

/** The offset (in points) at which to display the image.
*
* By default, the center point of an annotation image is placed at the coordinate point of the associated annotation. */
@property(nonatomic) CGPoint centerOffset;


/** The offset (in points) at which to place the callout bubble.
*
* This property determines the additional distance by which to move the callout bubble. */
@property(nonatomic) CGPoint calloutOffset;

@end

NS_ASSUME_NONNULL_END
5 changes: 4 additions & 1 deletion include/mbgl/sprite/sprite_image.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace mbgl {

class SpriteImage : private util::noncopyable {
public:
SpriteImage(PremultipliedImage&&, float pixelRatio, bool sdf = false);
SpriteImage(PremultipliedImage&&, float pixelRatio, bool sdf = false, vec2<float> offset = {0.0, 0.0});

PremultipliedImage image;

Expand All @@ -25,6 +25,9 @@ class SpriteImage : private util::noncopyable {

float getWidth() const { return image.width / pixelRatio; }
float getHeight() const { return image.height / pixelRatio; }

// position offset
const vec2<float> offset;
};

} // namespace mbgl
Expand Down
29 changes: 27 additions & 2 deletions ios/app/MBXViewController.mm
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

static UIColor *const kTintColor = [UIColor colorWithRed:0.120 green:0.550 blue:0.670 alpha:1.000];
static NSString * const kCustomCalloutTitle = @"Custom Callout";
static NSString * const kOffsetMarkerTitle = @"Offset Marker and Callout";

static const CLLocationCoordinate2D WorldTourDestinations[] = {
{ 38.9131982, -77.0325453144239 },
Expand All @@ -21,6 +22,7 @@ @interface MBXViewController () <UIActionSheetDelegate, MGLMapViewDelegate>

@property (nonatomic) MGLMapView *mapView;
@property (nonatomic) NSUInteger styleIndex;
@property (nonatomic, strong) NSDictionary *settingsDictionary;

@end

Expand Down Expand Up @@ -175,12 +177,13 @@ - (void)showSettings
: @"Show Custom Style Layer"),
@"Print Telemetry Logfile",
@"Delete Telemetry Logfile",
@"Add offset annotation",
nil];

[sheet showFromBarButtonItem:self.navigationItem.leftBarButtonItem animated:YES];
}

- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex
- (void)actionSheet:(__unused UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex
{
if (buttonIndex == actionSheet.firstOtherButtonIndex)
{
Expand Down Expand Up @@ -326,12 +329,16 @@ - (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSIn
}
}
}
else if (buttonIndex == actionSheet.firstOtherButtonIndex + 16)
{
[self addOffsetAnnotations];
}
}

- (void)parseFeaturesAddingCount:(NSUInteger)featuresCount
{
[self.mapView removeAnnotations:self.mapView.annotations];

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^
{
NSData *featuresData = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"points" ofType:@"geojson"]];
Expand Down Expand Up @@ -536,6 +543,17 @@ - (NSString *)telemetryDebugLogfilePath
return filePath;
}

- (void)addOffsetAnnotations
{
[self.mapView removeAnnotations:self.mapView.annotations];

MGLPointAnnotation *annotation = [MGLPointAnnotation new];
annotation.coordinate = CLLocationCoordinate2DMake(48.8533940, 2.3775439);
annotation.title = kOffsetMarkerTitle;
[self.mapView addAnnotation:annotation];
[self.mapView showAnnotations:@[annotation] animated:YES];
}

#pragma mark - Destruction

- (void)dealloc
Expand All @@ -554,6 +572,13 @@ - (MGLAnnotationImage *)mapView:(MGLMapView * __nonnull)mapView imageForAnnotati
{
return nil; // use default marker
}
if ([annotation.title isEqualToString:kOffsetMarkerTitle]) {
UIImage *imagePng = [UIImage imageNamed:@"default_marker"];
MGLAnnotationImage *image = [MGLAnnotationImage annotationImageWithImage:imagePng reuseIdentifier:kOffsetMarkerTitle];
image.centerOffset = CGPointMake(0.0, -12.0);
image.calloutOffset = CGPointMake(-8.0, 3.0);
return image;
}

NSString *title = [(MGLPointAnnotation *)annotation title];
if (!title.length) return nil;
Expand Down
Binary file modified platform/ios/resources/default_marker.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified platform/ios/resources/default_marker@2x.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified platform/ios/resources/default_marker@3x.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 4 additions & 2 deletions platform/ios/src/MGLMapView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -2508,8 +2508,10 @@ - (void)installAnnotationImage:(MGLAnnotationImage *)annotationImage

// add sprite
auto cSpriteImage = std::make_shared<mbgl::SpriteImage>(
std::move(cPremultipliedImage),
float(annotationImage.image.scale));
std::move(cPremultipliedImage),
float(annotationImage.image.scale),
Boolean(false),
mbgl::vec2<float>{static_cast<float>(annotationImage.centerOffset.x), static_cast<float>(annotationImage.centerOffset.y)});

// sprite upload
NSString *symbolName = [MGLAnnotationSpritePrefix stringByAppendingString:annotationImage.reuseIdentifier];
Expand Down
2 changes: 1 addition & 1 deletion src/mbgl/annotation/annotation_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ void AnnotationManager::removeIcon(const std::string& name) {

double AnnotationManager::getTopOffsetPixelsForIcon(const std::string& name) {
auto sprite = spriteStore.getSprite(name);
return sprite ? -(sprite->image.height / sprite->pixelRatio) / 2 : 0;
return sprite ? sprite->offset.y - ((sprite->image.height / sprite->pixelRatio) / 2) : 0;
}

} // namespace mbgl
2 changes: 1 addition & 1 deletion src/mbgl/renderer/symbol_bucket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ void SymbolBucket::addFeatures(uintptr_t tileUID,
if (feature.sprite.length()) {
auto image = spriteAtlas.getImage(feature.sprite, false);
if (image) {
shapedIcon = shapeIcon(*image, layout);
shapedIcon = shapeIcon(*image, layout, image->spriteImage->offset);
assert((*image).spriteImage);
if ((*image).spriteImage->sdf) {
sdfIcons = true;
Expand Down
6 changes: 4 additions & 2 deletions src/mbgl/sprite/sprite_image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ namespace mbgl {

SpriteImage::SpriteImage(PremultipliedImage&& image_,
const float pixelRatio_,
bool sdf_)
bool sdf_,
vec2<float> offset_)
: image(std::move(image_)),
pixelRatio(pixelRatio_),
sdf(sdf_) {
sdf(sdf_),
offset(offset_) {

if (image.size() == 0) {
throw util::SpriteImageException("Sprite image dimensions may not be zero");
Expand Down
8 changes: 4 additions & 4 deletions src/mbgl/text/shaping.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@

namespace mbgl {

PositionedIcon shapeIcon(const SpriteAtlasElement& image, const SymbolLayoutProperties& layout) {
float dx = layout.icon.offset.value[0];
float dy = layout.icon.offset.value[1];
PositionedIcon shapeIcon(const SpriteAtlasElement& image, const SymbolLayoutProperties& layout, vec2<float> offset) {
float dx = layout.icon.offset.value[0] + offset.x;
float dy = layout.icon.offset.value[1] + offset.y;
float x1 = dx - image.spriteImage->getWidth() / 2.0f;
float x2 = x1 + image.spriteImage->getWidth();
float y1 = dy - image.spriteImage->getHeight() / 2.0f;
float y2 = y1 + image.spriteImage->getHeight();

return PositionedIcon(image, y1, y2, x1, x2);
}

Expand Down
2 changes: 1 addition & 1 deletion src/mbgl/text/shaping.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ namespace mbgl {

class SymbolLayoutProperties;

PositionedIcon shapeIcon(const SpriteAtlasElement& image, const SymbolLayoutProperties&);
PositionedIcon shapeIcon(const SpriteAtlasElement& image, const SymbolLayoutProperties&, const vec2<float> offset);

} // namespace mbgl

Expand Down