2016-03-08 66 views
0

出現錯誤,我一直在搜索如何將Corelocation和GoogleMaps一起使用。當我在代碼的GMScameraposition部分發生錯誤時,我以爲我已經擁有它了。基本上我試圖讓我的位置加載視圖這就是爲什麼大多數在viewdidload()。構建地圖應用程序致命錯誤ios

import UIKit 
    import GoogleMaps 
    import CoreLocation 

    class ViewController: UIViewController, CLLocationManagerDelegate { 


     var locationManager: CLLocationManager! 

     var currentLocation: CLLocation! 
     var long: Float64! 
     var lat: Float64! 

     override func viewDidLoad() { 
      super.viewDidLoad() 
      // Do any additional setup after loading the view, typically from a nib. 


      locationManager = CLLocationManager() 
      locationManager.delegate = self; 
      locationManager.desiredAccuracy = kCLLocationAccuracyBest 
      locationManager.requestAlwaysAuthorization() 
      locationManager.startUpdatingLocation() 

      let camera = GMSCameraPosition.cameraWithLatitude(lat, 
       longitude: long, zoom:6) 
      let mapView = GMSMapView.mapWithFrame(CGRectZero, camera:camera) 

      let marker = GMSMarker() 
      marker.position = camera.target 
      marker.snippet = "Hello World" 
      marker.appearAnimation = kGMSMarkerAnimationPop 
      marker.map = mapView 

      self.view = mapView 
     } 

     override func didReceiveMemoryWarning() { 
      super.didReceiveMemoryWarning() 
      // Dispose of any resources that can be recreated. 
     } 
     func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { 
      currentLocation = locations.last! as CLLocation 
      long = currentLocation.coordinate.longitude; 
      lat = currentLocation.coordinate.latitude; 
     } 


    } 
+0

你得到了什麼錯誤?請不要讓我們猜測。 – Jonah

+0

對不起,不提。 「exc_bad_instruction(code = exc_i386_invop subcode = 0x0)」這是當我嘗試構建 –

+0

時出現在我看來進一步陷入錯誤,我認爲它的「長」和「拉特」變量沒有一個值,然後被傳遞給GM​​SCameraPosition –

回答

0

你的觀點負載你有一個更新的位置填寫您的變量之前,從而爲您在您的評論說,這是,你的長和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) 
    } 
} 

讓我知道這是否爲你。

+0

「self.view = mapView」行給出「使用未解析的標識符'mapView'」。我嘗試將其移動到locationManager函數中,並且它以某種方式消失,但仍然沒有顯示地圖 –

+0

啊,因爲mapView是以編程方式創建的,所以我們必須設置默認相機視圖並在相機更新一次後更新相機已設置。我已更新我的帖子以反映這些更改。 –

+0

我認爲我們需要將mapView設置爲一個全局變量,因爲我得到的錯誤與上次相同,但在func locationManager的「mapView.camera」行中。 –

相關問題