I have a map in my swift ios app and it is full of markers. Therefore I've decided to use marker clustering taken from here: https://github.com/ribl/FBAnnotationClusteringSwift
After implementing it in my app I see pins and clusters, so that's cool.
This is how I do it - first I call my webservice and fetch all the data for each pin and put it into the array:
func fetchRequests(radius: Double, lat: Double, lon: Double, completionHandler: (() -> Void)?){
Alamofire.request(.GET, "http://mywebservice.com/fetchdata", parameters: ["param": "1"])
.responseJSON { response in
switch response.result {
case .Success:
self.array.removeAll()
if let jsonData = response.result.value as? [[String: AnyObject]] {
for requestJSON in jsonData {
if let request = SingleRequest.fromJSON(JSON(requestJSON)){
let pinOne = FBAnnotation()
pinOne.coordinate = CLLocationCoordinate2D(latitude: request.latitude, longitude: request.longitude)
pinOne.title = request.title
pinOne.subtitle = request.discipline
self.array.append(pinOne)
}
}
}
self.clusteringManager.setAnnotations(self.array)
completionHandler!()
case .Failure(let error):
print("SWITCH ERROR")
print(error)
}
}
}
Then I'm actually calling the function from above and doing:
let annotationArray = self?.clusteringManager.clusteredAnnotationsWithinMapRect(self!.mapView.visibleMapRect, withZoomScale:scale)
self?.clusteringManager.displayAnnotations(annotationArray!, onMapView:(self?.mapView)!)
and at the end I'm putting data on map while checking whether each pin is a cluster or a normal pin:
func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {
var reuseId = ""
if annotation.isKindOfClass(FBAnnotationCluster) {
reuseId = "Cluster"
var clusterView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId)
clusterView = FBAnnotationClusterView(annotation: annotation, reuseIdentifier: reuseId, options: nil)
return clusterView
} else {
reuseId = "Pin"
var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId) as? MKPinAnnotationView
pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
pinView!.calloutOffset = CGPoint(x: -5, y: 5)
pinView!.canShowCallout = true
pinView!.rightCalloutAccessoryView = UIButton(type: .DetailDisclosure) as UIView
return pinView
}
}
So far so good. It works as expected, after clicking each pin I see the popup above it with a title
and subtitle
. But I would like to achieve a different effect.
When user taps a pin - instead of a popup above it - some square component appears below the map. I was looking for some similar solutions to give you as an example and I found it in the app called Periscope
- this is how their map looks before user clicks any event:
and this is what happens right after clicking any point on the map:
This component of course disappears as soon as user scrolls the map.
Is that achievable in Swift
?