Skip to content
This repository has been archived by the owner on Mar 26, 2021. It is now read-only.

Commit

Permalink
[ios] Add insets and MBXDebugView to visualize insets.
Browse files Browse the repository at this point in the history
[core] Add insets in CameraOptions and applied them in Transform to endPoint.
  • Loading branch information
Larry Geromegnace committed Jan 14, 2016
1 parent 3585588 commit 2d7b54e
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 2 deletions.
5 changes: 5 additions & 0 deletions include/mbgl/ios/MGLMapView.h
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,11 @@ IB_DESIGNABLE
*/
- (void)setZoomLevel:(double)zoomLevel animated:(BOOL)animated;

/**
*/

@property (nonatomic) UIEdgeInsets mapViewInsets;

/**
The heading of the map, measured in degrees clockwise from true north.
Expand Down
5 changes: 4 additions & 1 deletion include/mbgl/map/camera.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include <mbgl/util/geo.hpp>
#include <mbgl/util/chrono.hpp>
#include <mbgl/util/unitbezier.hpp>

#include <mbgl/map/map.hpp>
#include <functional>

namespace mbgl {
Expand All @@ -32,6 +32,9 @@ struct CameraOptions {
/** Pitch toward the horizon measured in radians, with 0 rad resulting in a
two-dimensional map. */
mapbox::util::optional<double> pitch;

/** Insets to apply to the bounds of the map. They impact the map's center.*/
mapbox::util::optional<EdgeInsets> insets;
};

/** Various options for describing a transition between viewpoints with
Expand Down
18 changes: 18 additions & 0 deletions ios/app/MBXDebugView.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//
// MBXDebugView.h
// ios
//
// Created by Mappy SA on 04/01/2016.
//
//

#import <UIKit/UIKit.h>

@interface MBXDebugView : UIView

@property (nonatomic, assign) UIEdgeInsets insets;

- (instancetype)initWithFrame:(CGRect)frame andWithInsets:(UIEdgeInsets) insets;

@end

39 changes: 39 additions & 0 deletions ios/app/MBXDebugView.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//
// MBXDebugView.m
// ios
//
// Created by Mappy SA on 04/01/2016.
//
//

#import "MBXDebugView.h"

@implementation MBXDebugView

- (instancetype)initWithFrame:(CGRect)frame andWithInsets:(UIEdgeInsets) insets {
if (self = [super initWithFrame:frame]) {
self.insets = insets;
self.backgroundColor = [UIColor clearColor];
[self setUserInteractionEnabled:NO];
}
return self;
}

- (void)drawRect:(__unused CGRect) rect{
UIEdgeInsets insets = self.insets;
CGContextRef context = UIGraphicsGetCurrentContext();
CGRect insetsRect = CGRectMake(insets.left,
insets.top,
self.frame.size.width - insets.right - insets.left,
self.frame.size.height - insets.bottom - insets.top);

UIColor * redColor = [UIColor colorWithRed:1.0 green:0.0 blue:0.0 alpha:1.0];

CGContextSetStrokeColorWithColor(context, redColor.CGColor);
CGContextStrokeRect(context, insetsRect);
CGPoint center = CGPointMake(CGRectGetMidX(insetsRect), CGRectGetMidY(insetsRect));
CGRect circleRect = CGRectMake(center.x - 10, center.y - 10, 20, 20);
CGContextStrokeEllipseInRect(context, circleRect);
}

@end
7 changes: 7 additions & 0 deletions ios/app/MBXViewController.mm
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

#import <CoreLocation/CoreLocation.h>
#import <OpenGLES/ES2/gl.h>
#import "MBXDebugView.h"


static UIColor *const kTintColor = [UIColor colorWithRed:0.120 green:0.550 blue:0.670 alpha:1.000];
static NSString * const kCustomCalloutTitle = @"Custom Callout";
Expand Down Expand Up @@ -63,6 +65,7 @@ - (void)viewDidLoad
[super viewDidLoad];

self.mapView = [[MGLMapView alloc] initWithFrame:self.view.bounds];
self.mapView.mapViewInsets = UIEdgeInsetsMake(400, 300, 0, 0);
self.mapView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.mapView.delegate = self;
[self.view addSubview:self.mapView];
Expand Down Expand Up @@ -93,6 +96,10 @@ - (void)viewDidLoad
[self.mapView addGestureRecognizer:[[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)]];

[self restoreState:nil];

MBXDebugView *debugView = [[MBXDebugView alloc] initWithFrame:self.mapView.frame andWithInsets:self.mapView.mapViewInsets];
[self.view addSubview:debugView];

}

- (void)saveState:(__unused NSNotification *)notification
Expand Down
1 change: 1 addition & 0 deletions platform/ios/src/MGLMapView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -1624,6 +1624,7 @@ - (void)_setCenterCoordinate:(CLLocationCoordinate2D)centerCoordinate zoomLevel:
mbgl::CameraOptions cameraOptions;
cameraOptions.center = MGLLatLngFromLocationCoordinate2D(centerCoordinate);
cameraOptions.zoom = fmaxf(zoomLevel, self.currentMinimumZoom);
cameraOptions.insets = {self.mapViewInsets.top, self.mapViewInsets.left, self.mapViewInsets.bottom, self.mapViewInsets.right};
if (direction >= 0)
{
cameraOptions.angle = MGLRadiansFromDegrees(-direction);
Expand Down
10 changes: 9 additions & 1 deletion src/mbgl/map/transform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ void Transform::easeTo(const CameraOptions& camera, const AnimationOptions& anim
double zoom = camera.zoom ? *camera.zoom : getZoom();
double angle = camera.angle ? *camera.angle : getAngle();
double pitch = camera.pitch ? *camera.pitch : getPitch();
EdgeInsets insets = camera.insets ? *camera.insets : EdgeInsets({});

if (!latLng || std::isnan(zoom)) {
return;
Expand All @@ -97,7 +98,7 @@ void Transform::easeTo(const CameraOptions& camera, const AnimationOptions& anim
state.latY(startLatLng.latitude),
};
unwrapLatLng(latLng);
const PrecisionPoint endPoint = {
PrecisionPoint endPoint = {
state.lngX(latLng.longitude),
state.latY(latLng.latitude),
};
Expand Down Expand Up @@ -125,6 +126,13 @@ void Transform::easeTo(const CameraOptions& camera, const AnimationOptions& anim
state.panning = latLng != startLatLng;
state.scaling = scale != startScale;
state.rotating = angle != startAngle;

if (!isGestureInProgress()) {
double insetX = (insets.left - insets.right) / 2;
double insetY = (insets.top - insets.bottom) / 2;
endPoint.x -= insetX * std::cos( angle) + insetY * std::cos(state.pitch) * std::sin(angle);
endPoint.y -= insetX * std::sin(-angle) + insetY * std::cos(angle) * std::cos(state.pitch);
}

startTransition(camera, animation, [=](double t) {
PrecisionPoint framePoint = util::interpolate(startPoint, endPoint, t);
Expand Down

0 comments on commit 2d7b54e

Please sign in to comment.