我有一個大規模的視圖控制器,並試圖將我的代碼分成不同的類。 我創建了一個類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