构建地图应用程序ios时出现致命错误
遇到一个错误,我整天都在搜索如何使用Corelocation和GoogleMaps。当我在代码的GMScameraposition部分出错时,我想我已经有了它。基本上,我试图在加载视图时获取我的位置,这就是为什么大多数都在viewdidload()中构建地图应用程序ios时出现致命错误,ios,xcode,swift,Ios,Xcode,Swift,遇到一个错误,我整天都在搜索如何使用Corelocation和GoogleMaps。当我在代码的GMScameraposition部分出错时,我想我已经有了它。基本上,我试图在加载视图时获取我的位置,这就是为什么大多数都在viewdidload()中 视图在更新位置以填充变量之前加载,所以正如您在评论中所说,long和lat变量没有值 我在这里为您编写了fixed viewDidLoad和didUpdateLocations函数: override func viewDidLoad()
视图在更新位置以填充变量之前加载,所以正如您在评论中所说,long和lat变量没有值 我在这里为您编写了fixed 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)
}
}
让我知道这是否适合您。您遇到了什么错误?请不要让我们猜。很抱歉,我没有提到这件事。“exc_bad_指令(code=exc_i386_invop subcode=0x0)”这是我尝试构建时出现的情况。我进一步了解了错误,我认为是“long”和“lat”变量没有值,然后被传递到gmscameraposition“self.view=mapView”行,给出了“使用未解析标识符‘mapView’”。我试着将它移到下面的locationManager函数中,它不知怎么消失了,但仍然没有显示地图。因为mapView是通过编程方式创建的,所以我们必须设置默认的相机视图,并在设置lat和long后更新相机。我已经更新了我的帖子以反映这些更改。我认为我们需要将mapView设置为全局变量,因为我遇到了与上次相同的错误,但出现在func locationManager中的“mapView.camera”行上。没错,请继续为mapView添加一个全局变量,看看它是否有效。好的,我尝试了,没有错误,这是一个加号,但是没有地图出现。
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)
}
}