How to perform an action with mapbox ios markers

2.3k views Asked by At

Is there way to detect on the on the screen if a user double taps on marker? or if they tap and hold the screen for a certain amount of time?

Mapbox has the below method that returns true, which signals to show information about the current place/marker. What will be the best way to check if the user clicks on this white section or double taps the current marker?

Thank you in advance!

    /* Tapping the marker */
 func mapView(mapView: MGLMapView!, annotationCanShowCallout annotation: MGLAnnotation!) -> Bool {
    return true

    /*THIS WHERE THE TAP IS BEING RECOGNIZED, 
      return true here allows the information about the current marker (name and address)*/

    }//eom

mapbox

import UIKit
import MapboxGL
import CoreLocation

class ViewController: UIViewController, MGLMapViewDelegate , CLLocationManagerDelegate {

private var currLocLatitude:CLLocationDegrees = 28.43173
private var currLocLongitude:CLLocationDegrees = -81.4704
private var currLocTitle:String = "Tommy Bahama"
private var currLocSubtitle:String = "9101 International Drive #1200, Orlando, FL 32819, United States"


var mapView:MGLMapView!
private var MapBoxAccessToken = "pk.eyJ1IjoiZGFya2ZhZGVyIiwiYSI6IlplV6hfR3MpfQ.pPEz732qS8g0WEScdItakg"

/* defining the marker from MyAnnotation.swift */
func mapView(mapView: MGLMapView!, symbolNameForAnnotation annotation: MGLAnnotation!) -> String! {
    return "secondary_marker"
}

/**/
override func viewDidLoad() {
    super.viewDidLoad()

    self.createMapBoxMap()

}//eom

/**/
override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


/*************** MapBox Methods **********/
//method creating the mapbox map
private func createMapBoxMap(){

    //type of map style
    self.mapView = MGLMapView(frame: view.bounds, accessToken: MapBoxAccessToken)

    self.mapView.autoresizingMask = .FlexibleWidth | .FlexibleHeight

    //setting the map's center coordinate
    self.mapView.setCenterCoordinate(CLLocationCoordinate2D(latitude: self.currLocLatitude, longitude: self.currLocLongitude),
        zoomLevel: 12, animated: true)
    self.view.addSubview(mapView)

    /*define the marker and its coordinates, title, and subtitle:*/
    self.mapView.delegate = self  // Set the delegate property of our map view to self after instantiating it.

    // Declare the marker `ellipse` and set its coordinates, title, and subtitle
    let ellipse = MyAnnotation(location: CLLocationCoordinate2D(latitude: self.currLocLatitude, longitude: self.currLocLongitude),
        title: self.currLocTitle, subtitle: self.currLocSubtitle)

    self.mapView.addAnnotation(ellipse) // Add marker `ellipse` to the map

    self.mapView.showsUserLocation = false //shows the current positions

}//eom


/* Tapping the marker */
func mapView(mapView: MGLMapView!, annotationCanShowCallout annotation: MGLAnnotation!) -> Bool {
    return true

    /*THIS WHERE THE TAP IS BEING RECOGNIZED, 
      return true here allows the information about the current marker (name and address)*/

    }//eom
}//eoc
1

There are 1 answers

0
jadec On

To detect the touch of the user on the white section (callout of annotation), you can use the following method :

func mapView(mapView: MGLMapView, tapOnCalloutForAnnotation annotation: MGLAnnotation) {
    print("tap on callout")
}

See mapbox documentation for more info : https://www.mapbox.com/ios-sdk/api/3.0.0/Protocols/MGLMapViewDelegate.html#//api/name/mapView:tapOnCalloutForAnnotation: