Cluster, MKMapView show many time same annotation - swift

So, I have a swift application (swift 3) who use a map, in this map I have many annotation who gets up to date when user change region of map.
I wanted to use the pod "Cluster", I add it and when I zoom in to show what was in the cluster, the annotations appear several times almost at the same place (some time it form a circle, see picture)
I have already tried to filter my list to get only once instance of annotation in cluster but it doesn't works.
This method is call each time the user change region :
func createAnnotations(pois list:[PlaceModel]) {
poiAnnotations.removeAll()
let filteredAnnotations = mapView.annotations.filter { annotation in
//if annotation is MKUserLocation { return false } // don't remove MKUserLocation
if let temp = annotation.subtitle, let value = temp {
return value == "Place"
}
return false
}
clusterManager.remove(poiAnnotations)
clusterManager.remove(filteredAnnotations)
for poi in list {
let centerLocation = CLLocation(latitude: mapView.centerCoordinate.latitude, longitude: mapView.centerCoordinate.longitude)
if poi.getLocation().distance(from: centerLocation) > Double(Constants.POI_Radius) {
continue
}
let annotation = Annotation()
annotation.coordinate = CLLocationCoordinate2D(latitude: poi.latitude!, longitude: poi.longitude!)
annotation.title = poi.name!
annotation.subtitle = "Place"
annotation.type = .image( Images.MapPins.velhop)
// For first value
if poiAnnotations.isEmpty {
poiAnnotations.append(annotation)
}
for pois in poiAnnotations {
if poiAnnotations.contains(where: {$0.title == poi.name}) {
// Do nothing
} else {
poiAnnotations.append(annotation)
}
}
}
clusterManager.add(poiAnnotations)
}
And Here the place where the clusters and the annotations are created :
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
if let annotation = annotation as? ClusterAnnotation {
let identifier = "Cluster"
var view = mapView.dequeueReusableAnnotationView(withIdentifier: identifier)
// Type for cluster
let color = UIColor(red: 255/255, green: 149/255, blue: 0/255, alpha: 1)
let type:ClusterAnnotationType = .color(color, radius: 25)
if let view = view as? BorderedClusterAnnotationView {
view.annotation = annotation
view.configure(with: type)
} else {
view = BorderedClusterAnnotationView(annotation: annotation, reuseIdentifier: identifier, type: type, borderColor: .white)
}
return view
} else {
let identifier = "Pin"
var view = mapView.dequeueReusableAnnotationView(withIdentifier: identifier) as? MKPinAnnotationView
if let view = view {
view.annotation = annotation
} else {
view = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier)
}
return view
}
}
I tried several things that did not succeed..
Thanks in advance.

Related

Reload MKAnnotation in mapView - Swift

I have a problem, I have to constantly reload my view as I have to be able to view within this function the value of a UISlider that is constantly updated through an animation made through a function that manages the touch of a button. I noticed that when I open the map this slider value always remains fixed at the initial value, despite the latter having changed. How do I update the annotations? I have read on other posts about deleting and re-adding them:
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView?
{
if (annotation is MapEarthquakeAnnotation) {
let mapEqAnnotation = annotation as! MapEarthquakeAnnotation
var view: MKPinAnnotationView
print("MARIOCASH",Int(sliderTime.value))
if let dequeuedView = mapView.dequeueReusableAnnotationView(withIdentifier: mapEqAnnotation.identifier) as? MKPinAnnotationView
{
// Recycle old view
dequeuedView.annotation = annotation
view = dequeuedView
}
else
{
// Create new view
view = MKPinAnnotationView(annotation: annotation, reuseIdentifier: mapEqAnnotation.identifier)
view.canShowCallout = true
view.pinTintColor = UIColor.brown
let infoButton = UIButton(type: .infoDark)
infoButton.addTarget(self, action: #selector(infoButtonClicked(_:)), for: .touchUpInside)
view.rightCalloutAccessoryView = infoButton
}
return view
}else if annotation is MapSeismometerAnnotation
{
if let annotation = annotation as? MapSeismometerAnnotation
{
var view: MKPinAnnotationView
/*
// Recycle old view
if let dequeuedView = mapView.dequeueReusableAnnotationView(withIdentifier: annotation.identifier) as? MKPinAnnotationView
{
dequeuedView.annotation = annotation
view = dequeuedView
}
else
{ */
// Create new view
view = MKPinAnnotationView(annotation: annotation, reuseIdentifier: annotation.identifier)
view.canShowCallout = true
view.pinTintColor = UIColor.green
view.image = UIImage(named: "pin-verde")
if annotation.identifier == "redpin"
{
var count = 0
var c = 0.8 //valore iniziale dell'aplha per l'opacità del pin rosso
view.image = UIImage(named: "pin-rosso")
Timer.scheduledTimer(withTimeInterval: 1,repeats: true)
{t in //ogni secondo questo timer cambia il valore dell'alpha del pin che sta vibrando
view.alpha = CGFloat(c)
count += 1
c -= 0.15
if count == 5
{
t.invalidate()
}
}
}
return view
//}
}
return nil
}
return nil
}

Set glyphText of MKMarkerAnnotationView for MKClusterAnnotation

I have a class MapItem which implements MKAnnotation protocol. I am using MKMarkerAnnotationView for displaying Annotations on map.
According to Documentation,
glyphText property of MKMarkerAnnotationView when set to nil, it produces pin image on the marker.
When Clustering the annotation, I want the same pin image on the marker. But system by default sets this to the number of annotations clustered within this cluster.
I even tried setting this property to nil, but has no effect.
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
if let item = annotation as? MapItem {
let annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: "mapItem") as? MKMarkerAnnotationView
?? MKMarkerAnnotationView(annotation: annotation, reuseIdentifier: "mapItem")
annotationView.annotation = item
annotationView.glyphText = nil
annotationView.clusteringIdentifier = "mapItemClustered"
return annotationView
} else if let cluster = annotation as? MKClusterAnnotation {
let clusterView = mapView.dequeueReusableAnnotationView(withIdentifier: "clusterView") as? MKMarkerAnnotationView
?? MKMarkerAnnotationView(annotation: annotation, reuseIdentifier: "clusterView")
clusterView.annotation = cluster
clusterView.glyphText = nil
return clusterView
} else {
return nil
}
}
This is how I do it:
class POIMarkerClusterView: MKMarkerAnnotationView {
override var annotation: MKAnnotation? {
willSet {
update(newValue: newValue)
}
}
private func update(newValue: MKAnnotation?) {
if let cluster = newValue as? MKClusterAnnotation {
self.image = POIClusterImage(poiStatistics: poiStatistics, count: count)
// MKMarkerAnnotationView's default rendering usually hides our own image.
// so we make it invisible:
self.glyphText = ""
self.glyphTintColor = UIColor.clear
self.markerTintColor = UIColor.clear
}
}
}
This means you can set an arbitrary image that is rendered as the annotation view. I create the image dynamically, but you can just borrow the image from the MKMarkerAnnotationView and set it here, so it looks like the pin that you want.
The main trick is to use UIColor.clear to hide what you don't want to see.

