sguruapp@gmail.com
facebook vk instagram Телеграм youtube

Определить местоположение на устройстве (Determine the location on the device), Map Kit – Swift 5


Индивидуальное обучение iOS разработке начать обучение

import MapKit
import CoreLocation

class ItGuruController: UIViewController {
    
@IBOutlet weak var mapViewGURU: MKMapView!

let locationManager = CLLocationManager()
    
    
override func viewDidLoad() {
super.viewDidLoad()
setup()
}
    
    
func setup() {
self.locationManager.requestAlwaysAuthorization()
self.locationManager.requestWhenInUseAuthorization()
        
if CLLocationManager.locationServicesEnabled() {
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.startUpdatingLocation()
}
        
locationManager.delegate = self

mapViewGURU.mapType = .standard
mapViewGURU.isZoomEnabled = true
mapViewGURU.isScrollEnabled = true
        
if let coor = mapViewGURU.userLocation.location?.coordinate {
mapViewGURU.setCenter(coor, animated: true)
}
}
}


extension ItGuruController: CLLocationManagerDelegate {
    
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
        
switch status {
case .notDetermined:
print("not determined - hence ask for Permission")
manager.requestWhenInUseAuthorization()
            
            
case .restricted, .denied:
print("permission denied")
            
case .authorizedAlways, .authorizedWhenInUse:
print("Apple delegate gives the call back here once user taps Allow option, Make sure delegate is set to self")
            
default:
break
}
}
    
    
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let locValue:CLLocationCoordinate2D = manager.location!.coordinate
        
mapViewGURU.mapType = MKMapType.standard
        
let span = MKCoordinateSpan(latitudeDelta: 0.05, longitudeDelta: 0.05)
let region = MKCoordinateRegion(center: locValue, span: span)
mapViewGURU.setRegion(region, animated: true)
        
let annotation = MKPointAnnotation()
annotation.coordinate = locValue
annotation.title = "GURU Here"
mapViewGURU.addAnnotation(annotation)
}
}

Индивидуальное обучение iOS разработке программа курса


Возврат к списку

обучись профессии будущего

Хочешь стать успешным разработчиком
приложений? Тогда добро пожаловать к нам.

обучись профессии будущего