Using MapKit, MKMapview how can I orientate the map by aligning two coordinates vertical on the screen - objective-c

I have two coordinates that represent a portion of a route, and I want to orientate the MKMapView such that the route, polyline is displayed going from the bottom of the screen to the top of the screen.
Currently I first calculate the angle between the two coordinates, and then rotate the map by setting the camera heading, where 90 degrees is facing north and vertically centred on the screen.
Is there perhaps an easier approach, sample code would be great in either Swift or Objective-C.

Currently I first calculate the angle between the two coordinates, and then rotate the map by setting the camera heading, where 90 degrees is facing north and vertically centred on the screen.
You've got the right idea. There's no way to set the map view's heading directly, so setting the camera heading is the right way to go. Be sure to set the map view's rotationEnabled property to YES to get the map view to use the camera's heading, and if you want to always see the map in plan view (i.e. looking straight down), you can set pitchEnabled to NO.

Related

Rotate a camera on its centre

What I want to do is to rotate a camera on it's centre while the camera position is steady. So as a result when the user clicks and moves the mouse he will get all the perspectives possible from that point of view.
I am using trackback controls so I have tried things like:
controls.target.set(camera.position.x,camera.position.y,camera.position.z);
but it does not give the wanted result. I am looking something like camera rotation on axes x,y,z while the camera position is the same.
How do I do it?
If you want to keep TrackballControls, set a very close target (set the camera position and the controls.target to very close coordinates, but different ones).
Otherwise use PointerLockControls

Camera for Spotlight

I have two orthographic cameras. The first one is for the main display and the other one is used for a spotlight effect.
What I want is, if I move the second camera (spotlight), I want the view port rect to be normalized as well. Right now, if I move the second camera's Transform, it looks at the camera's direction but the display is not moving.
So, if the display is at the bottom-right corner, and I move the camera at the top-left corner, I want to display the camera at the top-left corner as well instead of displaying it at the bottom-right corner.
Also, I'm building it on iPad.

Resize UIImage/UIImageView without keeping aspect ratio

I have a UIImageView that I've added a PinchGestureRecognizer to. Currently, the image is resized nicely when pinching, but I want to be able to resize the image without maintaining the aspect ratio. So if the user pinches horizontally, the image view's width would enlarge; if they pinch vertically, the height would enlarge and so forth.
Can anyone give me a hint on how I could do that please?
Write a custom gesture recognizer that requires two fingers to be on screen.
Once both fingers are on screen store their offset to the imageView's border in some UIEdgeInsets.
In touchesMoved, check if both fingers are onscreen: if so, calculate the new frame by applying the edgeInsets in the current touch position.
Header: click
Implementation: click
Works well and feels more natural than other implementations I've seen.
You would need to do the touch handling yourself as UIPinchGestureRecognizer only supports one scale which has no concept of being pinched horizontally or vertically.
You could create your own subclass of UIGestureRecognizer (see here for docs) which looked at the horizontal and vertical separation of the touch points to determine 2 different scales. It should be fairly straightforward to create I would have thought. Just look at the initial touch points and then when they move, calculate the difference in the current separation of the touches to the initial separation of the touches, in both the vertical and horizontal directions.

Difficulty finding correct anchor point for rotation transforms

I'm animating a button's position, rotating it around a circle. This creates a UISwitch-like behavior, except it is a lot more fancy. The button rotates, but it ends up off the desired position by about 0.25 radians. I'm trying to figure out where to put the anchor point to make the button rotate in a perfect circle around its origin.
Here's the code that I use to make the button "orbit" with a 120 pixel radius from the original location.
float offsetX=120;
float offsetY=0;
enableDisableButton.layer.anchorPoint =
CGPointMake(offsetX/enableDisableButton.frame.size.width,
offsetY/enableDisableButton.frame.size.height);
I use the following method to do the calculations. Passing an argument of 90 for degrees, I expect to see the button start at a 180˚ position and move to 90˚, still 120 pixels away from its origin
-(CGAffineTransform)calculateLabelPositionFromFixedPointWithOffsetClockwiseInDegrees:(float)degrees
{
float rotation = degrees*(2*M_PI/360);
CGAffineTransform transform24 = CGAffineTransformMakeRotation(rotation);
NSLog(#"rotation: %f", rotation);
return transform24;
}
This little 0.25 radian or so offset means that I need to visually confirm the location of each button and cannot easily adjust its location programmatically. Any help in determining the correct way to rotate the button is appreciated.
I tried other ways to rotate objects. For example, I have an arrow
<--------x, and I would like to rotate this arrow in a circle around x . What is the correct anchor point placement for this operation? Is it [1, 0.5]?
An easier way to do this kind of rotations is to put an object within a symmetric UIView, center it at the desired point of rotation and assign a rotation transform. This works, but requires the view to be twice as big:
Y----x----Y < this rotates Y around center point X without any anchor point adjustments. this is a very useful method to rotate arrows within analog gauges and such.
"An easier way to do this kind of rotations is to put an object within a symmetric UIView, center it at the desired point of rotation and assign a rotation transform." this way is fine.
The anchor point of your enableDisableButton in the example would be
CGPointMake(offsetX/enableDisableButton.frame.size.width, 0)
i.e. not vertically centered in your button. Rotating around the anchor point would result in the button being offset to the top or bottom of the desired position. The transform looks alright, so I think it is just the anchor point. It should be:
CGPointMake(offsetX/enableDisableButton.frame.size.width, 0.5f)

What's the RIGHT way to draw an image in the upper left corner on a mac?

I have an NSView in a ScrollView and I'm trying to draw an image in it. The problem is that I want the upper left corner of the image locked to the upper left corner of the frame, instead of the lower left corner, which is what the View wants me to do. I will need to be able to zoom in and out, and rotate.
currently, I have a kludge of a system where I calculate how much I have to translate my image based on the size of the image and the size of the window. In order to do this, I needed to create an extra view outside the scrollview, so that I could get the size of the window, not including decorations. Then I can calculate the size of the view based on the size of the image and the size of the window, and based on THAT, I can figure out where to translate the image to.
My only other thought was to use the isFlipped: method, but that ends up reversing my image L-R which is bad.
Is there another way I should be doing this?
If you want 0,0 to be in the upper-left corner, then overriding -isFlipped to return YES is the way to go. It should not affect the coordinate systems of any subviews (I think!), but images drawn directly into the flipped view will appear upside-down unless you apply a transform to them.
View Programming Guide for Cocoa: View Geometry