我有一个大规模的视图控制器,并试图将我的代码分成不同的类。 我创建了一个类CurrentLocation。在视图控制器中,我调用google maps方法animateToLocation,并得到一个EXC Bad Instruction。对适当的应用程序架构进行了大量的研究,但仍然是新的,并通过经验学习。使用谷歌地图的iOS,并试图正确实施。将更新位置放在与ViewController分开的类中是否可以接受然后只需调用我希望在ViewController中调用的方法?我在想,我已经正确实现了Model-View-Controller,可能只是继承了我不应该拥有的东西。应该是一个简单的解决方案,不知道从哪里开始。错误EXC Bad Instruction尝试从另一个类获取当前位置?
import UIKit
import GoogleMaps
class ViewController: UIViewController, CLLocationManagerDelegate, GMSMapViewDelegate {
@IBOutlet weak var googleMapView: GMSMapView!
let locationManager = CLLocationManager()
let currentLocation = CurrentLocation()
override func viewDidLoad() {
super.viewDidLoad()
currentLocation.trackLocation
}
override func viewDidAppear(animated: Bool)
{
super.viewDidAppear(animated)
if CLLocationManager.locationServicesEnabled() {
googleMapView.myLocationEnabled = true
googleMapView.animateToLocation(currentLocation.coordinate) // EXC Bad Instruction
} else
{
locationManager.requestWhenInUseAuthorization()
}
}
import Foundation
import CoreLocation
import GoogleMaps
class CurrentLocation: NSObject,CLLocationManagerDelegate {
override init()
{
super.init()
}
var locationManager = CLLocationManager()
var location : CLLocation!
var coordinate : CLLocationCoordinate2D!
var latitude : CLLocationDegrees!
var longitude : CLLocationDegrees!
func trackLocation()
{
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
{
if CLLocationManager.locationServicesEnabled() {
location = locations.last
coordinate = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude)
latitude = coordinate.latitude
longitude = coordinate.longitude
}
}
我更新了我的代码,我相信应该修复它,但在相同的位置有相同的错误? – lifewithelliott
请参阅文章:“由于这是异步发生的,因此应用程序无法立即开始使用位置服务,而必须实施locationManager:didChangeAuthorizationStatus委托方法,该方法会在任何授权状态根据用户输入而更改时触发。 – bearMountain
啊,最后通过添加一个观察者来解决问题 – lifewithelliott