Swift Custom Annotation does not get select

I have a custom annotationView on my map, when I select the custom annotation i want to create a action and pass the annotation data to another view. The best way for it to work I would have to use the built in func didSelect. When i do select the custom annotation nothing thing happen. Im not show why the didSelect function did not get called thank you in advance.
class RquestCustomPointAnnotation:MKPointAnnotation {
var image: String!
}
func placeRquestByUsersOnMap(){
//retrieve item from firebase
var markerArray = [MKPointAnnotation]()
let path = "rquest/frontEnd/posts/userCreatedPost"
self.childRef(path).observe(.childAdded, andPreviousSiblingKeyWith: {snapshot, _ in
//get status
if let status = snapshot.childSnapshot(forPath: "status").value as? String {
if status == "pending"{
let indentifier = "rquest"
if let coordinate = snapshot.childSnapshot(forPath: "coordinate").value as? NSDictionary {
let lat = coordinate["lat"] as! Double
let long = coordinate["long"] as! Double
let location = CLLocationCoordinate2D(latitude: lat, longitude: long)
let point = RquestCustomPointAnnotation()
let rquestView = MKAnnotationView(annotation: point, reuseIdentifier: indentifier)
point.image = "22"
let key = snapshot.key
point.coordinate = location
point.accessibilityValue = key
point.accessibilityLabel = "Rquest"
markerArray.append(point)
self.mapView.addAnnotation(rquestView.annotation!)
//create an obserarver to check if it is
let paths = "rquest/frontEnd/posts/userCreatedPost/\(key)/"
self.childRef(paths).observe(.childChanged, andPreviousSiblingKeyWith: { (snap, _) in
if snap.key == "status" {
if let status = snap.value as? String {
if status != "pending" {
for i in markerArray {
if i.accessibilityValue == key {
self.mapView.removeAnnotation(i)
}
}
}
}
}
})
}
}
}
})
}
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
if annotation is RquestCustomPointAnnotation {
let reuseIdentifier = "rquest"
var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseIdentifier)
if annotationView == nil {
annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseIdentifier)
annotationView?.canShowCallout = true
} else {
annotationView?.annotation = annotation
}
let customPointAnnotation = annotation as! RquestCustomPointAnnotation
annotationView?.image = UIImage(named: customPointAnnotation.image)
return annotationView
}
}
func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
print("did select")
}
You have canShowCallout set to true. So, when you tap on it, do you want callout or have it call didSelect? (Usually you'd do one or the other, but not both.) And are you seeing your callout?
I notice a curious behavior that if (a) the annotation doesn't have a title, and (b) the annotation view's canShowCallout is true, then not only can it not show the callout, but it also prevents the didSelect from being called.
You may either want to turn canShowCallout to false, or make sure your annotation has a title.

I want to loop over all the annotations from my Cluster Annotation

I'm using the cocoapod FBAnnotationClusteringSwift and it's possible to group my annotations together. However I want to loop over all those annotations that are clustered together when the cluster annotation is tapped.
How do I do this?
func mapView(mapView: MKMapView, didSelectAnnotationView view: MKAnnotationView) {
if (view.annotation!.isKindOfClass(FBAnnotationCluster) == true){
//I WANT TO LOOP OVER ALL ANNOTATIONS IN THE CLUSTER HERE
}
if (view.annotation!.isKindOfClass(ItemAnnotation) == true){
let annotation = view.annotation! as? ItemAnnotation
if let annotation = annotation, let item = annotation.item, d = delegate{
d.itemAnnotationPressed(item)
}
}
}
func mapView(mapView: MKMapView, didSelectAnnotationView view: MKAnnotationView) {
if (view.annotation!.isKindOfClass(FBAnnotationCluster) == true){
let annotation = view.annotation! as? FBAnnotationCluster
var itemListFromAnnotation = [Item]()
for annotation in (annotation?.annotations)! {
let itemAnnotation = annotation as? ItemAnnotation
itemListFromAnnotation.append((itemAnnotation?.item)!)
}
if let d = delegate{
d.itemClusterAnnotationPressed(itemListFromAnnotation)
}
}
if (view.annotation!.isKindOfClass(ItemAnnotation) == true){
mapView.deselectAnnotation(view.annotation, animated: false)
let annotation = view.annotation! as? ItemAnnotation
if let annotation = annotation, let item = annotation.item, d = delegate{
d.itemAnnotationPressed(item)
}
}
}

MapKit Display Annotation Clusters and Along With Non-Clustered Annotations

I am new to iOS development and currently using the FBAnnotationClusteringSwift to cluster makers. My app needs to have two annotations which will not be clustered, as they indicate the source and destination addresses. The remaining annotations must be clustered as they represent stations.
What I want to achieve is something like the image bellow, where "A" is my source address, and the clusters represent stations:
However what is happening is, as the clusters are created, the Annotation that represents a non-clustered annotation (the source address) disappears as following:
If the library is mixing all annotations together, that is pretty bad, there should be a way to separate those that I've added through clusteredAnnotationsWithinMapRect (stations) and those markers that were already added before (addresses). Here is the relevant part of the current code:
var markerClusterer: FBClusteringManager?
ViewController ... {
override func viewDidLoad() {
super.viewDidLoad()
...
self.markerClusterer = FBClusteringManager()
self.markerClusterer!.delegate = self
}
func cellSizeFactorForCoordinator(coordinator:FBClusteringManager) -> CGFloat{
return 1.0
}
func mapView(mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
// Here is just like the example code from FBAnnotationClusterSwift
var stations: [MKAnnotation] = fetchStations(...)
if (stations.count > 0) {
NSOperationQueue().addOperationWithBlock({
let scale:Double = Double(self.mapView.bounds.size.width) / self.mapView.visibleMapRect.size.width
self.markerClusterer!.addAnnotations(stations)
var annotationArray = stations
// Needed to do that otherwise the clusters never "explode" into pins
if (scale <= 0.06) {
annotationArray = self.markerClusterer!.clusteredAnnotationsWithinMapRect(self.mapView.visibleMapRect, withZoomScale:scale)
}
self.markerClusterer!.displayAnnotations(annotationArray, onMapView: self.mapView)
})
}
}
func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
var reuseId = ""
if (annotation.isKindOfClass(FBAnnotationCluster)) {
reuseId = "Cluster"
var clusterView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId)
if clusterView == nil {
clusterView = FBAnnotationClusterView(annotation: annotation, reuseIdentifier: reuseId, options: nil)
} else {
clusterView!.annotation = annotation
}
return clusterView
} else if (annotation.isKindOfClass(AddressPointAnnotation)) {
reuseId = "AddressPin"
var addressPinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId)
if addressPinView == nil {
addressPinView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
addressPinView!.canShowCallout = true
}
else {
addressPinView!.annotation = annotation
}
let addressPin = annotation as! AddressPointAnnotation
addressPinView!.image = UIImage(named: addressPin.icon)
return addressPinView
} else if (annotation is Station) {
reuseId = "StationPin"
var stationPinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId)
if stationPinView == nil {
stationPinView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
}
else {
stationPinView!.annotation = annotation
}
let stationPin = annotation as! Station
stationPinView!.image = UIImage(named: "station")
return stationPinView
} else {
return nil
}
}
}
// Annotation for the stations
class Station: NSObject, MKAnnotation {
var id: Int = 0
var availableBikes: Int = 0
var availableBikeStands: Int = 0
var coordinate: CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: 39.208407, longitude: -76.799555)
}
// Annotations for addresses
class AddressPointAnnotation: MKPointAnnotation {
var icon: String!
}
class Address: NSObject {
var marker: AddressPointAnnotation?
func addMarker(coordinate: CLLocationCoordinate2D) {
marker = AddressPointAnnotation()
marker!.coordinate = coordinate
// ViewController is passed to the Address, so it can add itself to the map
self.controller.mapView.addAnnotation(marker!)
if (self.direction == SOURCE) {
marker!.title = "Starting address"
marker!.icon = "from"
} else {
marker!.title = "Destination address"
marker!.icon = "to"
}
self.controller.mapView.addAnnotation(marker!)
}
}
Any help, idea, or comment is more than welcome. Thanks.