稱它爲我在編程很新,這是我的第一個應用程序,很抱歉,如果該方法是非常寒酸。斯威夫特3 - 存儲用戶的位置,並從不同的視圖控制器
我創建了一個輔助方法來獲取用戶的位置,因爲我需要,所以我認爲這是做一個更清潔的方式,從不同的視圖控制器調用它。但我不知道爲什麼現在不工作(沒有錯誤,它只是顯示了歐洲的一般看法)。但是當它在視圖控制器內時,它工作得很好。
我從我在做的過程中這種新的方法,我已經在許多來源一直在研究。我也檢查了this question,但我還沒有找到任何解決方案。
這是我在GMSClient文件中創建的方法。它會獲取用戶的位置,但如果用戶禁用此選項,它會顯示(在柏林中心)的默認位置:
extension GMSClient: CLLocationManagerDelegate {
//MARK: Initial Location: Berlin
func setDefaultInitialLocation(_ map: GMSMapView) {
let camera = GMSCameraPosition.camera(withLatitude: 52.520736, longitude: 13.409423, zoom: 8)
map.camera = camera
let initialLocation = CLLocationCoordinate2DMake(52.520736, 13.409423)
let marker = GMSMarker(position: initialLocation)
marker.title = "Berlin"
marker.map = map
}
//MARK: Get user location
func getUserLocation(_ map: GMSMapView,_ locationManager: CLLocationManager) {
var userLocation: String?
locationManager.requestWhenInUseAuthorization()
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
if status == .authorizedWhenInUse {
locationManager.startUpdatingLocation()
map.isMyLocationEnabled = true
map.settings.myLocationButton = true
} else {
setDefaultInitialLocation(map)
}
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if let location = locations.first {
map.camera = GMSCameraPosition(target: location.coordinate, zoom: 15, bearing: 0, viewingAngle: 0)
locationManager.stopUpdatingLocation()
//Store User Location
userLocation = "\(location.coordinate.latitude), \(location.coordinate.longitude)"
print("userLocation is: \((userLocation) ?? "No user Location")")
}
}
}
}
這個文件也是這個singelton:
// MARK: Shared Instance
class func sharedInstance() -> GMSClient {
struct Singleton {
static var sharedInstance = GMSClient()
}
return Singleton.sharedInstance
}
然後我在我的視圖控制器中這樣稱呼它:
class MapViewController: UIViewController, CLLocationManagerDelegate {
// MARK: Outlets
@IBOutlet weak var mapView: GMSMapView!
// MARK: Properties
let locationManager = CLLocationManager()
var userLocation: String?
let locationManagerDelegate = GMSClient()
// MARK: Life Cycle
override func viewDidLoad() {
super.viewDidLoad()
self.locationManager.delegate = locationManagerDelegate
GMSClient.sharedInstance().getUserLocation(mapView, locationManager)
}
任何人有什麼想法可能是錯的?
謝謝!
你嵌套你'getUserLocation'函數內部的委託功能。這不起作用。將它們移出。 – Paulw11
我之前曾嘗試過並且確實奏效,但我想稍後創建一個完成處理程序,以獲取Google地圖Web搜索請求的用戶位置。就像...'func getUserLocation(_ map:GMSMapView,_ locationManager:CLLocationManager,completionHandlerForUserLocation:@escaping(_ userLocation:String ?, _error:NSError?) - > Void)){'所以我不知道如何使用委託方法... – celiux
說實話,完成處理程序可能不是這裏正確的模式。你可以使用NSNotification,或者你可以設置一個委託或者在一個屬性中存儲一個閉包。我可能會使用NSNotification,因爲它允許您輕鬆通知多個觀察者 – Paulw11