-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLocationPickerViewController.swift
83 lines (68 loc) · 2.39 KB
/
LocationPickerViewController.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
74
75
76
77
78
79
80
81
82
83
//
// LocationPickerViewController.swift
// chatWithFBAppSample
//
// Created by anies1212 on 2022/03/30.
//
import UIKit
import CoreLocation
import MapKit
class LocationPickerViewController: UIViewController {
public var completion: ((CLLocationCoordinate2D) -> Void)?
private var isPickable = true
private let map: MKMapView = {
let map = MKMapView()
return map
}()
private var coordinates: CLLocationCoordinate2D?
init(coordinates: CLLocationCoordinate2D?) {
self.coordinates = coordinates
self.isPickable = coordinates == nil
super.init(nibName: nil, bundle: nil)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .systemBackground
if isPickable {
navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Send", style: .done, target: self, action: #selector(sendButtonTapped))
let gesture = UITapGestureRecognizer(target: self, action: #selector(didTappedMap(_:)))
gesture.numberOfTouchesRequired = 1
gesture.numberOfTapsRequired = 1
map.addGestureRecognizer(gesture)
} else {
guard let coordinates = self.coordinates else {
return
}
let pin = MKPointAnnotation()
pin.coordinate = coordinates
map.addAnnotation(pin)
}
view.addSubview(map)
map.isUserInteractionEnabled = true
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
map.frame = view.bounds
}
@objc func sendButtonTapped(){
guard let coordinates = coordinates else {
return
}
navigationController?.popViewController(animated: true)
completion?(coordinates)
}
@objc func didTappedMap(_ gesture: UITapGestureRecognizer){
let locationInView = gesture.location(in: map)
let coordinates = map.convert(locationInView, toCoordinateFrom: map)
self.coordinates = coordinates
for annotation in map.annotations{
map.removeAnnotation(annotation)
}
let pin = MKPointAnnotation()
pin.coordinate = coordinates
map.addAnnotation(pin)
}
}