Comments (5)
What is the reason behind using UIGestureRecognizer? And what is the relation between animated
and viewDidLoad()
? And can you send me a small sample project that demonstrates the bug you're experiencing?
from fbannotationclusteringswift.
You can see below you project with this class FBViewController: UIViewController with one pices of code I added to drop a pin with UIGesturRecognizer :
Just replace your class FBViewController with this code below and you will notice when you drop a pin, it will disappear once you go left or right or zoom out or in in the map.
Thank you
class FBViewController: UIViewController {
@IBOutlet weak var mapView: MKMapView!
let numberOfLocations = 1000
let clusteringManager = FBClusteringManager()
override func viewDidLoad() {
super.viewDidLoad()
let array:[MKAnnotation] = randomLocationsWithCount(numberOfLocations)
clusteringManager.addAnnotations(array)
clusteringManager.delegate = self;
mapView.centerCoordinate = CLLocationCoordinate2DMake(0, 0);
let uilpgr = UILongPressGestureRecognizer(target: self, action: "action:")
uilpgr.minimumPressDuration = 0.1
mapView.addGestureRecognizer(uilpgr)
}
func action(longPressGestureRecognizer:UILongPressGestureRecognizer){
if longPressGestureRecognizer.state == UIGestureRecognizerState.Began {
let touchPoint = longPressGestureRecognizer.locationInView(self.mapView)
let newCoordinate = self.mapView.convertPoint(touchPoint, toCoordinateFromView: self.mapView)
// to know where exactly the pin is (adress)
let location = CLLocation(latitude : newCoordinate.latitude, longitude: newCoordinate.longitude)
let annotation = MKPointAnnotation()
annotation.coordinate = newCoordinate
annotation.title = title
print("\(title)")
self.mapView.addAnnotation(annotation)
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Utility
func randomLocationsWithCount(count:Int) -> [FBAnnotation] {
var array:[FBAnnotation] = []
for _ in 0...count {
let a:FBAnnotation = FBAnnotation()
a.coordinate = CLLocationCoordinate2D(latitude: drand48() * 40 - 20, longitude: drand48() * 80 - 40 )
array.append(a)
print("\(array)")
}
return array
}
}
from fbannotationclusteringswift.
When you pan the map, regionDidChangeAnimated
gets called. Within this delegate method, the clusteringManager
will displayAnnotations
that intersect with the current map region (clusteredAnnotationsWithinMapRect
). In other words, panning the map refreshes it with all the annotations already loaded into the clustering manager.
You probably want to add the annotation to the clustering manager (in addition to dropping it on the map). Within your action
method, try adding this line to the end:
clusteringManager.addAnnotations([annotation])
from fbannotationclusteringswift.
Awesome thank you ! I have another question, How to retrieve the title and the subtitle of the cluster inside the viewcontroller ?
from fbannotationclusteringswift.
I think it might make more sense to get the title and subtitle of the pin rather than the cluster. This is how you could go about it:
In FBAnnotation.swift
, add a subtitle
property:
class FBAnnotation : NSObject {
var coordinate = CLLocationCoordinate2D(latitude: 39.208407, longitude: -76.799555)
var title: String? = ""
var subtitle: String? = "" // NEW
}
In ViewController.swift
, populate the title
and subtitle
when you create the random pins:
func randomLocationsWithCount(count:Int) -> [FBAnnotation] {
var array:[FBAnnotation] = []
for _ in 0...count {
let a:FBAnnotation = FBAnnotation()
a.coordinate = CLLocationCoordinate2D(latitude: drand48() * 40 - 20, longitude: drand48() * 80 - 40 )
a.title = "lat: \(a.coordinate.latitude)" // NEW
a.subtitle = "long: \(a.coordinate.longitude)" // NEW
array.append(a)
}
return array
}
Enable the callout when a pin is tapped:
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)
return clusterView
} else {
reuseId = "Pin"
var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId) as? MKPinAnnotationView
pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
pinView!.pinTintColor = UIColor.greenColor()
pinView?.canShowCallout = true // NEW!
return pinView
}
}
Since the gesture recognizer conflicts with the pin callout, comment out the following line:
// mapView.addGestureRecognizer(uilpgr)
from fbannotationclusteringswift.
Related Issues (20)
- How not to decluster annotations that are close enough to each other HOT 2
- How do I get all annotations from a cluster ? Like if there is a cluster of 20 annotations, I want to get that data of 20 annotations. HOT 2
- Dynamic annotation count HOT 5
- Dynamic change image of annotation base on annotation status
- How to identify how many annotations does a cluster annotation has HOT 1
- ContiguousArrayStorage memory leak HOT 1
- Pod install older version HOT 1
- How to hide cluster count when we do full zoom out. HOT 1
- Custom Image Template
- Cluster to Pin change animation
- Cluster Pins OverLapping Each Other
- Is there a way to achieve cluster onclick? HOT 1
- Is it possible to show overlay in cluster? HOT 1
- clustering same type of annotations
- Unable to set title to clustered annotation
- how to use this library into objective c HOT 1
- Update Swift 4? HOT 7
- How to load geojson data with this library in swift for MKPolylines?
- No clustering for less than 4 markers
- Lots of errors HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from fbannotationclusteringswift.