【发布时间】:2017-07-08 09:33:10
【问题描述】:
我是编程新手,这是我的第一个应用程序,如果方法非常简陋,请见谅。
我创建了一个辅助方法来获取用户位置,因为我需要从不同的视图控制器调用它,所以我认为这是一种更简洁的方法。但是我不知道为什么现在不起作用(没有错误,它只是显示了欧洲的一般看法)。但是当它在视图控制器中时,它工作得非常好。
我从我正在做的课程中获得了这种新方法,并且我一直在研究许多来源。我也检查了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函数中。这行不通。把它们移走。 -
我之前尝试过并且确实有效,问题是我想稍后创建一个完成处理程序,以便获取谷歌地图网络搜索请求的用户位置。类似于...
func getUserLocation(_ map: GMSMapView,_ locationManager: CLLocationManager, completionHandlerForUserLocation: @escaping (_ userLocation: String?, _ error: NSError?) -> Void)) {所以我不知道如何使用委托方法... -
老实说,完成处理程序在这里可能不是正确的模式。你可以使用 NSNotification 或者你可以设置一个委托或者在一个属性中存储一个闭包。我可能会使用 NSNotification,因为它可以让您轻松通知多个观察者
-
哦,我明白了,我从未使用过 NSNotifications。我会查的!感谢您的回答,我找到了这个thread,它已经很老了,但我认为它可以解决我的问题。稍后我会提供更新!
标签: ios swift mapkit google-maps-sdk-ios