ios - 构建 map 应用程序 ios 时出现 fatal error

标签 ios xcode swift

出现错误,我一整天都在搜索如何将 Corelocation 与 GoogleMaps 一起使用。当我在代码的 GMScameraposition 部分出现错误时,我以为我已经有了它。基本上我试图在加载 View 时获取我的位置,这就是为什么大多数位置都在 viewdidload() 内。

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;
        }


    }

最佳答案

您的 View 会在更新位置来填充变量之前加载,因此正如您在评论中所说,您的 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)
    }
}

请告诉我这是否适合您。

关于ios - 构建 map 应用程序 ios 时出现 fatal error ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35858335/

相关文章:

ios - 类似于 iOS 7 Safari 的类似 UIActionSheet 的控件

iphone - iOS "Open With..."ZIP 文件

swift - 动态链接 Firebase 函数根本没有被调用 Xcode 12?

swift - SKSpriteNode 非常巨大

ios - 在 UITabBarController 中推送不支持的 navController

ios - Iphone 4 和 Iphone 5 中的 UIView 问题

ios - 苹果(企业)开发者程序 : Same team agent of multiple teams seems not possible

ios - 如何禁用地铁 bundler "auto pop-up"

ios - 如何四舍五入 NSNumber 并在对象 C 中创建一系列数字

ios - 在 TextView 中显示多行