Manos Chorianopoulos

Add MapViewController

......@@ -5,25 +5,143 @@
// Created by Manos Chorianopoulos on 7/2/24.
//
import UIKit
import MapKit
@objc public class MapViewController: UIViewController, MKMapViewDelegate {
@IBOutlet private var mapView: MKMapView!
var merchantsArray:Array<swiftApi.MerchantModel> = []
@objc public class MapViewController: UIViewController {
var loading: Bool = false
let initialLocation = CLLocation(latitude: 37.9641262, longitude: 23.7468592) // greece
public override func viewDidLoad() {
super.viewDidLoad()
// self.hidesBottomBarWhenPushed = true
mapView.delegate = self
mapView.centerToLocation(initialLocation, regionRadius: 1000000)
load()
}
public override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
// self.navigationController?.hideHairline()
}
@objc func load() {
if (loading) {
return;
}
showLoading()
self.merchantsArray = generateRandomMerchantsInGreece()
self.loadMapPins()
showContent()
}
func generateRandomMerchantsInGreece() -> Array<swiftApi.MerchantModel> {
// Define the approximate bounding box coordinates for Greece
let greeceBoundingBox = MKCoordinateRegion(
center: CLLocationCoordinate2D(latitude: 38.5, longitude: 23.0),
span: MKCoordinateSpan(latitudeDelta: 2.0, longitudeDelta: 3.0)
)
// Generate 10 random merchants within Greece's bounding box
var randomMerchants: Array<swiftApi.MerchantModel> = []
for _ in 1...10 {
let randomLatitude = Double.random(in: greeceBoundingBox.center.latitude - greeceBoundingBox.span.latitudeDelta / 2 ... greeceBoundingBox.center.latitude + greeceBoundingBox.span.latitudeDelta / 2)
let randomLongitude = Double.random(in: greeceBoundingBox.center.longitude - greeceBoundingBox.span.longitudeDelta / 2 ... greeceBoundingBox.center.longitude + greeceBoundingBox.span.longitudeDelta / 2)
let randomMerchant = swiftApi.MerchantModel()
randomMerchant._latitude = randomLatitude
randomMerchant._longitude = randomLongitude
randomMerchants.append(randomMerchant)
}
return randomMerchants
}
// Do any additional setup after loading the view.
private func showLoading() {
loading = true
}
private func showContent() {
loading = false
}
private func loadMapPins() {
for item in merchantsArray {
let pin = MerchantAnnotation(item)
if let annotationView = mapView.view(for: pin) {
annotationView.isEnabled = false
}
mapView.addAnnotation(pin)
}
}
// map view delegate
public func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
/*
// MARK: - Navigation
// guard !(annotation is MKUserLocation) else {
// return nil
// }
// 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.destination.
// Pass the selected object to the new view controller.
var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: "warply_custom")
if (annotationView == nil) {
annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "warply_custom")
annotationView?.canShowCallout = false
} else {
annotationView?.annotation = annotation
}
// custom pin image
let customImageSize = CGSize(width: 60, height: 50)
annotationView?.image = UIImage(named: "map_marker", in: MyEmptyClass.resourceBundle(), compatibleWith: nil)
annotationView?.frame = CGRect(x: 0, y: 0, width: customImageSize.width, height: customImageSize.height)
// Add shadow
annotationView?.layer.shadowColor = UIColor(red: 0.00, green: 0.00, blue: 0.00, alpha: 0.16).cgColor
annotationView?.layer.shadowOffset = CGSize(width: 0.0, height: 3.0)
annotationView?.layer.shadowOpacity = 1.0
annotationView?.layer.shadowRadius = 6.0
return annotationView
}
*/
public func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
// Disable the default behavior of selecting and zooming in on the pin
mapView.deselectAnnotation(view.annotation, animated: false)
}
public func mapView(_ mapView: MKMapView, didDeselect view: MKAnnotationView) {
}
}
private extension MKMapView {
func centerToLocation(_ location: CLLocation, regionRadius: CLLocationDistance = 1000) {
let coordinateRegion = MKCoordinateRegion(
center: location.coordinate,
latitudinalMeters: regionRadius,
longitudinalMeters: regionRadius)
setRegion(coordinateRegion, animated: true)
}
}
......