2016-08-20 118 views
1

我想在地图上设置初始位置,我从用户配置文件中获取的坐标存储在数据库中,例如userLocation。我打电话GMSCameraPositionviewDidLoad,但已经注意到,模拟器检测当前位置并将地图放在它的中心,而不是我试图设置的位置。我可以短暂地看到位于userLocation上的地图,但立即移动到当前位置。如何在Swift中设置初始地图位置?

我甚至打电话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) 

    } 
} 

感谢

回答

1

地址: -

locationManager. stopMonitoringSignificantLocationChanges()locationManager.stopUpdatingLocation()

而且心说CLLocationManagerDelegateasynchronously运行类的协议,这意味着一旦你执行线mapView.delegate = self,您已初始化您的委托self这将发送电话获取usersCurrentLocation地理坐标,这意味着它将检查它是否是协议Deleg吃的方法符合那个特定的类,因为如果他们那么它会调用这些方法,不管你是否打电话给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() 
    } 

} 
+0

感谢。我用一个标志来实现,但你的答案更清晰。 – ashishn

+0

我正面临另一个问题。地图随机打开<+ 51.17889992,-1.82639994>坐标。下面是一些打印语句输出 MapViewCoontroller - idleAtCameraPosition - 位置:CLLocationCoordinate2D(纬度:51.17889991879489,经度:-1.8263999372720716) MapViewCoontroller - didUpdateLocations - 位置选自:可选(<+ 51.17889992,-1.82639994> MapViewCoontroller - didUpdateLocations - 位置选择:可选(<+ 51.17889992,-1.82639994> – ashishn

+0

不是,它们是<+ 18.49655889,+ 73.81614894> – ashishn

相关问题