I am currently using the MapBox swift library and I am trying to add a delegate to the callout view.
I am trying to do it like this :
func mapView(_ mapView: MGLMapView, calloutViewFor annotation: MGLAnnotation) -> MGLCalloutView? {
let callout = TabletMapSearchCalloutView(representedObject: annotation, mapView: mapView)
callout.delegate = self
return callout
}
But it seems that there is an internal worker that runs in the library that sets the delegate after this method to the MGLMapView.
So my question is, how do I go about adding a delegate to the callout so that I can access the tapped event?
So it seems the MGLMapViewDelegate has the method that I was looking for
func mapView(_ mapView: MGLMapView, tapOnCalloutFor annotation: MGLAnnotation)
Related
I'm trying to get the MKAnnotationView that displays the user location (blue dot) in MapKit to add a custom gesture recognizer. Is this possible?
I've tried via the delegate method but I don't know how to dequeue the view when I don't have the identifier string. If I had the identifier I could probably do something like the code below;
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
if annotation is MKUserLocation {
let userLocationAnnotationView = mapView.dequeueReusableAnnotationView(withIdentifier: "??", for: annotation)
userLocationAnnotationView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(self.handleStuff)))
return userLocationAnnotationView
}
}
EDIT
Ah, been stuck on this for hours and found the solution directly after I posted. I found two different ways of solving it with delegate methods. The first way could be via didSelect:
func mapView(_ mapView: MKMapView,
didSelect view: MKAnnotationView){
if view.annotation is MKUserLocation {
// User clicked on the blue dot
}
}
Or if a gesture recognizer is needed it could be done as below. Should probably add a check if the gesture recognizer already have been added since this function gets called every time an annotation view is added.
func mapView(_ mapView: MKMapView,
didAdd views: [MKAnnotationView]){
for view in views {
if view.annotation is MKUserLocation {
view.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(self.handleStuff)))
}
}
}
How do I disable the "You are here" callout attached to the user location annotation in Mapbox in Swift?
You need to implement the following method which checks for the user location annotation.
func mapView(_ mapView: MGLMapView, annotationCanShowCallout annotation: MGLAnnotation) -> Bool {
if annotation is MGLUserLocation {
return false
} else {
return true
}
}
I have set up a simple app with a tabbed map view (map on tab 1) that reads info from a json file on a server (point info is stored in MySQL) with SwiftyJSON and Alamofire. The points are loaded into the map. What I would like to do is add a button to the annotation, which outlets to a different view tab and passes along the point's ID.
I have not tried anything yet as I have no idea where to get started and the documentation doesn't mention anything similar.
How does one go about adding an outlet to a map point annotation?
When you tap your pin the callout appears with a title and/or a subtitle. You need to add left and right callout accessory views to this callout. You do this by conforming to the MGLMapViewDelegate and implementing the following methods:
func mapView(_ mapView: MGLMapView, annotationCanShowCallout annotation: MGLAnnotation) -> Bool {
return true
}
func mapView(_ mapView: MGLMapView, leftCalloutAccessoryViewFor annotation: MGLAnnotation) -> UIView? {
...your code here...
let deleteButton = UIButton(type: .custom)
deleteButton.tag = 100
}
func mapView(_ mapView: MGLMapView, rightCalloutAccessoryViewFor annotation: MGLAnnotation) -> UIView? {
...your code here...
let infoButton = UIButton(type: .custom)
infoButton.tag = 101
}
func mapView(_ mapView: MGLMapView, annotation: MGLAnnotation, calloutAccessoryControlTapped control: UIControl) {
...your code here...
switch control.tag {
case 100:
// do some delete stuff
case 101:
// do some info stuff
default:
break
}
The MapBox example showing actual usage is here:
MapBox example. This example doesn't segue but you would just trigger a segue with the button tap instead of the UIAlert.
For future reference:
func mapView(_ mapView: MGLMapView, annotation: MGLAnnotation, calloutAccessoryControlTapped control: UIControl) {
// Hide the callout view.
// mapView.deselectAnnotation(annotation, animated: true)
performSegue(withIdentifier: "toDetail", sender: view)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
print("Seque toDetail")
// do stuff
}
I am trying to work on Google Map with some UIView. My requirement is : If I touch the MapView, one of UIView will disappear(animated) and it will remain disappear until I release the touch from MapView. I managed to disappear the UIView from screen on touching the MapView using the below code :
func mapView(_ mapView: GMSMapView, didLongPressAt
coordinate: CLLocationCoordinate2D) {
self.animate()
}
func mapView(_ mapView: GMSMapView,
idleAt position: GMSCameraPosition) {
self.collapse()
}
func mapView(_ mapView: GMSMapView, idleAt position: GMSCameraPosition) {
//
}
func mapView(_ mapView: GMSMapView, didTapAt coordinate: CLLocationCoordinate2D) {
On dragging map and release { Touch --> Drag --> Lift } I can detect Finger Lift using idleAt. But I couldn't detect with anything if I { Touch --> hold --> Lift }. You can consider Uber or Ola app MapView for example
You can use GoogleMaps GMSMapViewDelegate:
mapView.delegate = self
This will call when Google map start moving and idle.
func mapView(_ mapView: GMSMapView, willMove gesture: Bool) {
//
}
// Touch drag and lift
func mapView(_ mapView: GMSMapView, idleAt position: GMSCameraPosition) {
//
}
// Touch and lift
func mapView(_ mapView: GMSMapView, didTapAt coordinate: CLLocationCoordinate2D) {
print("yes")
}
I have a MapView in my app. And user can add an annotaion/pin to that MapView. I works fine but I would like to know if it is possible to add new annotation design.
This is the new pin:
This is what my map shows:
This is my annotation code:
let annotaion = MKPointAnnotation()
this line adds my annotation to map:
self.myMap.addAnnotation(self.annotaion)
Thank you.
I believe #0ndre_ was looking for the default image, not to bring in his own custom image. The answer by #Franco_Meloni was incomplete. Here is how I got it to work.
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
let pin = MKMarkerAnnotationView(annotation: annotation, reuseIdentifier: nil)
pin.canShowCallout = true
let button = UIButton(type: .detailDisclosure)
pin.rightCalloutAccessoryView = button
if annotation.isEqual(mapView.userLocation) {
return nil
}
return pin
}
The key is using MKMarkerAnnotationView to get the bigger marker. If you use MKPinAnnotationView you will see the smaller one appear.
You should implement
mapView(_ mapView: MKMapView,
viewFor annotation: MKAnnotation) -> MKAnnotationView?
from MKMapViewDelegate (https://developer.apple.com/reference/mapkit/mkmapviewdelegate/1452045-mapview)
Implementation example:
func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
let view = MKAnnotationView()
view.image = UIImage(named: "pin")
return view
}