Set limit on Clustering annotations using Mapkit - swift

I have added clustering with annotations in my project and it's working fine but having problem to set the limit.
Please check below image here, grouping is done for 6 annotations but I want to set clustering limit which should start grouping at 20 (So never see one of these with the number 19 or lower)
func mapView(_ mapView: MKMapView, clusterAnnotationForMemberAnnotations memberAnnotations: [MKAnnotation]) -> MKClusterAnnotation {
return MKClusterAnnotation(memberAnnotations: memberAnnotations)
}
Also I tried this but not working for me.
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
let groupedpin = annotation as! MKClusterAnnotation
if groupedpin.memberAnnotations.count >= 20 { //<— Not working
let view = PlaceClusterAnnotationView.annotationView()
view._count = groupedpin.memberAnnotations.count.description
return view
}
return MKAnnotationView()
}

You cannot force annotations to cluster, but you can prevent annotations from clustering. Do not give any view a clusteringIdentifier (meaning, set it to nil to prevent clustering; an empty string "" still acts as a clustering identifier) until you want it to be able to cluster, and keep the view's displayPriority at required until you want it to be able to cluster. To try to make a view cluster, set its displayPriority to 0. If you suddenly set twenty views' displayPriority to 0 then if they are close enough together they will probably all cluster. But you can't guarantee this.

Related

SwiftUI MapKit - Multiple annotations in same map

I've had a look at similar questions but nothing seems to answer this question exactly.
I have a Map view and I want to add pin overlays, as well as a poly line route overlay on top of it. I am very very new at SwiftUI dev (this is my first app), so some help would be appreciated.
At the moment the map only renders the pins, and not the overlay. I'm assuming they need to be put into the same method but cannot figure out how you can return multiple overlays to the mapView.
Here is the Coordinator code:
class Coordinator: NSObject,MKMapViewDelegate{
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
//draw pins
if annotation.isKind(of: MKUserLocation.self){return nil}
else{
let pinAnnotation = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "PIN_VIEW")
pinAnnotation.tintColor = .red
pinAnnotation.animatesDrop = true
pinAnnotation.canShowCallout = true
return pinAnnotation
}
}
private func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKPolylineRenderer? {
//draw route
let render = MKPolylineRenderer(overlay: overlay)
render.strokeColor = .orange
render.lineWidth = 2
return render;
}
}
Here is the output: (no route is drawn). I tried to put the annotations within the same method but couldn't figure out how to return both at the same time (pins are of type MKAnnotationView and the route is of type MKPolylineRenderer...
Help would be really appreciated. Thanks
It should be a separate method, but it shouldn’t be private. Also, the return type should be MKOverlayRenderer.

Adding MKAnnotation to MKMapView above location, not in center

I have this code in MapView Coordinator class for adding annotation to map:
func mapView(_ mapview: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
return mapview.dequeueReusableAnnotationView(withIdentifier: "myiconid")
}
Annotation added in center of my location :
How can I add annotation above my location?
The documentation says:
By default, the center point of an annotation view is placed at the coordinate point of the associated annotation. You can use this property to reposition the annotation view as needed. This x and y offset values are measured in points. Positive offset values move the annotation view down and to the right, while negative values move it up and to the left.
https://developer.apple.com/documentation/mapkit/mkannotationview/1452144-centeroffset
So you might want to change the centerOffset attribute for your annotations...
Annotation are set on the latitude and longitude that you provide while making it. I think there is no means we can place our annotation above our location unless we change our latitude and longitude.

Mapkit annotation anchor point

I'm using a custom image for MapKit annotations using :
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {}
The "pin" shows with the right image now when I add a new annotation to my map, but it seems to use the horizontal and vertical centre of the image as the pin point. Is it possible to make this the bottom centre of the image? I could create a new image thats double the hight with the bottom being blank, but was hoping I could do this programmatically
Thanks

How in Swift to display details of multiple dropped pins

In Swift, I can add map annotation and display details using segue of a dropped pin when the user clicks the "i", but I am having trouble displaying details of multiple dropped pins using segue; unlike ViewTable no index is involved here. How do I do that?
Can this give you some hint?
func mapView(mapView: MKMapView!, annotationView view: MKAnnotationView!,
calloutAccessoryControlTapped control: UIControl!) {
//get the annotation ,maybe you need cast the type
// let anno = view.annotation
//performSegue here
}

Swift 2 MKMapViewDelegate rendererForOverlay compiler warning

I am trying to draw a polyline on a map in Swift 2. It all works well, but I get a compiler warning for this code:
func mapView(mapView: MKMapView!, rendererForOverlay overlay: MKOverlay!) -> MKOverlayRenderer! {
if overlay is MKPolyline {
let polylineRenderer = MKPolylineRenderer(overlay: overlay)
polylineRenderer.strokeColor = UIColor.redColor()
polylineRenderer.lineWidth = 5
return polylineRenderer
}
return nil
}
This will give me a warning says that 'Result and parameters in mapView (rendererForOverlay) have different optionality than expected by protocol MKMapViewDelegate'
Now, this will compile fine, but it bugs me that the compiler warning is showing.
If I change the first line to
func mapView(mapView: MKMapView, rendererForOverlay overlay: MKOverlay) -> MKOverlayRenderer {
by removing the !, the warning will go away but I get an error that the return cannot be nil and the code will not compile anymore.
This is also a follow up to this thread where the same problem was stated but no satisfying answer is available:
Swift 2 MKMapViewDelegate rendererForOverlay optionality
Can anyone shed any light on the correct way to use this function now in Swift 2?
Thanks.
Going by what autocomplete suggests the prototype looks like this:
func mapView(mapView: MKMapView, rendererForOverlay overlay: MKOverlay) -> MKOverlayRenderer
And apparently there's nothing you can do about it, except for returning return MKPolylineRenderer() where normally you would return nil.
To me it looks like an implementation bug, because here's what the documentation says about the returned object:
The renderer to use when presenting the specified overlay on the map. If you return nil, no content is drawn for the specified overlay object.
I suggest you create a case for it in Apple's bug report
Don't return nil. This is only called for overlays you create, so instead of checking if overlay is MKPolyline, check which of your overlays it is. If you have only one, return the specified polyline renderer without checking which one it is.