When I'm setting an MKMapCamera with a specific altitude on my map view (MapKit) it doesn't zoom to the correct altitude sometimes. I think it has something to do with the map not being fully loaded so it stops higher (950m or so) instead of the altitude I set (about 280m).
Initially I noticed the issue when I first loaded the map but it seems more related to the lower altitudes. Higher altitudes seems to work OK.
Here's a video demonstrating the problem: https://streamable.com/644l1
In the video I'm setting the same camera twice.
The code for setting the camera:
let distance = currentHole!.teeCoordinate.distance(to: currentHole!.greenCoordinate)
let altitude = Double(distance) * 2.5
let camera = MKMapCamera(
lookingAtCenter: currentHole!.centerCoordinate(),
fromDistance: altitude,
pitch: 0.0,
heading: currentHole!.teeCoordinate.bearing(to: currentHole!.greenCoordinate) - 20
)
mapView.setCamera(camera, animated: true)
I also tried to use something like:
UIView.animate(withDuration: 1.0, animations: { () -> Void in
self.mapView.camera = camera
}, completion: { (done) -> Void in
print("Animation complete")
})
to do the animation instead. It works better (not perfect) when setting the duration to something very high, like 10 seconds or so.
Any ideas on what might be the issue here?
UPDATE:
It seems to only happen with "Satellite Flyover" maps. Satellite is fine.
I don't know for certain why this is happening but I have a theory. When you're using the flyover map types the minimum altitude of the camera is restricted by the tallest structure in the centre of the map.
If you go to the Maps app, set it to 3D Satellite view and go directly above a tall building (say the Empire State Building in New York) you can only pinch to zoom to a little above the height of the building. If you pan the camera away from the tall structure you can pinch to zoom in further. The map won't let you zoom through or inside the structure. If you zoom in to the entrance of a tall building and try to pan towards the building, the map will adjust the altitude upwards without you pinching to zoom out to prevent you passing through the building.
So before the map is fully loaded, it doesn't know what the tallest structure at the centre is going to be. To prevent you zooming inside a tall structure, the map limits the minimum height. After the map is fully loaded and it knows that there are no tall structures it lets you zoom in closer.
When you set a long duration on the animation, it's giving the map a chance to load before it gets to the lower altitude. The map knows that there are no tall structures and allows further zooming in. I would say that if you tried a longer duration animation but throttled the network bandwidth it would stop working again.
Note that the Satellite mode allows you to pass through tall structures.
As a workaround, try using mapViewDidFinishLoadingMap: or mapViewDidFinishRenderingMap:fullyRendered: to know when to zoom in more.
Related
I built a complete structure as a node (with its child nodes) and the user will walk through it using ARKit.
At some point, if the user cannot continue because of some real obstacle in the real world, I added a "pause" button which should freeze whatever the user currently sees in front of the camera, the user could then move freely to some other open space and when the user will release the pause button he/she will be able to resume where they left off (only someplace else in the real world).
A while ago I asked about it in the Apple Developer forum and an Apple Frameworks Engineer gave the following reply:
For "freezing" the scene, you could transform the anchor's position (in world coordinates) to camera coordinates, and then anchor your content to the camera. This will give you the effect that the scene is "frozen", i.e., does not move relative to the camera.
I'm currently not using an anchor because I don't necessarily need to find a flat surface. Rather, my node is placed at a certain position relative to where we start at (0,0,0).
My question is how do I exactly do what the Apple engineer told me to do?
I have the following code which I'm still stuck with. When I add the node to the camera (pointOfView, last line of the code below), it does freeze in place, but I can't get it to freeze in the same position and orientation as it was before it was frozen.
#IBAction func pauseButtonClicked(_ sender: UIButton) {
let currentPosition = sceneView.pointOfView?.position
let currentEulerAngles = sceneView.pointOfView?.eulerAngles
var internalNodeTraversal = lastNodeRootPosition - currentPosition! // for now, lastNodeRootPosition is (0,0,0)
internalNodeTraversal.y = lastNodeRootPosition.y + 20 // just so it’s positioned a little higher in front of the camera
myNode?.removeFromParentNode() // remove the node from the Real World view. Looks like this line has no effect and just adding the node as a child to the camera (pointOfView) is enough, but it feels more right to do this anyway.
myNode?.position = internalNodeTraversal // the whole node is moved respectively in the opposite direction from the root to where I’m standing to reposition the camera in my current position inside the node
// myNode?.eulerAngles = (currentEulerAngles! * -1) — this code put the whole node in weird positions so I removed it
myNode?.eulerAngles.y = currentEulerAngles!.y * -1 // opposite orientation of the node so the camera will be oriented in the same direction
myNode?.eulerAngles.x = 0.3 // just tilting it up a little bit to have a better view, more similar to the view as before it was locked to the camera
// I don’t think I need to change the eulerAngles.z
myNode!.convertPosition(internalNodeTraversal, to: sceneView.pointOfView) // I’m not sure I wrote this correctly. Also, this line doesn’t seem tp change anything
sceneView.pointOfView?.addChildNode(myNode!) // attaching the node to the camera so it will remain stuck while the user moves around until the button is released
}
So I first calculate where in the node I'm currently standing and then I change the position of the node in the opposite direction so that the camera will now be in that position. That seems to be correct.
Now I need to change the orientation of the node so that it will point in the right direction and here things get funky. I've been trying so many things for days now.
I use the eulerAngles for the orientation. If I set the whole vector multiplied by -1, it would show weird orientations. I ended up only using the eulerAngles.y which is the left/right orientation and I hardcoded the x orientation (up/down).
Ultimately what I have in the code above is the closest that I was able to get. If I'm pointing straight, the freeze will be correct. If I turn just a little bit, the freeze will be pretty close as well. Almost the same as what the user saw before the freeze. But the more I turn, the more the frozen image is off and more slanted. At some point (say I turn 50 or 60 degrees to the side) the whole node is off the camera and cannot be seen.
Somehow I have a feeling that there must be an easier and more correct way to achieve the above.
The Apple engineer wrote to "transform the anchor's position (in world coordinates) to camera coordinates". For that reason I added the "convertPosition" function in my code, but a) I'm not sure I used it correctly and b) it doesn't seem to change anything in my code if I have that line or not.
What am I doing wrong?
Any help would be very much appreciated.
Thanks!
I found the solution!
Actually, the problem I had was not even described as I didn't think it was relevant. I built the AR nodes 2 meters in front of the origin (-2 for the z-coordinate) while the center of my node was still at the origin. So when I changed the rotation or eulerAngles, it rotated around the origin so my nodes moved in a large curve and in fact also changed their position as a result.
The solution was to use a simdPivot. Instead of changing the position and rotation of the node itself, I created a translation matrix and a rotation matrix which was at the point of the camera (where the user is standing) and I then multiplied both matrices. Now when I added the node as a child of the camera (pointOfView) this would freeze the image and in effect show exactly what the user was seeing before it was frozen as the position is the same and the rotation is exactly around the user's standing position.
I am working on a program that should detect the pins on the map when the user is approaching to some distance, and the pin has to be in a certain angle of view. I have imported MapKit and added all pins to the annotation. Now my app is working but takes into account all pins in the map. I need to take into account only the pins that are in a 30 degree of angle. How to do this?
I think that you are asking how to change the angle of the view so the pins near the distance you say are visible in a different perspective, if im correct this is the answer:
You need to use the method setCamera on the MKMpaView, this receives an MKMapCamera, you can instantiate a camera like this let camera = MKMapCamera(lookingAtCenter:CLLocationCoordinate2D, fromDistance: CLLocationDistance, pitch: CGFloat, heading: CLLocationDirection)
where pitch is the angle, all the other parameters are super clear. when you create the camera the you jus call map.setCamera(camera: camera, animated: true) and thats it.
It's not written anywhere in the documentation, but it's still being able to calculate the viewing angle of MKMapCamera manually. For instance, SCNCamera has a property called fieldOfView, which is vertical viewing angle and it equals 60 degree. If MKMapCamera had the same property, it would be 30 degree.
I am using GMSMarkers on iOS with the Google Maps API. I have a GMSMarker with a PNG image and no matter my zoom level on the map view the GMSMarker keeps its size respective to the screen. If I zoom in it is 30 points across the screen, and if I am zoomed all the way out on the map it is still 30 points. I do NOT want this to happen, I want the GMSMarker to stay small no matter what the zoom level is. It would be preferable that I do not have to loop through all my GMSMarkers every time the camera zoom changes, as I will be eventually having 50-100 of them on the map.
Is there anyway to keep the GMSMarker small no matter the zoom level on the GMSMapView?
I am using Objective-C, but if someone can only give me help in Swift or even Java I can still make do with that.
You could use a GMSGroundOverlay to add an image to the map which scales along with the map. So instead of a fixed size in pixels like a GMSMarker, it stays a fixed size in metres.
Note that another difference is that a ground overlay also stays oriented with the map, ie if the map is rotated/tilted, the top of the image rotates to point north, instead of staying pointing to the top of the screen like a marker.
I'm displaying several user's locations on a map simultaneously as circles of different colors.
I can do this using an annotation and then when the user's location updates use UIView:animateWithDuration: to move to their new location.
However there is a requirement that the size of the circles reflects the accuracy of the location i.e. very accurate equals a circle of size 10 meters, rough accuracy is represented as a circle of size 500 meters etc.
However there are two problems using annotations for this - the first is how to transform meters into a CGRect on the correct size to draw on the map. And the second is the annotations need to be resized if the user zooms the map.
So I was looking at using an overlay instead as that already has a radius and automatic resizing during zooming built in so it handles those two problems.
However it looks like overlays are meant to be static and their coordinate property is read only.
Is there some way I can make the overlays move as the user's location moves? (other than completely remove it and re-add it?)
[THis is for iOS 7 only]
In iOS 7, an MKMapView can be rotated by the user (like in the Maps app).
I have overlays and to determine whether I can display them, I need to compute the zoom scale. In iOS 6, I used to do:
MKZoomScale zoomScale = self.mapView.bounds.size.width / self.mapView.visibleMapRect.size.width;
The problem is that the result of this computation changes when the user rotates the map, where as the actual zoom scale should be the same (the size of the overlays tiles is the same, it's just rotated).
So my problem is the following: how to compute the real zoom scale that does not change when the user rotates the map ? If I had the rotation angle, I could correct the "bias" but I could not find any property in MKMapView to have this angle.
A workaround would be to disable map rotation, but I want to keep this feature.
Thanks in advance.
Instead of using a computed zoom scale, you can use the new MKMapCamera altitude property. It won't change as the map rotates and I think it stays the same even if the user changes the map's pitch angle.
MKMapCamera *camera = self.mapView.camera;
CLLocationDistance altitude = camera.altitude;
if (altitude < 3000 && altitude > 1000) {
// do something
}
If you still need to know the rotation angle, you can get that from the map camera too:
CLLocationDirection mapAngle = camera.heading;