GoogleMaps IOS SDK:viewMap.myLocationEnabled = true。不工作

时间:2016-04-04 21:16:57

标签: ios swift google-maps

我正在尝试获取第一个视图是用户在地图上的当前位置,但我总是在第22行死了应用。非常感谢能够帮助我的人或者其他一些方法来做到这一点< / p>

viewMap.myLocationEnabled = true

引发此错误:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIView setMyLocationEnabled:]: unrecognized selector sent to instance 0x14c5bde10'

代码如下:

import UIKit
import GoogleMaps

class WhereViewController: UIViewController, CLLocationManagerDelegate {


    @IBOutlet weak var viewMap: GMSMapView!
    var locationManager = CLLocationManager()
    var didFindMyLocation = false



    override func viewDidLoad() {
        super.viewDidLoad()

        locationManager.delegate = self
        locationManager.requestWhenInUseAuthorization()

        viewMap.addObserver(self, forKeyPath: "myLocation", options: NSKeyValueObservingOptions.New, context: nil)
        print("despues de observer")
        viewMap.myLocationEnabled = true // Line 22
    }

    func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
        if status == CLAuthorizationStatus.AuthorizedWhenInUse{
            viewMap.myLocationEnabled = true
            print("sali de locationManager")
        }
    }

    override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
        if !didFindMyLocation{
            print("dentro de observer false")
            let myLocation: CLLocation = change![NSKeyValueChangeNewKey] as! CLLocation
            viewMap.camera = GMSCameraPosition.cameraWithTarget(myLocation.coordinate, zoom: 10.0)
            viewMap.settings.myLocationButton = true

            didFindMyLocation = true
        }
        print("dentro de observer false")
    }


}

1 个答案:

答案 0 :(得分:0)

您应该在Interface Builder中将视图的类类型设置为GMSMapView(在Identity Inspector中设置视图的自定义类)。目前它是UIView,你发送消息setMyLocationEnabled:无法识别。