如何在 Swift 中设置初始地图位置?
How to set initial map location in Swift?
我正在尝试将地图上的初始位置设置为我从存储在数据库中的用户配置文件中获得的坐标,比如 userLocation。我在 viewDidLoad
中调用 GMSCameraPosition
,但注意到模拟器检测到当前位置并将地图居中放置在其上,而不是我尝试设置的位置。我可以短暂地看到地图位于 userLocation
上,但会立即移动到当前位置。
我什至调用了 stopUpdatingLocation
方法但没有成功。
stopUpdatingLocation
呼叫位置是否在停止前更新一次?这就是我使用断点进行跟踪时出现的情况!
GMSCameraPosition
是否也会更改基础坐标或仅关注提供的坐标?
下面是代码。
override func viewDidLoad() {
super.viewDidLoad()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters
locationManager.distanceFilter = 50
locationManager.requestWhenInUseAuthorization()
mapView.delegate = self
if userLocation != nil {
mapView.myLocationEnabled = false
locationManager.stopUpdatingLocation()
mapView.camera = GMSCameraPosition(target: selectedCoordinate!.coordinate, zoom: 15, bearing: 0, viewingAngle: 0)
}
}
extension MapViewController: CLLocationManagerDelegate {
func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
if status == .AuthorizedWhenInUse {
locationManager.startUpdatingLocation()
mapView.myLocationEnabled = true
mapView.settings.myLocationButton = true
}
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if let location = locations.first {
mapView.camera = GMSCameraPosition(target: location.coordinate, zoom: 15, bearing: 0, viewingAngle: 0)
locationManager.stopUpdatingLocation()
}
}
}
extension MapViewController: GMSMapViewDelegate {
func mapView(mapView: GMSMapView, idleAtCameraPosition position: GMSCameraPosition) {
reverseGeocodeCoordinate(position.target)
}
}
谢谢
添加:-
locationManager. stopMonitoringSignificantLocationChanges()
在 locationManager.stopUpdatingLocation()
之后
另外请注意 CLLocationManagerDelegate
是一个 asynchronously
运行 class 协议,这意味着一旦你执行行 mapView.delegate = self
,你就已经将你的委托初始化为 self
这将发送一个调用来获取 usersCurrentLocation
geoCoordinates 这意味着它将检查它的协议委托方法是否符合该特定 class ,因为如果它们是那么它将调用这些方法而不管不管你有没有打电话给 stopUpdatingLocation()
.
所以解决方案:-
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if userLocation != nil {
locationManager.stopUpdatingLocation()
mapView.camera = GMSCameraPosition(target: selectedCoordinate!.coordinate, zoom: 15, bearing: 0, viewingAngle: 0)
} else if let location = locations.first {
mapView.camera = GMSCameraPosition(target: location.coordinate, zoom: 15, bearing: 0, viewingAngle: 0)
locationManager.stopUpdatingLocation()
}
}
我正在尝试将地图上的初始位置设置为我从存储在数据库中的用户配置文件中获得的坐标,比如 userLocation。我在 viewDidLoad
中调用 GMSCameraPosition
,但注意到模拟器检测到当前位置并将地图居中放置在其上,而不是我尝试设置的位置。我可以短暂地看到地图位于 userLocation
上,但会立即移动到当前位置。
我什至调用了 stopUpdatingLocation
方法但没有成功。
stopUpdatingLocation
呼叫位置是否在停止前更新一次?这就是我使用断点进行跟踪时出现的情况!
GMSCameraPosition
是否也会更改基础坐标或仅关注提供的坐标?
下面是代码。
override func viewDidLoad() {
super.viewDidLoad()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters
locationManager.distanceFilter = 50
locationManager.requestWhenInUseAuthorization()
mapView.delegate = self
if userLocation != nil {
mapView.myLocationEnabled = false
locationManager.stopUpdatingLocation()
mapView.camera = GMSCameraPosition(target: selectedCoordinate!.coordinate, zoom: 15, bearing: 0, viewingAngle: 0)
}
}
extension MapViewController: CLLocationManagerDelegate {
func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
if status == .AuthorizedWhenInUse {
locationManager.startUpdatingLocation()
mapView.myLocationEnabled = true
mapView.settings.myLocationButton = true
}
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if let location = locations.first {
mapView.camera = GMSCameraPosition(target: location.coordinate, zoom: 15, bearing: 0, viewingAngle: 0)
locationManager.stopUpdatingLocation()
}
}
}
extension MapViewController: GMSMapViewDelegate {
func mapView(mapView: GMSMapView, idleAtCameraPosition position: GMSCameraPosition) {
reverseGeocodeCoordinate(position.target)
}
}
谢谢
添加:-
locationManager. stopMonitoringSignificantLocationChanges()
在 locationManager.stopUpdatingLocation()
另外请注意 CLLocationManagerDelegate
是一个 asynchronously
运行 class 协议,这意味着一旦你执行行 mapView.delegate = self
,你就已经将你的委托初始化为 self
这将发送一个调用来获取 usersCurrentLocation
geoCoordinates 这意味着它将检查它的协议委托方法是否符合该特定 class ,因为如果它们是那么它将调用这些方法而不管不管你有没有打电话给 stopUpdatingLocation()
.
所以解决方案:-
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if userLocation != nil {
locationManager.stopUpdatingLocation()
mapView.camera = GMSCameraPosition(target: selectedCoordinate!.coordinate, zoom: 15, bearing: 0, viewingAngle: 0)
} else if let location = locations.first {
mapView.camera = GMSCameraPosition(target: location.coordinate, zoom: 15, bearing: 0, viewingAngle: 0)
locationManager.stopUpdatingLocation()
}
}