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.
Related
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)
I have a MKMapview view where there are no of annotations. A location entered by the user is considered as current location. I want this current location to be on top of all other annotations.
Right now, the current location is getting ovwerlapped by other annotations.
I have set the current location annotation displaypriority to required in mapView(_ mapView: MKMapView, didAdd views: [MKAnnotationView]) this method.
Please let me know if there is any other way
I dont want the other annotations to disappear. I just want them overlapped with current location annotation.
I query a database for store information. I want stores to appear on screen within a certain radius around the user's current location. I want new stores to appear on screen as the user moves without the user having to manually refresh anything. What delegate method should I use to query for stores in and to place them on screen?
you just need to implement only func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? and listen location changes with CLLocation Manager.Whenever new location appear query point of interests and append to mapview by addAnnotations.After than set mapview centerCoordinate property to reveal user current position in mapview
You can get the coordinates of the point at the center of the map using centerCoordinate
var coord: CLLocationCoordinate2D = CLLocationCoordinate2DMake(map.centerCoordinate.latitude, map.centerCoordinate.longitude)
You can have a look at Tracking the User Location for MKMapView using its delegates.
For location update MKMapViewDelagate's
- mapView:didUpdateUserLocation:
To place them on screen remove the previous annotation and add new annotations fetched from your query.
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")
}
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