MKMapView center coordinates inside specific frame - ios

Is there a way to center mapView on specific coordinates in specific mapView's frame?
Let me illustrate:
I have a centered pin:
At some point of time popup view may appear and I need to center my pin in visible mapView rect:
Moreover I have to do this during popup appearing animating (this is another issue since popup.frame is no KVO compliant)
What I have:
I've implemented fast-&-dirty solution by centering mapView on fake location.
Simply I copied fake coordinates from original and replaced latitude, this lets original pin to be on top of popup.
But this approach not really elegant and not accurate.
Important:
I can't change mapView's frame simple because popup has indents through which map is visible.

Related

fix a MKPointAnnotation to centre of map (MapKit) even while user is scrolling

I need to fix an MKPointAnnotation to the center of the map even while the user is scrolling the map, it should still stay in middle. just like Ola app
By far I achieved searching, setting annotation to the center of the map and changing annotation to center again when user scrolls
however it updates when the user stops scrolling, but I want it to stay in the middle always so the user can scroll to an exact location.

MKMapView Swift : MKPointPointAnnotation Centre of screen

I'm trying to build a location selector in swift. The idea is :
When the map appears, an annotation pin appears at the user location. Then the user can change the location of the pin by moving the map around. (the pin stays at the centre of the screen. Like the uber app).
I have no issue spawning an annotation at the user location when the map loads. But I can't find a way to keep the pin at the centre of the screen when the user moves the map around.
I believe I must use regiondidChangeAnimated function, but I can`t find a way to update smoothly the position of the annotation.
Thank you for your help !
When you want some view to not move when you pan the map, just put it on the map view's superview (e.g. add a UIImageView to that superview), add the necessary constraints so that it's centered where you want it, and make sure that user interaction is disabled with that image view. Then you can pan the map and the image view will gracefully float there, above the map, not moving at all.

iOS touch event triggering by movement of other view

Here's the scenario I am trying to implement:
I already have a view that can let user draw doodles by touching inside the view directly (like a doodle canvas). This view implements touchesBegan, touchMoved, and touchEnded handlers to draw lines from touch event parameters.
Now instead of that, I want user to be able to drag and move another UIView on this canvas view and can still draw lines just like they touch directly. For example, user can drag a pen image view on the canvas view to draw pen-style lines.
In this case, how can I transfer the movement of this pen image view to the canvas so it recognize it? One more question: if I want this canvas view to only recognize movements of drag other views rather than touching directly, what I should do?
(Sorry that this question is little too general, just want to get some pointer of it)... Thanks!
A better way to look at the problem is
How can I transfer movement on the canvas into the location of a pen
image view?
That's easy. You already have all the code that keeps track of movement in the canvas (touchesBegan, touchesMoved, touchesEnded), so all you need to do is change to center property of the pen view to track the movement in the canvas. (Obviously, you'll need to apply small X and Y offsets to put the center of the pen view at the correct location).
The only non-obvious detail that you need to be aware of is that the pen view must have userInteractionEnabled set to NO. That way, the pen view won't interfere with touches reaching the canvas view.
Note that UIImageView has user interaction disabled by default, so you don't need to do anything if the pen view is a UIImageView. However, if you're using a generic UIView to display the pen, then you need to disable user interaction in storyboard under the Attributes inspector or disable it in code, e.g. in viewDidLoad.

Tracking MKMapView centerCoordinate while panning

I'm implementing a map feature in my app where I allow the user to set their current location by panning around.
All this time, I want to have an MKAnnotation in the centerCoordinate. So what I want to do is keep track of when the map's centerCoordinate changes and change the annotation's coordinate correctly. The behaviour would be similar to that of Uber, Hailo and others.
I tried a time based implementation where every 0.00001s the centerCoordinate would be checked and the annotation would also be moved. But if the map isn't flicked gently, the annotation jumps from one place to another which doesn't make for a good UI.
Another implementation I tried is by way of gesture recognisers and the delegate methods of MKMapView (regionDidChange/regionWillChange). This, again, makes for a very abrupt transition.
Can anyone please advise me on how to do this better?
I suggest not using an actual id<MKAnnotation> at all (at least for this "current location setting" mode).
Instead:
Add a view (eg. UIImageView) containing an image of a pin (or whatever icon you like) in front of the map view.
This pin view should not be a subview of the map view.
The pin view should be a subview of the same view that the map view is a subview of (eg. both should be subviews of the same superview).
The pin view should be sized and positioned such that it appears above the center of the map view (you could make the pin view have the same frame and the same autolayout/autoresizing logic as the map view so they stay visually synchronized regardless of screen size or orientation).
If using a UIImageView, set its content mode to "center" and background color to "clear" (default is clear).
The pin view should have user interaction disabled on it so that the user can still interact with the map view behind it. As the user pans or zooms the map view, the pin view in front will seem to move instantly.
If necessary, the app can get the location coordinates from mapView.centerCoordinate in the regionDidChangeAnimated: MKMapViewDelegate method (or pan/pinch gesture recognizers) or only when the user says they're done positioning. I don't recommend using a timer (especially every 0.00001s) to query the current center coordinate.
When the user indicates that the current position is where they want to finally place the annotation, you can then create and add an actual annotation at that coordinate and hide the "location setting mode" pin view.

Draw animations dynamically (interactively) on MKMapView

I want to draw an animation over MKMapView. I want it to be something like a compass arrow, that follows (rotates) user's taps / swipes .
So the arrow goes from the center of the screen and is of a fixed length. I don't need the line to be coordinate-specific, but I need to keep the map interactions intact (i.e. still being able to pinch-zoom on the map).
I tried to do that via MKPolyline (creating and then destroying a line), but that does not work (and from the way I had to do that I feel like it won't work). I wonder what would be the best way to handle that? Quartz?
I would accept just an explanation (which kind of view overlay over what, which classes to use), no code is necessary (but if you have a working example that's so much better ))
I draw views like a map ruler not as subview from MkMapView. I put kMapView and my ruler view into a container view. This works for views which positions are fixed on screen, like on center of screen, and are not related to a geographical position.

Resources