我正在使用CLLocation获取购物中心的当前楼层。
private var currentLocation = CLLocation() {
didSet {
locationLabel.text = "Longitude = \(currentLocation.coordinate.longitude) \nLatitude = \(currentLocation.coordinate.latitude)"
if let level = currentLocation.floor?.level {
floorLabel.text = "Floor = \(level)"
} else {
floorLabel.text = "No floor detected"
}
}
}
extension ViewController: CLLocationmanagerDelegate {
func locationmanager(_ manager: CLLocationmanager,didUpdateLocations locations: [CLLocation]) {
if let currentLocation = locations.first {
self.currentLocation = currentLocation
}
}
func locationmanager(_ manager: CLLocationmanager,didFailWithError error: Error) {
print("locationmanager didFailWithError: \(error.localizedDescription)")
}
}
现在,当我运行此代码时,在加载Google地图之前,它可以正常工作。这是代码。
@IBaction func mapInitClicked(_ sender: Any) {
let mapView = GMSMapView(frame: mapContainerView.bounds)
mapView.autoresizesSubviews = true
mapView.autoresizingMask = [.flexibleHeight,.flexibleWidth,.flexibleTopMargin,.flexibleBottomMargin,.flexibleLeftMargin,.flexibleRightMargin]
mapView.settings.compassButton = true
mapView.settings.indoorPicker = false
mapView.isIndoorEnabled = false
mapView.settings.myLocationButton = true
mapView.isBuildingsEnabled = false
//mapView.isMyLocationEnabled = true
//floorLevel = mapView.indoorDisplay.activeLevel?.shortName
if currentLocation.coordinate.latitude == 0.0
{
let newCamera = GMSCameraPosition.camera(withLatitude: 40.7139018,longitude: -74.0156599,zoom: 19.5)
mapView.camera = newCamera
}
else
{
let newCamera = GMSCameraPosition.camera(withLatitude: currentLocation.coordinate.latitude,longitude: currentLocation.coordinate.longitude,zoom: 1.0)
mapView.camera = newCamera
}
mapContainerView.addSubview(mapView)
}
一旦我加载了Google Map,我就开始从CLLocation获取Nil。接下来的代码行开始执行。
floorLabel.text = "No floor detected"
有人知道它出了什么问题吗?