-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTravelLocationsMapViewController.swift
73 lines (57 loc) · 2.44 KB
/
TravelLocationsMapViewController.swift
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
//
// TravelLocationsMapViewController.swift
// Virtual Tourist
//
// Created by Brahma Reddy Chilakala on 13/04/17.
// Copyright © 2017 Brahma Reddy Chilakala. All rights reserved.
//
import UIKit
import MapKit
import CoreLocation
class TravelLocationsMapViewController: UIViewController {
@IBOutlet weak var mapView: MKMapView!
var manager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
let uilpgr = UILongPressGestureRecognizer(target: self, action: #selector(TravelLocationsMapViewController.longpress(gestureRecognizer:)))
uilpgr.minimumPressDuration = 2
mapView.addGestureRecognizer(uilpgr)
}
func longpress(gestureRecognizer: UIGestureRecognizer) {
if gestureRecognizer.state == UIGestureRecognizerState.began {
let touchPoint = gestureRecognizer.location(in: self.mapView)
let newCoordinate = self.mapView.convert(touchPoint, toCoordinateFrom: self.mapView)
let location = CLLocation(latitude: newCoordinate.latitude, longitude: newCoordinate.longitude)
var title = ""
CLGeocoder().reverseGeocodeLocation(location, completionHandler: { (placemarks, error) in
if error != nil {
print(error)
} else {
if let placemark = placemarks?[0] {
if placemark.subThoroughfare != nil {
title += placemark.subThoroughfare! + " "
}
if placemark.thoroughfare != nil {
title += placemark.thoroughfare!
}
}
}
if title == "" {
title = "Added \(NSDate())"
}
let annotation = MKPointAnnotation()
annotation.coordinate = newCoordinate
annotation.title = title
self.mapView.addAnnotation(annotation)
})
}
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}