Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.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 SDK快速拟合边界_Ios_Swift_Google Maps_Bounds - Fatal编程技术网

谷歌ios SDK快速拟合边界

谷歌ios SDK快速拟合边界,ios,swift,google-maps,bounds,Ios,Swift,Google Maps,Bounds,我的谷歌地图上有几个标记,如何将相机安装到这些标记上 这是我的密码: NSURLSession.sharedSession().dataTaskWithURL( NSURL(string: url)!) { data, response, error in do { let jsonData = try NSJSONSerialization.JSONObjectWithData(data!, options:NS

我的谷歌地图上有几个标记,如何将相机安装到这些标记上

这是我的密码:

NSURLSession.sharedSession().dataTaskWithURL(
    NSURL(string: url)!) 
    { data, response, error in
                    do {
            let jsonData = try NSJSONSerialization.JSONObjectWithData(data!, options:NSJSONReadingOptions.MutableContainers ) as! NSDictionary

                        for (var i=0; i < jsonData.allKeys.count; i++){
                            var key = jsonData.allKeys[i] as! String

                            var id = jsonData[key] as! NSDictionary
                            var club = id["club"] as! NSDictionary

                            var location = club["location"] as! NSDictionary

                            var latitude = location["latitude"] as! Double
                            var longitude = location["longitude"] as! Double

                            dispatch_async(dispatch_get_main_queue()) {
                                let position: CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
                                let marker = GMSMarker(position: position)
                                marker.title = club["name"] as! String
                                marker.map = self.map
                            }

                        }

        } catch {
            // report error
        }
        }.resume()
NSURLSession.sharedSession().dataTaskWithURL(
NSURL(字符串:url)!)
{中的数据、响应、错误
做{
让jsonData=try NSJSONSerialization.JSONObjectWithData(data!,选项:NSJSONReadingOptions.MutableContainers)作为!NSDictionary
对于(var i=0;i
首先,您需要创建一个数组来保存标记列表

var markerList = [GMSMarker]()
然后,您可以调用
animateWithCameraUpdate(GMSCameraUpdate.fitBounds(bounds))
来调整相机:

func fitAllMarkers() {
    var bounds = GMSCoordinateBounds()

    for marker in markerList {
        bounds = bounds.includingCoordinate(marker.position)
    }

    map.animateWithCameraUpdate(GMSCameraUpdate.fitBounds(bounds))
    //For Swift 5 use the one below
    //self.mapView.animate(with: GMSCameraUpdate.fit(bounds))
}
您能否在
dispatch\u async
closure中调用此选项:

 dispatch_async(dispatch_get_main_queue()) {
       let position: CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
       let marker = GMSMarker(position: position)
       marker.title = club["name"] as! String
       marker.map = self.map

      //new code
      self.markerList.append(marker)
      self.fitAllMarkers()
   }

首先,您需要创建一个数组来保存标记列表

var markerList = [GMSMarker]()
然后,您可以调用
animateWithCameraUpdate(GMSCameraUpdate.fitBounds(bounds))
来调整相机:

func fitAllMarkers() {
    var bounds = GMSCoordinateBounds()

    for marker in markerList {
        bounds = bounds.includingCoordinate(marker.position)
    }

    map.animateWithCameraUpdate(GMSCameraUpdate.fitBounds(bounds))
    //For Swift 5 use the one below
    //self.mapView.animate(with: GMSCameraUpdate.fit(bounds))
}
您能否在
dispatch\u async
closure中调用此选项:

 dispatch_async(dispatch_get_main_queue()) {
       let position: CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
       let marker = GMSMarker(position: position)
       marker.title = club["name"] as! String
       marker.map = self.map

      //new code
      self.markerList.append(marker)
      self.fitAllMarkers()
   }

在SWIFT 3.1集群点击中:

func clusterManager(_ clusterManager: GMUClusterManager, didTap cluster: GMUCluster) {
    var bounds = GMSCoordinateBounds()
    for marker in cluster.items {
        bounds = bounds.includingCoordinate(marker.position)
    }
    mapView.animate(with: GMSCameraUpdate.fit(bounds))
}

在SWIFT 3.1集群点击中:

func clusterManager(_ clusterManager: GMUClusterManager, didTap cluster: GMUCluster) {
    var bounds = GMSCoordinateBounds()
    for marker in cluster.items {
        bounds = bounds.includingCoordinate(marker.position)
    }
    mapView.animate(with: GMSCameraUpdate.fit(bounds))
}