For some reason ever since I updated to iOS 11, my callout Accessory isn't being displayed for my annotations. I have a title and subtitle set up from data I query from my server, however I can't get the callout accessory along with the .detailedDisclosure to display.
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
print("viewForannotation")
if annotation is MKUserLocation {
//return nil
return nil
}
// guard let annotation = annotation as? CalloutPin else { return nil }
let reuseId = "pin"
var pinView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseId) as? MKPinAnnotationView
if pinView == nil {
//print("Pinview was nil")
pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
pinView!.canShowCallout = true
pinView!.animatesDrop = true
}
let button = UIButton(type: UIButtonType.detailDisclosure) as UIButton // button with info sign in it
pinView!.canShowCallout = true
pinView!.rightCalloutAccessoryView = button
// pinView?.detailCalloutAccessoryView = button
return pinView
}
Does anyone have a solution for this? Any help would be much appreciated as I've been dealing with this problem for a long time now as I need the callout Accessory to display in order for me to segue to another view controller when my annotation pin is selected. Thanks!
Here's what's being displayed right now: MapView Annotation
Related
I am currently working on creating callouts for annotations I have added to my mapview via MapKit. The annotations work out well but currently callouts aren't being displayed even though I am using the right code to enable them (I believe).
HERE is my viewFor annotation code block.
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
if annotation is MKUserLocation {
return nil
}
if let annotation = annotation as? ClusterAnnotation {
let identifier = "cluster"
return mapView.annotationView(annotation: annotation, reuseIdentifier: identifier)
} else {
let identifier = "pin"
let annotationView = mapView.annotationView(of: MKMarkerAnnotationView.self, annotation: annotation, reuseIdentifier: identifier)
annotationView.isEnabled = true
annotationView.canShowCallout = true
annotationView.accessibilityLabel = "hi"
annotationView.isHidden = false
annotationView.rightCalloutAccessoryView = UIButton(type: .detailDisclosure)
annotationView.markerTintColor = UIColor.customBlue()
annotationView.glyphImage = UIImage(named: "person")
return annotationView
}
}
Extension code block for annotationView function.
extension MKMapView {
func annotationView<T: MKAnnotationView>(of type: T.Type, annotation: MKAnnotation?, reuseIdentifier: String) -> T {
guard let annotationView = dequeueReusableAnnotationView(withIdentifier: reuseIdentifier) as? T else {
return type.init(annotation: annotation, reuseIdentifier: reuseIdentifier)
}
annotationView.annotation = annotation
return annotationView
}
}
The annotation enlarges as I select it, and in the didSelect code block it runs the print statement I run through it. Not exactly sure what is going on that's not allowing the callout to show even though I've literally enabled just about everything.
Please Used This Code.
This code working fine for me.
This Code support Swift4 and Swift5.
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
// Don't want to show a custom image if the annotation is the user's location.
if (annotation is MKUserLocation) {
return nil
} else {
let annotationIdentifier = "AnnotationIdentifier"
let nibName = "MyAnnotationView" //My XIB Name
let viewFromNib = Bundle.main.loadNibNamed(nibName, owner: self, options: nil)?.first as! MyAnnotationView // get My XIB
var annotationView: MyAnnotationView?
// if there is a view to be dequeued, use it for the annotation
if let dequeuedAnnotationView = mapView.dequeueReusableAnnotationView(withIdentifier: annotationIdentifier) as? MyAnnotationView {
if dequeuedAnnotationView.subviews.isEmpty {
dequeuedAnnotationView.addSubview(viewFromNib)
}
annotationView = dequeuedAnnotationView
annotationView?.annotation = annotation
} else {
// if no views to dequeue, create an Annotation View
let av = MyAnnotationView(annotation: annotation, reuseIdentifier: annotationIdentifier)
av.addSubview(viewFromNib)
annotationView = av // extend scope to be able to return at the end of the func
}
// after we manage to create or dequeue the av, configure it
if let annotationView = annotationView {
annotationView.canShowCallout = true // callout bubble
annotationView.rightCalloutAccessoryView = UIButton(type: .detailDisclosure)
annotationView.frame = CGRect(x: 0, y: 0, width: 75, height: 80)
let customView = annotationView.subviews.first as! MyAnnotationView
customView.frame = annotationView.frame
}
return annotationView
}
}
This Code OutPut :
Happy To Help You.
From the source code documentation:
// If YES, a standard callout bubble will be shown when the annotation is selected.
// The annotation must have a title for the callout to be shown.
#property (nonatomic) BOOL canShowCallout;
Without the Title value being set the other items will not show up.
In 2017 MKMarkerAnnotationView was announced to replace MKPinAnnotationView. As described by this WWDC video at 12:21, MKMarkerAnnotationView has three states:
Normal
Selected
Selected with Callout
How do you programmatically set the "Selected with Callout" state so that it displays as it does in the WWDC video? This seems like it should be a super straight forward thing to do, but I see absolutely nothing in the MapKit documentation, the only way I can get it to work reliably is this:
Documentation Links:
MKAnnotationView
MKMarkerAnnotationView
You can use canShowCallout property.
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
if annotation is MKUserLocation {
return nil
}
let reuseId = "pin"
var pinView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseId) as? MKMarkerAnnotationView
if pinView == nil {
pinView = MKMarkerAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
pinView?.canShowCallout = true
let rightButton: AnyObject! = UIButton(type: UIButtonType.detailDisclosure)
pinView?.rightCalloutAccessoryView = rightButton as? UIView
}
else {
pinView?.annotation = annotation
}
return pinView
}
And you need to select the annotation to set the "Selected with Callout" state.
mapView.selectAnnotation(annotation, animated: true)
In my code I am using Apple maps and correctly setting up a mapView and the delegate to self within a View Controller.
Within my ViewController I also have the following code within a for loop:
if userOnMapAlready == false {
let annotation = MKPointAnnotation()
annotation.coordinate = CLLocationCoordinate2D(latitude: otherUserLocation.coordinate.latitude, longitude: otherUserLocation.coordinate.longitude)
annotation.subtitle = otherUserUUID
self.mapView.addAnnotation(annotation)
}
I also have the following delegate function in my view controller, and again am setting mapview.delegate = self within the View Controller:
func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
if annotation is MKUserLocation {
//return nil so map view draws "blue dot" for standard user location
return nil
}
let reuseId = "pin"
var pinView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseId) as? MKPinAnnotationView
if pinView == nil {
pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
pinView!.canShowCallout = true
pinView!.animatesDrop = true
pinView!.pinColor = .purple
}
else {
pinView!.annotation = annotation
}
return pinView
}
Sadly when the pins are drawn they are all red, not purple like the viewForAnnotation would suggest they should be. What is the problem?! Is it something to do with the reuseId not matching the unique subtitle im a assigning to each MKPointAnnotation??
UPDATE: just realized that the delegate function viewForAnnotation is never being called - which would explain why the pins are not purple. Why would viewForAnnotation not be getting called???
In the latest version of Swift the property would be:
pinView!.pinTintColor = .purple
or...
pinView!.pinTintColor = MKPinAnnotationView.purplePinColor()
↳ https://developer.apple.com/reference/mapkit/mkpinannotationview
I want to change the colour of pin. Following is my code to add the pin on map. Thanks in advance. Any kind of help will be appreciated:
map.AddAnnotations(new MKPointAnnotation
{
Title = "Technician's Location",
Coordinate = new CLLocationCoordinate2D(lattitude, longitude)
});
You can use the pinColor property of MKPinAnnotationView.
This can be added in the delegate method func mapView(mapView: MKMapView!,
viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView!
A typical example of what this method can hold is :-
if annotation is MKUserLocation {
return nil
}
let reuseId = "pin"
var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId) as? MKPinAnnotationView
if pinView == nil {
pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
pinView!.animatesDrop = true
pinView!.pinColor = .Purple // you can change color here!
}
else {
pinView!.annotation = annotation
}
return pinView
I'm trying to change my pin annotation image to something other than the pin.
func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {
if annotation is MKUserLocation {
//return nil so map view draws "blue dot" for standard user location
return nil
}
let reuseId = "pin"
var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId) as? MKPinAnnotationView
if pinView == nil {
pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
pinView!.canShowCallout = true
pinView!.animatesDrop = true
pinView!.pinColor = .Green
pinView!.image = UIImage(named: "icon1.png")
// Add image to left callout
var mugIconView = UIImageView(image: UIImage(named: "test.png"))
pinView!.leftCalloutAccessoryView = mugIconView
// Add detail button to right callout
var calloutButton = UIButton.buttonWithType(.DetailDisclosure) as UIButton
pinView!.rightCalloutAccessoryView = calloutButton
}
else {
pinView!.annotation = annotation
}
return pinView
Everything works except changing the pin image. I have a custom point annotation subclass which is this
I've been searching for hours but every other SO question I've found hasn't helped. I'd appreciate any help... thanks.
UPDATED WITH ANSWER
func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {
if annotation is MKUserLocation {
//return nil so map view draws "blue dot" for standard user location
return nil
}
var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier("pin")
if pinView == nil {
pinView = MKAnnotationView(annotation: annotation, reuseIdentifier: "pin")
pinView!.canShowCallout = true
pinView!.image = UIImage(named: "test.png")
// Add image to left callout
var mugIconView = UIImageView(image: UIImage(named: "test.png"))
pinView!.leftCalloutAccessoryView = mugIconView
// Add detail button to right callout
var calloutButton = UIButton.buttonWithType(.DetailDisclosure) as UIButton
pinView!.rightCalloutAccessoryView = calloutButton
}
else {
pinView!.annotation = annotation
}
return pinView
}
You want MKAnnotationView, not MKPinAnnotationView. Pin annotations can't customize their image.
See this SO answer for additional details