I have a TableView that is used to show MapView annotation callouts when the cells are tapped.
In iOS 10 I can centre the MapView on an annotation then show it's callout using:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let location = locations[indexPath.item]
mapView.setCenter(location.coordinate, animated: true)
mapView.selectAnnotation(location, animated: true)
}
locations is an array of MKAnnotations. I'm using MKPinAnnotationViews on iOS 10 and MKMarkerAnnotationViews on iOS 11.
iOS 11 automatically hides and shows MKMarkerAnnotationViews as you zoom the map.
This has the unfortunate side effect of preventing .selectAnnotation() from working reliably because the marker could still be hidden after centering the map.
I've seen the docs and understand why:
If the specified annotation is not onscreen, and therefore does not
have an associated annotation view, this method has no effect.
Is there a way to disable annotation clustering/hiding?
Or some way to force the selected annotation to be visible?
You can set the displayPriority of an MKMarkerAnnotationView to a rawValue of 1000 and the less interesting MKMarkerAnnotationView's displayPriority to something lower. This will cause that marker annotation to take precedence over others.
In your case, you will like want to hold a reference to the annotation that you would like to select, remove that annotation from the map view and add it again. This will cause the map view to request a view for the annotation again and you can adjust the priority so that it is higher than the annotations around it. For example:
func showAnnotation()
{
self.specialAnnotation = annotations.last
self.mapView.removeAnnotation(self.specialAnnotation)
self.mapView.addAnnotation(self.specialAnnotation)
self.mapView.setCenter(self.specialAnnotation.coordinate, animated: true)
self.mapView.selectAnnotation(self.specialAnnotation, animated: true)
}
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView?
{
let markerView = mapView.dequeueReusableAnnotationView(withIdentifier: "Marker", for: annotation) as? MKMarkerAnnotationView
let priority = (annotation as? Annotation) == self.specialAnnotation ? 1000 : 500
markerView?.displayPriority = MKFeatureDisplayPriority(rawValue: priority)
// optionally change the tint color for the selected annotation
markerView?.markerTintColor = priority == 1000 ? .blue : .red
return markerView
}
Where specialAnnotation is an object that conforms to MKAnnotation.
Related
I have custom annotations that sometimes display a textView above them.
They don't display a textView if a variable named text on my annotation is nil.
An annotation may have text to display, but the value of the text variable could change while the annotation is being displayed. In this case I would like the annotation to refresh so that it is no longer displaying the textView.
I already have a delegate function that either creates an annotation with a textView if the annotations text variable is set and creates an annotation without a textView if the text variable of the annotation is not set, it works something like this, although this is not the actual code
func mapView(_ mapView: MGLMapView, viewFor annotation: MGLAnnotation) -> MGLAnnotationView?{
if annotation is MyCustomAnnotation{
if annotation.hasText(){
return MyCustomAnnotationView(hasText: True)
}else{
return ViewWithoutTextView(hasText: False)
}
}
But if the annotation changes from having text to not having text or vice versa while the annotation is already being displayed, then I don't know how to refresh this or call this again so that the right annotation view is displayed
As #Magnas said in the comment, you would have to remove the annotation and re-add it to update the state.
It would be better to create one custom annotation view that has the logic to handle hiding/showing of the text view inside it. Then you just hold onto a reference of the annotation and update that through the annotationView without going through and messing with map annotations at all.
A rough example (lots of blanks to fill):
// your methods in your custom annotation. Use these wherever you want to change things
class CustomAnnotation: MGLAnnotationView {
func showText() { }
func hideText() { }
}
// Define data structure to access your annotation with some kind of key
dataSourceToAnnotationView: [String: CustomAnnotation]
// save your annotations so you can access them later
func mapView(_ mapView: MGLMapView, viewFor annotation: MGLAnnotation) -> MGLAnnotationView? {
var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: "customReuseId")
if annotationView == nil {
annotationView = CustomAnnotation()
let key = "exampleKeyString"
dataSourceToAnnotationView[key] = annotationView as! CustomAnnotation
}
return annotationView
}
I need to paint 100 pins on a map, up to 4 different colors in attention to a variable created by my 'colorOfPin' (green, yellow, red and blue).
Through a function, I loop to generate the 100 annotations of the 100 pines:
func paintPinsIP () {
for item in 0 ... (numberPoints - 1) {
let annotationPin = LocationPoint ()
annotationPin.namePoint= elements [item].namePoint
annotationPin.coordinate = elements [item].coordinate
identificaPin = annotationPin.namePoint
viewOfMap.addAnnotation(annotationPin)
}
}
Next I show those 100 annotations on the map, but each pin must have the color indicated by the variable ‘colorOfPin’ (green, yellow, red or blue):
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
if (annotation is MKUserLocation) {return nil}
let pinIdentifier = identificaPin
var aView = mapView.dequeueReusableAnnotationView(withIdentifier: pinIdentifier)
if aView == nil {
let pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: pinIdentifier)
guard annotation is LocationPoint else {return nil}
pinView.isEnabled = true
pinView.canShowCallout = false
aView = pinView
switch colorOfPin {
case .red: pinView.pinTintColor = UIColor.red
case .yellow: pinView.pinTintColor = UIColor.yellow
case .green: pinView.pinTintColor = UIColor.green
case .blue: pinView.pinTintColor = UIColor.blue
}
} else {
aView?.annotation = annotation
}
return aView
}
But the 100 points appear on the map with the color of the last pin. The color of all the points is the color of pin # 99. As the ‘colorOfPin’ variable of point # 99 is 'blue', the 100 pins appear blue.
How can I do to paint each pin of the color indicated by the variable ‘colorOfPin’?
As the ‘colorOfPin’ variable of point # 99 is 'blue', the 100 pins appear blue.
Set a breakpoint in your paintPinsIP() and mapView(_:viewFor:) methods and take a look at the order in which these methods are called. You're not setting the value of colorOfPin in mapView(_:viewFor:), so it looks like you're expecting the map view to fetch the annotation view as soon as you add the annotation to the map. What really happens, though, is that you add a bunch of annotations to the map, and some time later, when the map tries to draw itself, it calls mapView(_:viewFor:) to get views for any annotations that happen to be visible. By that time, colorOfPin will have changed.
It looks like LocationPoint is probably a class you've created that adopts the MKAnnotation protocol. What you should do to fix your problem is to add a pinColor property to that class, so that you can set the color for the pin when you create the annotation. Then, when the map view calls your mapView(_:viewFor:) method for one of your annotations, you can use it's pinColor property to set the pin annotation view's pinTintColor property.
I have been stuck on this issue for the past day. I have created a custom MKAnnotation subclass to display various custom pins on an MKMapView. I recursively call a function that keeps animating these pins around the map. My goal is to stop all of these animations in place when the user taps on a button. I have tried
self.view.layer.removeAllAnimations()
and
self.map.layer.removeAllAnimations()
and other hacky solutions, but none seem to work.
Below is the code that creates the animation/pin movement
func animate(duration:Double, newLocation:CLLocationCoordinate2D){
UIView.animate(withDuration: duration, animations: {
self.coordinate = newLocation
}) { (done) in
self.finished_segment()
}
}
Any suggestions are much appreciated.
For anyone stuck on this issue. The problem was that I had to remove the animation from the MKAnnotationView associated with the annotation. I basically created a member variable in the custom class that I set in the mapView annotation delegate method as seen below.
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
let annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: "annotationView") ?? MKAnnotationView()
if let a = annotation as? Alien{
annotationView.image = a.image
a.annotationView = annotationView
}
return annotationView
}
And to remove the animation from the map. Simply call
self.annotationView.layer.removeAllAnimations()
This is my first foray into the app world so it's taken a lot of research to get to this point. I'm building a map application and am going for an interface similar to the zillow app seen below. I am trying to come up with the right approach that allows me to click on a map annotation and it brings up a smaller view where I can interact with it. So essentially I have a few questions:
Should I use a subview inside the map controller, or use a container view. Or is there another approach I haven't seen?
How do I push data from the annotation to that function?
How do I keep this subview hidden until an annotation has been clicked?
So far this is the closest thing I can find: Customize MKAnnotation Callout View?
Thanks!
I am new to iOS also, but I have done something similar to what you want to do. I have a view that shows some statistics, speed, bearing, etc. When someone clicks on the annotation, I toggle showing and hiding the statistics. There may be better ways, but here is what I did.
"Executive summary for your questions"
1 and 3) Use a subview over the map that you hide and unhide
2) Subclass both MKAnnotation and MKAnnotationView. Put the data you want to pass in a property of the subclassed MKAnnotationView, and then transfer the property to the MKAnnotationView when you create it. You can then retrieve it from the view passed in to didSelectAnnotationView.
Details
1) and 3) I created a subview that sits on the mapView and set it as hidden in the story board initially. I then have a toggeleMarkerStatistics() func
tion that toggles the visibility of the view. So something like this
func toggleMarkerStatistics() {
if mapMarkerStatistics.hidden {
mapMarkerStatistics.hidden = false
} else {
mapMarkerStatistics.hidden = true
}
}
This function is called from within
func mapView(mapView: MKMapView, didSelectAnnotationView view: MKAnnotationView) { }
2) To get data into the didSelectAnnotationView, here is what I did.
I subclassed both MKAnnotation and MKAnnotationView and added properties to hold the data that I wanted to pass to didSelectAnnotationView. So something like this:
class MyAnnotation: MKPointAnnotation {
var myMarker: MyMapMarker?
}
class MyMKAnnotationView: MKAnnotationView {
var myMarker: MyMapMarker?
}
When you create the annotation, set the property, before you add the annotation to the map.
let annotation = MyAnnotation()
annotation.myMarker = marker
annotation.coordinate = location
annotation.title = "btw, you MUST have a title or bad things happen"
mapView.addAnnotation(annotation)
Then in viewForAnnotation, you will be given your custom annotation with the property you set after you created it and you are asked to create a view for this annotation. Now when you create this view, set the view property to annotation property before you return the view. Now the data you want to pass to didSelectAnnotationView will be available on the view passed to didSelectAnnotationView
func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
let view: MKAnnotationView! = mapView.dequeueReusableAnnotationViewWithIdentifier("marker") ?? MyMKAnnotationView(annotation: annotation, reuseIdentifier: "marker")
view.annotation = annotation
view.canShowCallout = false // you need this to make this work
if let annotation = annotation as? MyAnnotation {
if let view = view as? MyMKAnnotationView {
view.myMarker = annotation.myMarker
}
}
return view
}
Now in didSelectAnnotationView, retrieve the data you set when you created the annotation:
func mapView(mapView: MKMapView, didSelectAnnotationView view: MKAnnotationView) {
if let view = view as? MyMKAnnotationView {
if let marker = view.myMarker {
toggleMarkerStatistics() // hide or unhide the view
// do something with your data
}
}
}
Note:
I tried to copy and simplify this from my code, which actually tries to support both Apple and Google maps, so hopefully I don't have any typo's, but I think is a good representation of the steps I take.
A few more things to note:
I think you must provide a title for the annotaion
I think you must set the view's canShowCallout to false
I think both of these requirements can be found in the documentation, but I don't have a pointer to this right now.
How do you always show the annotation callouts, i.e. don't hide them when you tab the map view?
Resetting the annotations also will bring the callout to view state true.
[mapView removeAnnotation: currentMarker];
[mapView addAnnotation:currentMarker];
The callout is shown when an MKAnnotationView is selected and the view's canShowCallout property is set to YES.
It is then hidden when that MKAnnotationView is deselected. This occurs by tapping another annotation view, or by tapping outside of the currently selected annotation view.
As the delegate of MKMapView (conforming to MKMapViewDelegate), you are told when an annotation view is selected and deselected, but it's too late to do anything about it.
If you want to not deselect an annotation view, you should subclass MKAnnotationView and override the setSelected:animated: method and stop the annotation view from being deselected.
Thanks, #Zumry Mohammed for this idea. This solution in swift works for me:
func mapView(_ mapView: MKMapView, didDeselect view: MKAnnotationView) {
guard let ann = view.annotation else {return}
mapView.removeAnnotation(ann)
mapView.addAnnotation(ann)
mapView.selectAnnotation(ann, animated: false)
}
I just set isSelected property to true on viewFor annotation method and that is all.
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
if annotation is MKUserLocation {
return nil
}
let annotationV = MKAnnotationView(annotation: annotation, reuseIdentifier: nil)
annotationV.image = UIImage(named: "ZeusSurveyMarkerTaskIcon", in: Bundle(for: ZsurveysGeofenceLocationMapView.self), compatibleWith: nil)
annotationV.canShowCallout = true
annotationV.isSelected = true
return annotationV
}