Strange positioning after using MKAnnotationView centerOffset - ios

in iOS 5.0, I first place annotations on the map and also set the centre offset to CGPointMake(0,-annotationView.frame.size.height/2); right after setting the custom image for the annotation in the mapView:viewForAnnotation: method... after this the pins still appear at the original position which would be the case when the centre offset was not applied.
However, when I pinch or zoom the map, the annotation jumps to the correct position as would be the case with the centerOffset being set and then behaves correctly..
What could be the reason and solution for this?
Any help would be much appreciated.
Baffled!!

I was finally able to solve it by subclassing the MKAnnotationView and in the initWithAnnotation:reuseIdentifier: constructor method, synchronously get the annotationView image and then set the centerOffset to CGPointMake(0.0, -img.size.height/2);
If there are too many annotations, this will slow down the display of annotations, so would need to handle that differently.

Related

Don't resize annotation on MKMapView zoom-in/out

Here is my code: https://ghostbin.com/paste/mvhdx
When my view is zoomed-in/out, the annotations resize. Is there a way to disable this behavior and keep the annotations at the size that they were created at?
Another bonus question, how do you prohibit a MKAnnotationView from rotating as well?
If you want to create shapes that are fixed to geography, you may want to look at overlays instead of annotations.
iOS7AnimatedMapOverlay shows how to add overlays to annotations.

Add a UIView at specific coordinates to a MKMapView

I want to add a UIView to fixed coordinates on the mapview like a annotation. To be more specific the UIView should stay fixed at these coordinates on the mapview when the mapview is in interaction (moving, zooming, etc.) exactly the same like a annotation does it.
I tried to solve it with MKMapPoint but without success.
Hope someone has a tipp!
As I understand you want to use custom annotatin view. There is an answer for that question. I dont think you can use a UIView as you described.
How to create Custom MKAnnotationView and custom annotation title and subtitle

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.

UIScrollview sync a subview origin while zooming

i'm trying to develop a kind of custom map.
So I have a scrollview with zooming enabled.
This scrollview has an UIImageView as its subview that represent the map for the user.
I'd like the user to be able to "drop a pin" like in MapKit.
If I add the pin imageView to the map imageview, the pin image will zoom according to the scrollview zoom, which is not what I want.
I'd like to achieve the same result as in the MapKit Mapview.
The pin should move around when the user pan or zoom, but its image should not become smaller or bigger.
There's a simple way to do this without becoming crazy with math speculations over zoomscale and contentoffset?
Thanks a lot, everybody.
This is more of a guess than anything, but adding the pin's layer as a sublayer of the image view seems like the way to go about doing this.
I did it.
All I had to do was add the pin to the map imageview.
After that on the scrollView delegate, i adjusted the pin scale according to the scrollview zoomscale like this:
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
_redPin.transform = CGAffineTransformMakeScale(1 / scrollView.zoomScale, 1 / scrollView.zoomScale);
}

Custom MKAnnotationView, how to prevent selection when shadow touched

I have got a MapView with some custom MKAnnotation, MKAnnotationView which I use to create nice custom callout.
Anyway, for my main Annotation Pin, I use some nice image of pins with a pre-rendered shadow on their left.
However, I would like the annotation not to get selected when the user touch its shadow. Because when their are a lot of them, the shadow of one can overlap another, and the wrong one gets selected because the shadow gets touched.
I have tried to use a separate image for the shadow and put it in a UIImageView inside the MKAnnotationView but it does not change anything, even if I put enableUserInteraction = NO.
any idea?
Make your MKAnnotationView the size of the image excluding the shadow. Change it's frame so that it encompasses the part you want it to receive touch. The shadow should exceed this frame, but shouldn't be cut off.

Resources