出错,我一整天都在搜索如何使用Corelocation和GoogleMaps。当我在代码的GMScameraposition部分出现错误时,我已经知道了。基本上我试图在加载视图时获取我的位置,这就是为什么大多数都在viewdidload()中。
<div class ="form-hide">
答案 0 :(得分:0)
在您拥有更新位置以填充变量之前,您的视图已加载,因此正如您在评论中所述,您的long和lat变量没有任何价值。
我在这里为你写了固定的viewDidLoad和didUpdateLocations函数:
override func viewDidLoad() {
super.viewDidLoad()
locationManager = CLLocationManager()
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestAlwaysAuthorization()
locationManager.startUpdatingLocation()
let camera = GMSCameraPosition.cameraWithLatitude(0,
longitude: 0, zoom:0) //this can be set to wherever you want your view to start at
//UPDATE: changed the initializing frame to be equal to the view, instead of CGRectZero
let mapView = GMSMapView.mapWithFrame(self.view.frame, camera:camera)
let marker = GMSMarker()
marker.position = camera.target
marker.snippet = "Hello World"
marker.appearAnimation = kGMSMarkerAnimationPop
marker.map = mapView
self.view = mapView
}
//updated! This will tell your camera to snap to your latest location once you've received it.
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
currentLocation = locations.last
mapView.camera = GMSCameraPosition(target: currentLocation.coordinate, zoom: 6, bearing: 0, viewingAngle: 0)
}
}
请告诉我这是否适合您。