0
저는 Xcode와 Swift를 처음 접했고 단순히 사용자의 현재 위치를 보여주고 업데이트하는지도를 만들려고합니다. 지금까지이 있습니다MKMapView가 0 인 것 같습니다.
import UIKit
import MapKit
import CoreLocation
class ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {
@IBOutlet weak var map: MKMapView!
let locationManager = CLLocationManager()
var mapp = MKMapView.self
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
if (CLLocationManager.locationServicesEnabled())
{
self.locationManager.delegate = self
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
self.locationManager.requestWhenInUseAuthorization()
self.locationManager.startUpdatingLocation()
self.map.showsUserLocation = true
}
else
{
print("Location services are not enabled")
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func loadView() {
//let camera = GMSCameraPosition.camera(withLatitude: 36.2108, longitude: -81.6774, zoom: 15.0)
//let mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
//mapView.isMyLocationEnabled = true
//view = mapView
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
{
let location = locations.last
let center = CLLocationCoordinate2D(latitude: location!.coordinate.latitude, longitude: location!.coordinate.longitude)
let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.002, longitudeDelta: 0.002))
self.map.setRegion(region, animated: true)
self.locationManager.stopUpdatingLocation()
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error)
{
print("Errors: " + error.localizedDescription)
}
}
나는 스토리 보드에서지도보기를 만든 및 Ctrl 파일로 드래그 (따라서 @IBOutlet 약한 VAR지도! MKMapView). 그러나 휴대 전화를 연결하고 응용 프로그램을 시뮬레이션 할 때 "선택 값을 언 래핑하는 동안 예기치 않게 발견 된 없음"을 제공하고 일부 인쇄 문을 실행 한 후에지도 변수를 참조 할 때마다 문제가 발생한다는 것을 알았습니다. 왜 이런거야? 감사! 원래 게시물의 코멘트에서
감사? 지도가 있어야합니다 (기본적으로 꺼져 있음). – lithium
'loadView' 메소드를 제거하십시오. 프로그래밍 방식으로 뷰를 만들 때만 사용됩니다. 스토리 보드 (또는 NIB)를 사용할 때'loadView'가 아닌'viewDidLoad'에 설정 코드를 넣습니다. – Rob
@Rob이 맞습니다. 'loadView'를 제거하면 제대로 작동합니다. –