Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 谷歌地图API(GMS)地图不';我没有出现_Ios_Swift_Google Maps_Gmsmapview - Fatal编程技术网

Ios 谷歌地图API(GMS)地图不';我没有出现

Ios 谷歌地图API(GMS)地图不';我没有出现,ios,swift,google-maps,gmsmapview,Ios,Swift,Google Maps,Gmsmapview,我试图让地图自动跟随用户(位置),但不知何故,应用程序崩溃(没有错误消息),或者启动应用程序时地图不显示。我做错了什么 我尝试过不使用locationManager func,但它确实有效。是否有其他方法来跟踪用户 class GMSTestViewController: BaseViewController, GMSMapViewDelegate { @IBOutlet weak var mapView: GMSMapView! let locati

我试图让地图自动跟随用户(位置),但不知何故,应用程序崩溃(没有错误消息),或者启动应用程序时地图不显示。我做错了什么

我尝试过不使用locationManager func,但它确实有效。是否有其他方法来跟踪用户

    class GMSTestViewController: BaseViewController, GMSMapViewDelegate {

        @IBOutlet weak var mapView: GMSMapView!

        let locationManager = CLLocationManager()
        var manager:CLLocationManager!

        override func viewDidLoad() {
            super.viewDidLoad()

            //Setup Location Manager
            manager = CLLocationManager()
            manager.delegate = self
            manager.desiredAccuracy = kCLLocationAccuracyBestForNavigation
            manager.requestAlwaysAuthorization()
            manager.startUpdatingLocation()

            //Map type
            mapView.mapType = kGMSTypeTerrain
        }

        override func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
            let userLocation = locations.last
            let center = CLLocationCoordinate2D(latitude: userLocation!.coordinate.latitude, longitude: userLocation!.coordinate.longitude)

            let camera = GMSCameraPosition.cameraWithLatitude(userLocation!.coordinate.latitude,
                                                              longitude: userLocation!.coordinate.longitude, zoom: 8)
            let mapView = GMSMapView.mapWithFrame(.zero, camera: camera)
            mapView.myLocationEnabled = true
            self.view = mapView

            let marker = GMSMarker()
            marker.position = center
            marker.title = "Current Location"
            marker.snippet = "XXX"
            marker.map = mapView

            locationManager.stopUpdatingLocation()
        }

    }
当我启动它时,它看起来是这样的,然后它会随着地图(在用户位置)不断闪烁。
检查以下事项:-

  • 您已将
    GMSMapview
    @IBOutlet的委托设置为self

  • 您已使用特定密钥更新了info.plist文件

  • 你有一个正确的互联网连接
  • 您已在
    AppDelegate

        var googleApiKey = String(_yourAPIkey)  
        GMSServices.provideAPIKey(googleApiKey)
    

我认为最好的方法是使用关键值观察(KVO):

覆盖函数视图将出现(动画:Bool){
addObserver(self,forKeyPath:“myLocation”,选项:0,上下文:nil)
}
脱硝{
mapView.removeObserver(self,forKeyPath:“myLocation”,上下文:0)
}
重写func observeValueForKeyPath(键路径:String?,对象对象的类型:AnyObject?,更改:[String:AnyObject]?,上下文:UnsafeMutablePointer){
if(keyPath!=“myLocation”]){
let location=[object myLocation]
让目标=
CLLocationCoordinate2DMake(位置.坐标.纬度,位置.坐标.经度);
mapView.animateToLocation(目标)
地图视图.动画缩放(17)
}
}

谷歌地图似乎并没有出现在你们的视图中。我想你们希望得到像图片中一样的视图

因此,我建议您下载谷歌地图SDK版本低于1.10,因为我已经多次面临这个问题

我建议您从下面的链接下载谷歌地图SDK 1.9.1

只需尝试使用版本1.9.1一次即可。希望这东西有用

注意:如果你想使用谷歌地图的place auto complete和其他新服务,那么使用更高版本的谷歌地图SDK也会遇到同样的问题。 通过获取新的API密钥进行修复。 正如我发现的那样,你需要两个键:一个用于GooglePlaces,一个用于GoogleMaps。至少用于测试目的


在谷歌文档中它没有提到。

你能发布崩溃的截图/输出吗?@johnfakerson我已经更新了这个问题
    var googleApiKey = String(_yourAPIkey)  
    GMSServices.provideAPIKey(googleApiKey)
override func viewWillAppear(animated: Bool) {
    mapView.addObserver(self, forKeyPath: "myLocation", options:0, context:nil)
}

deinit {
    mapView.removeObserver(self, forKeyPath:"myLocation", context:0)
}

override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
    if(keyPath! == "myLocation"]) {
        let location = [object myLocation]

        let target =
        CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude);

        mapView.animateToLocation(target)
        mapView.animateToZoom(17)
    }
}