14.1. Phase 1: Displaying Map View and Current Location
Setting up the Map View
//
// MapView.swift
// App14
//
// Created by Sakib Miazi on 6/14/23.
//
import UIKit
import MapKit
class MapView: UIView {
var mapView:MKMapView!
var buttonLoading:UIButton!
var buttonCurrentLocation:UIButton!
override init(frame: CGRect) {
super.init(frame: frame)
backgroundColor = .white
setupMapView()
setupButtonLoading()
setupButtonCurrentLocation()
initConstraints()
}
func setupMapView(){
mapView = MKMapView()
mapView.translatesAutoresizingMaskIntoConstraints = false
mapView.layer.cornerRadius = 10
self.addSubview(mapView)
}
func setupButtonLoading(){
buttonLoading = UIButton(type: .system)
buttonLoading.setTitle(" Fetching Location... ", for: .normal)
buttonLoading.titleLabel?.font = UIFont.boldSystemFont(ofSize: 20)
buttonLoading.setImage(UIImage(systemName: "circle.dotted"), for: .normal)
buttonLoading.layer.backgroundColor = UIColor.black.cgColor
buttonLoading.tintColor = .white
buttonLoading.layer.cornerRadius = 10
buttonLoading.layer.shadowOffset = .zero
buttonLoading.layer.shadowRadius = 4
buttonLoading.layer.shadowOpacity = 0.7
buttonLoading.translatesAutoresizingMaskIntoConstraints = false
buttonLoading.isEnabled = false
self.addSubview(buttonLoading)
}
func setupButtonCurrentLocation(){
buttonCurrentLocation = UIButton(type: .system)
buttonCurrentLocation.setImage(UIImage(systemName: "location.circle"), for: .normal)
buttonCurrentLocation.layer.backgroundColor = UIColor.lightGray.cgColor
buttonCurrentLocation.tintColor = .blue
buttonCurrentLocation.layer.cornerRadius = 10
buttonCurrentLocation.layer.shadowOffset = .zero
buttonCurrentLocation.layer.shadowRadius = 4
buttonCurrentLocation.layer.shadowOpacity = 0.7
buttonCurrentLocation.translatesAutoresizingMaskIntoConstraints = false
self.addSubview(buttonCurrentLocation)
}
func initConstraints(){
NSLayoutConstraint.activate([
mapView.centerXAnchor.constraint(equalTo: self.safeAreaLayoutGuide.centerXAnchor),
mapView.centerYAnchor.constraint(equalTo: self.safeAreaLayoutGuide.centerYAnchor),
mapView.widthAnchor.constraint(equalTo: self.safeAreaLayoutGuide.widthAnchor, multiplier: 0.95),
mapView.heightAnchor.constraint(equalTo: self.safeAreaLayoutGuide.heightAnchor, multiplier: 0.95),
buttonLoading.centerXAnchor.constraint(equalTo: mapView.centerXAnchor),
buttonLoading.centerYAnchor.constraint(equalTo: mapView.centerYAnchor),
buttonLoading.widthAnchor.constraint(equalToConstant: 240),
buttonLoading.heightAnchor.constraint(equalToConstant: 40),
buttonCurrentLocation.trailingAnchor.constraint(equalTo: mapView.trailingAnchor, constant: -16),
buttonCurrentLocation.bottomAnchor.constraint(equalTo: self.mapView.bottomAnchor, constant: -8),
buttonCurrentLocation.heightAnchor.constraint(equalToConstant: 36),
buttonCurrentLocation.widthAnchor.constraint(equalToConstant: 36)
])
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}Patching the View with the Controller

Location Manager
Action when the Current Location button is tapped
Extending MKMapView to center the view to the current location
Setting up Info.plist to allow the location access

Setting the Simulator/Emulator location


Code so far
Previous14. UIMapKit: Working with Location and MapsNext14.2. Phase 2: Annotations and Accessories for a certain place
Last updated

