blob: 79199aeeda65f591b5f2753009d569ebcb3ef693 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
import Mapbox
@objc(PointConversionExample_Swift)
class PointConversionExample_Swift: UIViewController, MGLMapViewDelegate {
var mapView: MGLMapView!
override func viewDidLoad() {
super.viewDidLoad()
mapView = MGLMapView(frame: view.bounds)
mapView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
view.addSubview(mapView)
// double tapping zooms the map, so ensure that can still happen
let doubleTap = UITapGestureRecognizer(target: self, action: nil)
doubleTap.numberOfTapsRequired = 2
mapView.addGestureRecognizer(doubleTap)
// delay single tap recognition until it is clearly not a double
let singleTap = UITapGestureRecognizer(target: self, action: #selector(handleSingleTap))
singleTap.require(toFail: doubleTap)
mapView.addGestureRecognizer(singleTap)
// convert `mapView.centerCoordinate` (CLLocationCoordinate2D)
// to screen location (CGPoint)
let centerScreenPoint: CGPoint = mapView.convert(mapView.centerCoordinate, toPointTo: mapView)
print("Screen center: \(centerScreenPoint) = \(mapView.center)")
}
func handleSingleTap(tap: UITapGestureRecognizer) {
// convert tap location (CGPoint)
// to geographic coordinates (CLLocationCoordinate2D)
let location: CLLocationCoordinate2D = mapView.convert(tap.location(in: mapView), toCoordinateFrom: mapView)
print("You tapped at: \(location.latitude), \(location.longitude)")
// create an array of coordinates for our polyline
var coordinates: [CLLocationCoordinate2D] = [mapView.centerCoordinate, location]
// remove existing polyline from the map, (re)add polyline with coordinates
if (mapView.annotations?.count != nil) {
mapView.removeAnnotations(mapView.annotations!)
}
let polyline = MGLPolyline(coordinates: &coordinates, count: UInt(coordinates.count))
mapView.addAnnotation(polyline)
}
}
|