MapKit Annotations Disappearing
NiltiakSivad's solution works but it reverts to the old iOS 10 look. If you want to keep the new iOS 11 balloon markers for iOS 11 and use the old pin look only for older iOS versions then you can implement the delegate method as below:
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
let reuseIdentifier = "annotationView"
var view = mapView.dequeueReusableAnnotationView(withIdentifier: reuseIdentifier)
if #available(iOS 11.0, *) {
if view == nil {
view = MKMarkerAnnotationView(annotation: annotation, reuseIdentifier: reuseIdentifier)
}
view?.displayPriority = .required
} else {
if view == nil {
view = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseIdentifier)
}
}
view?.annotation = annotation
view?.canShowCallout = true
return view
}
How to fix custom Point Annotations from disappearing from overlap?
You apparently haven’t set the delegate
for your map view because those annotation views are not MKPinAnnotationView
, but rather the default MKMarkerAnnotationView
. If you’re going to implement MKMapViewDelegate
methods, you have to set the delegate
of the map view (either in IB or programmatically).
This disappearing act is because the default MKMarkerAnnotationView
is configured to enable clustering but you haven’t registered a MKMapViewDefaultClusterAnnotationViewReuseIdentifier
.
So, if you really want pin annotation views and you don’t want clustering, set your map view’s delegate
and your method should accomplish what you want.
I’d personally suggest you reduce view controller bloat by moving the configuration of your annotation view into a MKPinAnnotationView
subclass:
class CustomAnnotationView: MKPinAnnotationView { // or use `MKMarkerAnnotationView` if you want
override init(annotation: MKAnnotation?, reuseIdentifier: String?) {
super.init(annotation: annotation, reuseIdentifier: reuseIdentifier)
displayPriority = .required
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
Then, if you’re targeting iOS 11 and later, in your viewDidLoad
, you can register your class, and you don’t have to implement mapView(_:viewFor:)
at all:
mapView.register(CustomAnnotationView.self, forAnnotationViewWithReuseIdentifier: MKMapViewDefaultAnnotationViewReuseIdentifier)
Or, if you want to enjoy clustering properly, you can expand your CustomAnnotationView
:
class CustomAnnotationView: MKPinAnnotationView { // or use `MKMarkerAnnotationView` if you want
static let preferredClusteringIdentifier: String? = Bundle.main.bundleIdentifier! + ".CustomAnnotationView"
override init(annotation: MKAnnotation?, reuseIdentifier: String?) {
super.init(annotation: annotation, reuseIdentifier: reuseIdentifier)
clusteringIdentifier = CustomAnnotationView.preferredClusteringIdentifier
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override var annotation: MKAnnotation? {
didSet {
clusteringIdentifier = CustomAnnotationView.preferredClusteringIdentifier
}
}
}
And then register both your annotation view and a cluster annotation view:
mapView.register(CustomAnnotationView.self, forAnnotationViewWithReuseIdentifier: MKMapViewDefaultAnnotationViewReuseIdentifier)
mapView.register(MKMarkerAnnotationView.self, forAnnotationViewWithReuseIdentifier: MKMapViewDefaultClusterAnnotationViewReuseIdentifier)
Then you enjoy clustering with minimal effort.
annotations not appearing on MapKit
A few observations:
Regarding not seeing the blue dot, the question is whether you set
showsUserLocation
(either programmatically or in the NIB/storyboard).Regarding annotations not showing up, you need to narrow down the problem. I would temporarily remove the
mapView(_:viewFor:)
and see whether you see annotations at that point.- If they do, then the problem is either in
mapView(_:viewFor:)
(see below) or your custom annotation view subclass. - If they do not appear, then the problem is either in your logic of how/when you added annotation or in your custom annotation class.
- With
mapView(_:viewFor:)
removed, you could then try adding aMKPointAnnotation
, rather than yourSuperMarketAnnotation
. That will help you figure out whether the problem is in your annotation class or the the logic about when and where you add annotations.
- If they do, then the problem is either in
FWIW, the
mapView(_:viewFor:)
has a little bug (which may or may not be related). If you successfully dequeue an existing annotation view, you just return it and never update itsannotation
property. You must update theannotation
of any dequeued annotation view or else it will point to the old annotation (and, specifically, its old coordinate).if let view = mapView.dequeueReusableAnnotationView(withIdentifier: SuperMarketAnnotationView.reuseID) {
view.annotation = annotation // make sure you update the annotation for dequeued annotation view
return view
}
Annotations are hidden when zooming out
The current user's annotation doesn't have a cluster identifier. If you provide a cluster identifier for the current user's annotation, it works.
Why does a custom MKMapView annotation image disappear on touch?
Answering my own question here, just in case others have the same issue. Notice that I am using "MKPinAnnotationView" - it should be changed to "MKAnnotationView" and everything works.
Fixed code:
- (MKAnnotationView *)mapView:(MKMapView *)newMapView viewForAnnotation:(id )newAnnotation {
MKAnnotationView *annotation = [[MKAnnotationView alloc] initWithAnnotation:newAnnotation reuseIdentifier:@"currentloc"];
if (annotation == nil) {
annotation = [[MKAnnotationView alloc] initWithAnnotation:newAnnotation reuseIdentifier:@"currentloc"];
}
annotation.image = [UIImage imageNamed:@"anno.png"];
annotation.canShowCallout = YES;
annotation.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bus_stop_30x30.png"]];
annotation.leftCalloutAccessoryView = imgView;
return annotation;
}
Related Topics
Xcode 9 Simulator Error Unable to Boot the Simulator Launchd Failed to Respond MAC Os Sierra
How to Tell If an iOS Device Has a Gps
Swift Switch Pattern Matching with Arrays
Remove Underline on Uibutton in iOS 7
Swift: Load Images Async in Uitableviewcell
In Swift, How to Get the Device Orientation Correctly Right After It's Launched
Breaking on Unrecognized Selector
Open a Wkwebview Target="_Blank" Link in Safari
How to Retrieve a File/Resource in a iOS Framework Bundle from a Main Program
iOS Calculate Text Height in Tableview Cell
How to Overlay a Skscene Over a Scnscene in Swift
Carthage Build Failed Xcode 12 12A7209 Building
Canopenurl Not Working in iOS 10
Convert Arabic String to English Number in Swift
How to Add External Webvtt Subtitles into Http Live Stream on iOS Client
Avplayer Uitapgesturerecognizer Not Working