How can I detect which annotation was selected in MapView
For that you can use selected Annotation
from didSelectAnnotationView
, then store that annotation to instance variable and after that used annotation in your Button
action method.
var selectedAnnotation: MKPointAnnotation?
func mapView(mapView: MKMapView, didSelectAnnotationView view: MKAnnotationView) {
self.selectedAnnotation = view.annotation as? MKPointAnnotation
}
func info(sender: UIButton) {
print(selectedAnnotation?.coordinate)
}
Edit: As of you have custom MKAnnotation
class you need to use that.
var selectedAnnotation: Islands?
func mapView(mapView: MKMapView, didSelectAnnotationView view: MKAnnotationView) {
self.selectedAnnotation = view.annotation as? Islands
}
Detect when a second annotation is selected in a MKMapView
Maybe try put a delay in your didDeselectAnnotationView method to hide your bottomView. You need to store a reference to your last selected annotation view though.
Example:
@interface MyViewController
{
MKAnnotationView *lastSelectedAnnotationView;
}
@end
@implementation MyViewController
...
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
{
...
[self updateBottomViewInfoWithAnnotationView:view];
lastSelectedAnnotationView = view;
}
- (void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKAnnotationView *)view
{
// ------------------------------------------------------------------
// perform check to hide bottomView after a delay, to give
// didSelectAnnotationView a chance to select new annotation
// ------------------------------------------------------------------
[self performSelector:@selector(checkShouldHideBottomView:) withObject:view afterDelay:0.5];
}
-(void)checkShouldHideBottomView:(MKAnnotationView *)lastDeselectedAnnotationView
{
// ----------------------------------------------------------------------
// Only hide bottom view if user did not select a new annotation or
// last selected annotation is the same as the one being deselected
// ----------------------------------------------------------------------
if(lastSelectedAnnotationView == nil || lastDeselectedAnnotationView == lastSelectedAnnotationView)
{
// hide your bottom view example
self.bottomView.alpha = 0;
// clear lastSelectedAnnotationView reference
lastSelectedAnnotationView = nil;
}
}
How could we print selected annotation to console?
You can use mapView:didSelect
method.
e.g.
func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
print("selected annotation", view.annotation)
print("selected annotation", view.annotation?.title)
print("selected annotation", view.annotation?.coordinate)
}
Don't forget to set mapView.delegate = self
in viewDidLoad
.
how to detect a callout of an annotation is showing on mapview?
Callout appears when annotation is selected - you can use mapView:didSelectAnnotationView:
method in delegate to track that event. Callout hides when annotation is deselected - use mapView:didDeselectAnnotationView:
method to track that event.
How to check if annotation is clustered (MKMarkerAnnotationView and Cluster)
In iOS 11, Apple also introduce a new callback in MKMapViewDelegate:
func mapView(_ mapView: MKMapView, clusterAnnotationForMemberAnnotations memberAnnotations: [MKAnnotation]) -> MKClusterAnnotation
Before annotations become clustered, this function will be called to request a MKClusterAnnotation
for memberAnnotations
. So the second parameter named memberAnnotations
indicates the annotations to be clustered.
EDIT (4/1/2018):
There are two key steps for cluster annotation:
First, before annotations become clustered, MapKit invoke mapView:clusterAnnotationForMemberAnnotations:
function to request a MKClusterAnnotation for memberAnnotations
.
Secondly, MapKit add the MKClusterAnnotation to MKMapView, and mapView:viewForAnnotation:
function will be called to produce a MKAnnotationView.
So you can deselect annotation in either of the two steps, like this:
var selectedAnnotation: MKAnnotation? //the selected annotation
func mapView(_ mapView: MKMapView, clusterAnnotationForMemberAnnotations memberAnnotations: [MKAnnotation]) -> MKClusterAnnotation {
for annotation in memberAnnotations {
if annotation === selectedAnnotation {
mapView.deselectAnnotation(selectedAnnotation, animated: false)//Or remove the callout
}
}
//...
}
Or:
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
if let clusterAnnotation = annotation as? MKClusterAnnotation {
for annotation in clusterAnnotation.memberAnnotations {
if annotation === selectedAnnotation {
mapView.deselectAnnotation(selectedAnnotation, animated: false)//Or remove the callout
}
}
}
//...
}
Find selected MapKit Annotation's postID
Try the following code:
func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
if let annotation = view.annotation as? EventAnnotation {
mapEventID = annotation.eventKey
}
}
how can I detect if singleTap was tapped or annotation on mapView of mapbox?
I made another workaround here
I cloned the release-ios-v3.3.0 and created package using Building the SDK and added one delegate method in MGLMapvViewDelegate.h
as per my need something like that. -(void)mapView:(MGLMapView *)mapView tapOnNonAnnotationAreaWithPoints:(CGPoint)points
and in MGLMapView.mm I updated the code like that,
else{
if(self.selectedAnnotation)
[self deselectAnnotation:self.selectedAnnotation animated:YES];
else if([self.delegate respondsToSelector:@selector(mapView:tapOnNonAnnotationAreaWithPoints:)])
[self.delegate mapView:self tapOnNonAnnotationAreaWithPoints:tapPoint];
}
which is in -(void)handleSingleTapGesture:(UITapGestureRecognizer *)singleTap
method.
Its working for me, as I am able to detect single tap on non annotation area. later I convert the passed points
to geo coordinates, so that I work with newly tapped coordinate.
Note
- Newly Created library size is about
48.7 MB
where as downloaded library was38.3 MB
. - Style is not working properly, you can see in attached pic. (Tried with different Style URL but none of it is same as previous)
- I feel lack in App performance, Zooming and Panning is not smooth as the dynamic library gave by Mapbox guys.
I am still exploring. lemme know if you wanna me to check/explore.
Please accept this answer, if it is helpful.
Thank you,
Happy Coding.
Detecting the tap which changes the selected annotation or deselects it
There is a delegate method for this. Just make sure you set the delegate
property of the map view to the containing view controller.
optional func mapView(_ mapView: MKMapView, didDeselect view:
MKAnnotationView)
This link may help you: https://developer.apple.com/documentation/mapkit/mkmapviewdelegate
Related Topics
Compiler Error When Assigning the Delegate for a Protocol in Swift iOS
How to Add a Link for a Rate Button with Swift
Show Two Different Custom Cells in Same Uitableview - Swift Firebase
Using Haneke to Cache Then Play Mp4 Files with Avplayer
Sort Alphanumeric Array, Consecutive Numbers Should Reside at Last
How to Call Secitemcopymatching in Xcode 7 Beta 4
iOS - Send Image to Instagram - Documentinteraction
How to Convert This Opengl Pointer Math to Swift
How to Use a Custom Initializer on a Uitableviewcell
Xcode 6 Project Crashing After Segue on iOS 7.1
How to Give Cornerradius for Uibezierpath
How to Use " Let Newswiftcolor = Uicolor(Red: 255, Green: 165, Blue: 0, Alpha: 0)
Pausing Spritekit Game Using Appdelegate in iOS8
How to Manage and Free Memory Through Viewcontrollers
How to Perform Realm Count Query
Cannot Convert Value of Type 'String.Type' to Expected Argument Type 'String!'