Draw route on pin touch mapkit swift - ios

I did some research and didn't find any information about this. Im working on a map with pin markers and I would like to show the route (direction) when the user touch a pin.
I already have the working code but I can't find a way to trigger this when the pin is touched (when the annotation appears), but only when touching a button inside this annotation.
Am I missing something here? a delegate function or something?
Thanks,

As always, I find my information right after I ask a question...
So for those who need the information too:
To trigger touch event when tapping on a mapkit pin, you just need to use this MKMapViewDelegate function:
func mapView(mapView: MKMapView!,
didSelectAnnotationView view: MKAnnotationView!){
println("Selected annotation")
}

Related

Mapbox (iOS) user annotations covering/preventing touch events from other annotations - use User annotations hit test

I'm having some difficulties with Mapbox iOS.
I'm adding annotations on my map, but when they are very close (actually overlap) to the user annotation (i.e MGLUserLocation) taps do not pass to the other annotations.
I have tried to play with the z-order both for the user annotation (setting it to. 0/-1 or any lower value) while increasing the value of the other annotations but with no success
func mapView(_ mapView: MGLMapView, didSelect annotation: MGLAnnotation)
Is always called with the user annotation.
I'm using a custom view for the user annotation and I tried to set its isEnable property false as well. Moreover, when I try to override the override open func tionsetSelected(_ selected: Bool, animated: Bool) In the custom view it is not called (no matter if it's isEnabled is true/false...)
It seems like the "halo" around the user location annotation make any tap to interact with the user annotation only - even if there are other objects that seems to be before it...
I was thinking as a last option to setup aUIGestureRecognizer on the relevant view that the user annotations "hides"... But am I missing something about the special user annotation in Mapbox?
I create a simple project trying to mimic as best as possible the problem in my original project which is not public - Repo
Here is an example of the capability I'm trying to achieve in Google Maps
Found the solution which I think it is utilising Mapbox API in the best way:
MGLUserLocationAnnotationView has a property called hitTestLayer: CALayer?
You can just override it and supply with the size of the clickable layer you wish to enable.... In my case I don't care for user annotation interaction so I set it to zero (I updated the code in the example repo as well)

Why does MKMapView show user's location just as a pin?

I have a mapView in which I want to show user's current location. However, I don't want to centre the mapView to that location (it is centred to other location). So, for that reason, I don't get coordinates for the user's location to set the region. I just call showsUserLocation and set it to true. Everything works fine, except the fact that this makes my mapView show user's current location just as a pin. I would like it to be in a form of a default blue dot.
Related to the problem above, I have two questions:
1) Why does it show user's current location as a pin?
2) How can I make it show user's current location as a blue dot?
If you know the answers to these questions (or only to one of them) I would appreciate your help.
Everything depends on your implementation of the map view delegate's mapView(_:viewFor:). You are probably returning a pin annotation.
You can distinguish the annotation provided by the map view as the user's location by checking its class, which will be MKUserLocation. If you want the default blue dot, return nil for that class.
In Swift 5
We need to check the type of annotation, if it is a user location do not want to return a pin:
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
if annotation is MKUserLocation {
return nil
}
[... code for the other annotations ... ]
}
Now you will see your location as a classic blue dot.

iOS mapkit - How can I show text on hover of a pin

I am using iOS 9 with Swift 2.
I have been unable to find a way of displaying text on hover of a pin that exists on a map using mapkit.
I would like to do this without having to have a callout, which forces the user to click on the pin.
Any ideas?
There isn't really a "hover" per say like with a web app. There is either tap (selected) or no tap (unselected), but terminology aside, you're going to want to use an MKAnnotationView. Specifically I'd replace it with a UILabel using this method:
func viewForAnnotation(_ annotation: MKAnnotation) -> MKAnnotationView?
That will let you set the view to be whatever you want, so create the UILabel view, then set it to be your annotation view there
Set MKAnnotarion.title property, then you can see the value on TAP. As mentioned above comment, there is no Hover concept in iOS

Swift: MKMapView annotation appears in top left corner after a didDeselectAnnotationView call

I'm drawing a MKMapView in my iOS app, and on this map are some annotations, with custom images representing them. The image changes slightly (has a glow) when a specific annotation is selected.
To do this, I'm using the mapView(mapView: MKMapView!, didSelectAnnotationView view: MKAnnotationView!) delegate method to change the image (by changing view.annotation.image). This seems to work fine.
However, when I use the mapView(mapView: MKMapView!, didDeselectAnnotationView view: MKAnnotationView!) method to change the image back after it's deselected, it redraws the annotation in the upper left corner of the map. Any user interaction with the map then redraws it in its proper place immediately, so it's like the map isn't being refreshed after the method call until a click or something is made.
Is this actually the correct way to change an annotation's image (by overwriting the current view.annotation.image, and if so is there a way to refresh the map?
EDIT: I've also just noticed every time I select a new annotation it drags the map center down by about 100 pixels. Is there something I'm missing that I need to do after calling either of these two methods mentioned above?
You could try removing and re-adding the annotation using mapView.removeAnnotation(yourAnnotation) and mapview.addAnnotation(yourAnnotation) with the new image

how to find where the user tapped on the screen in ios

I want to find where user tapped on MapKit. I cannot find a action to make a connection so i am wondering there is an alternate method for the same. please let me know.
As #Disco S2 says, add the instance of MKMapView as a subview to your view. To know where your user tapped on the map, use this method:
- (CGPoint)convertCoordinate:(CLLocationCoordinate2D)coordinate toPointToView:(UIView *)view
Put the mapkit inside a view and listen for touches in that view. There is then a method to convert the touch in the mapView to a touch in the view and vice versa. I can't remember the method for converting but it will be in the map kit documentation

Resources