MapKit - iOS 13 - How to show pin annotations on top on the user location annotation?
Asked Answered
T

5

9

Currently, when I try to add an annotation to the map in the user's location the annotation is not showing up. When I set the annotation (using the same code) besides the user's location it does show up.

The code:

let annotation = MKPointAnnotation()
annotation.coordinate = coordinate // set coordinate
annotation.title = "Title"
annotation.subtitle = "Subtitle"
mapView.addAnnotation(annotation)
Tole answered 29/9, 2019 at 12:15 Comment(3)
Please show more context for this code.Reld
I'm trying to let the user add an annotation pin on a mapKit map in the spot of his location. The map hides the annotation when it is close to the user's location blue dot. Is there any way of changing this behavior. This worked fine on iOS 12Tole
Hi gabguy, did you get any solution?Fibster
A
5

same issue here, it was working fine with iOS 12, now all annotations near the current location marker are not visible anymore. Critical information might be near the user location and needs to be displayed. This is mostly bad when you zoom out the map, as you don't know anymore if there is data since all annotations comes near the user location...

Is there a document that indicates this change from apple? I didn't find any... thanks in advance for someone that finds it.

If there is no options to turn off this behavior, the only workaround I can think of is to manually add the current location annotation ourselves...

Ataxia answered 16/10, 2019 at 12:21 Comment(1)
Using MKPinAnnotationView instead of MKAnnotationView has fixed my issueFibster
K
1

In the callback -mapView:viewForAnnotation on your MKMapViewDelegate be sure that the MKAnnotationView you return leaves its displayPriority as its default value which is MKFeatureDisplayPriorityRequired otherwise MapKit clusters your annotation view with the user location.

Kappel answered 12/11, 2019 at 19:26 Comment(1)
This works with me. It is now called MKFeatureDisplayPriority.requiredDensity
M
1

I have a fix for those having issues. I was having issues in my production app as well because I was just adding the annotations to the map and never calling the viewForAnnoation method. I created a sample project and uploaded it to Github so you can review the code and see what I did. In short, you need to create a class that subclasses NSObject and conforms to the MKAnnotation protocol. If using MKLocalsearch like my project, pass the MKMapItem object back and create a new instance of the new class. You will also need to create a class and subclass MKMarkerAnnotationView.

    class CustomMKMarkerSubclass: MKMarkerAnnotationView {
    override var annotation: MKAnnotation? {
        willSet {
            if let _ = newValue as? MapLocation {
                displayPriority = MKFeatureDisplayPriority.required
                canShowCallout = true
            }
        }
    }
}

For more detail, check the project below.

MapKit Demo Project

Meitner answered 27/11, 2019 at 4:0 Comment(0)
F
0

Using MKPinAnnotationView instead of MKAnnotationView has fixed my issue

- (MKAnnotationView *)mapView:(MKMapView *)tempMapView viewForAnnotation:(id <MKAnnotation>)tempAnnotation{
     if ([tempAnnotation isKindOfClass:[MKUserLocation class]]){
         return nil;
     }
     NSString *identifer = @"Identifer";
     MKPinAnnotationView *dPV = [[MKPinAnnotationView alloc] initWithAnnotation:tempAnnotation reuseIdentifier:identifer];
     [dPV setSelected:YES];
     [dPV setTag:102];
     dPV.canShowCallout = YES;
     [dPV setSelected:YES];
     dPV.draggable = YES;
     return dPV;}
Fibster answered 18/11, 2019 at 13:19 Comment(0)
T
0

It seems like iOS 14 fixed this issue

Tole answered 18/9, 2020 at 14:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.