Manos Chorianopoulos

Add MapViewController

...@@ -5,25 +5,143 @@ ...@@ -5,25 +5,143 @@
5 // Created by Manos Chorianopoulos on 7/2/24. 5 // Created by Manos Chorianopoulos on 7/2/24.
6 // 6 //
7 7
8 +
8 import UIKit 9 import UIKit
10 +import MapKit
11 +
12 +@objc public class MapViewController: UIViewController, MKMapViewDelegate {
13 + @IBOutlet private var mapView: MKMapView!
14 +
15 + var merchantsArray:Array<swiftApi.MerchantModel> = []
16 +
9 17
10 -@objc public class MapViewController: UIViewController { 18 + var loading: Bool = false
19 + let initialLocation = CLLocation(latitude: 37.9641262, longitude: 23.7468592) // greece
11 20
12 public override func viewDidLoad() { 21 public override func viewDidLoad() {
13 super.viewDidLoad() 22 super.viewDidLoad()
23 +
24 +// self.hidesBottomBarWhenPushed = true
25 +
26 + mapView.delegate = self
27 + mapView.centerToLocation(initialLocation, regionRadius: 1000000)
28 +
29 + load()
30 + }
31 +
32 + public override func viewWillAppear(_ animated: Bool) {
33 + super.viewWillAppear(animated)
34 +
35 +// self.navigationController?.hideHairline()
36 + }
37 +
38 + @objc func load() {
39 +
40 + if (loading) {
41 + return;
42 + }
43 + showLoading()
44 +
45 + self.merchantsArray = generateRandomMerchantsInGreece()
46 + self.loadMapPins()
47 + showContent()
48 + }
49 +
50 + func generateRandomMerchantsInGreece() -> Array<swiftApi.MerchantModel> {
51 + // Define the approximate bounding box coordinates for Greece
52 + let greeceBoundingBox = MKCoordinateRegion(
53 + center: CLLocationCoordinate2D(latitude: 38.5, longitude: 23.0),
54 + span: MKCoordinateSpan(latitudeDelta: 2.0, longitudeDelta: 3.0)
55 + )
56 +
57 + // Generate 10 random merchants within Greece's bounding box
58 + var randomMerchants: Array<swiftApi.MerchantModel> = []
59 + for _ in 1...10 {
60 + let randomLatitude = Double.random(in: greeceBoundingBox.center.latitude - greeceBoundingBox.span.latitudeDelta / 2 ... greeceBoundingBox.center.latitude + greeceBoundingBox.span.latitudeDelta / 2)
61 + let randomLongitude = Double.random(in: greeceBoundingBox.center.longitude - greeceBoundingBox.span.longitudeDelta / 2 ... greeceBoundingBox.center.longitude + greeceBoundingBox.span.longitudeDelta / 2)
62 +
63 + let randomMerchant = swiftApi.MerchantModel()
64 + randomMerchant._latitude = randomLatitude
65 + randomMerchant._longitude = randomLongitude
66 +
67 + randomMerchants.append(randomMerchant)
68 + }
69 +
70 + return randomMerchants
71 + }
72 +
14 73
15 - // Do any additional setup after loading the view. 74 + private func showLoading() {
75 +
76 + loading = true
16 } 77 }
17 78
79 + private func showContent() {
80 +
81 + loading = false
82 + }
83 +
84 + private func loadMapPins() {
85 + for item in merchantsArray {
86 + let pin = MerchantAnnotation(item)
87 +
88 + if let annotationView = mapView.view(for: pin) {
89 + annotationView.isEnabled = false
90 + }
91 + mapView.addAnnotation(pin)
92 +
93 + }
94 + }
95 +
96 + // map view delegate
97 + public func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
18 98
19 - /* 99 +// guard !(annotation is MKUserLocation) else {
20 - // MARK: - Navigation 100 +// return nil
101 +// }
21 102
22 - // In a storyboard-based application, you will often want to do a little preparation before navigation 103 + var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: "warply_custom")
23 - override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 104 + if (annotationView == nil) {
24 - // Get the new view controller using segue.destination. 105 + annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "warply_custom")
25 - // Pass the selected object to the new view controller. 106 + annotationView?.canShowCallout = false
107 + } else {
108 + annotationView?.annotation = annotation
109 + }
110 +
111 + // custom pin image
112 + let customImageSize = CGSize(width: 60, height: 50)
113 +
114 + annotationView?.image = UIImage(named: "map_marker", in: MyEmptyClass.resourceBundle(), compatibleWith: nil)
115 +
116 +
117 + annotationView?.frame = CGRect(x: 0, y: 0, width: customImageSize.width, height: customImageSize.height)
118 +
119 + // Add shadow
120 + annotationView?.layer.shadowColor = UIColor(red: 0.00, green: 0.00, blue: 0.00, alpha: 0.16).cgColor
121 + annotationView?.layer.shadowOffset = CGSize(width: 0.0, height: 3.0)
122 + annotationView?.layer.shadowOpacity = 1.0
123 + annotationView?.layer.shadowRadius = 6.0
124 +
125 + return annotationView
26 } 126 }
27 - */ 127 +
128 + public func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
129 + // Disable the default behavior of selecting and zooming in on the pin
130 + mapView.deselectAnnotation(view.annotation, animated: false)
131 + }
132 +
133 + public func mapView(_ mapView: MKMapView, didDeselect view: MKAnnotationView) {
134 +
135 + }
136 +}
28 137
138 +private extension MKMapView {
139 +
140 + func centerToLocation(_ location: CLLocation, regionRadius: CLLocationDistance = 1000) {
141 + let coordinateRegion = MKCoordinateRegion(
142 + center: location.coordinate,
143 + latitudinalMeters: regionRadius,
144 + longitudinalMeters: regionRadius)
145 + setRegion(coordinateRegion, animated: true)
146 + }
29 } 147 }
......