【问题标题】:Swift, Google map fit bound for all the markersSwift,谷歌地图适合所有标记
【发布时间】:2015-08-13 07:51:07
【问题描述】:

我想通过自动缩放来适应谷歌地图窗口中的所有标记。

下面是我的代码

func loadGoogleMap()
{

    dispatch_async(dispatch_get_main_queue(),
        {
            self.googleMapView.clear()

            self.googleMapView.delegate = self
            var visibleRegion : GMSVisibleRegion = self.googleMapView.projection.visibleRegion()
            var bounds = GMSCoordinateBounds(coordinate: visibleRegion.nearLeft, coordinate: visibleRegion.nearRight)
            var count = 1
            for Prop: Property in self.properties
            {
                if Prop.propLat != ""
                {

                   //  println("lat >>  \(Prop.propLat) lang >> \(Prop.propLang)")
                    var latStr = Prop.propLat
                    var latDbl : Double  = Double(latStr.floatValue)
                    var langStr = Prop.propLang as NSString
                    var langDbl : Double = Double(langStr.floatValue)
                    var marker = GMSMarker()

                    if count == 0
                    {
  //                           let initialLocation =   CLLocationCoordinate2DMake(langDbl,latDbl)
   //                            let initialDirection = CLLocationDirection()
    //                            
    //                            let camera = GMSCameraPosition.cameraWithTarget(initialLocation, zoom: 5)
    //                            self.googleMapView.camera = camera
    //                            
                    }

                    marker.position = CLLocationCoordinate2DMake(langDbl,latDbl)
                    marker.appearAnimation = kGMSMarkerAnimationPop
                    marker.icon = UIImage(named: "red_\(Prop.propSequence)")
                    marker.title = Prop.propBuildingName as String
                    marker.snippet = Prop.propCode as String
                    marker.infoWindowAnchor = CGPointMake(0.44, 0.45);
                    bounds.includingCoordinate(marker.position)
                    marker.map = self.googleMapView
                    count++

                }

            }

            self.googleMapView.animateWithCameraUpdate(GMSCameraUpdate.fitBounds(bounds, withPadding: 30.0))

    })

   }

上面的 swift 代码添加到 view willappear 和 viewdidload 方法中。 有人可以帮我在上面的代码中做错什么。

【问题讨论】:

    标签: ios swift google-maps


    【解决方案1】:
    @IBOutlet weak var viewMap: GMSMapView!
    var markers = [GMSMarker]()
    var bounds = GMSCoordinateBounds()
        for marker in self.markers {
            bounds = bounds.includingCoordinate(marker.position)
        }
        viewMap.animate(with: GMSCameraUpdate.fit(bounds, with: UIEdgeInsetsMake(50.0 , 50.0 ,50.0 ,50.0)))
    

    【讨论】:

    • 你拯救了我的一天
    【解决方案2】:

    在您使用的代码中:

    var bounds = GMSCoordinateBounds(coordinate: visibleRegion.nearLeft, coordinate: visibleRegion.nearRight)

    如果您阅读了docs 中的参考资料,您会明白现在您只是指定东南和西南坐标。
    对于边界框,您需要一对 SE/NW 或 NE/SW 坐标。因此,您需要将该行更改为:

    var bounds = GMSCoordinateBounds(coordinate: visibleRegion.nearLeft, coordinate: visibleRegion.farRight)

    希望这会有所帮助, 干杯!

    PS:在您的问题中,您没有具体说明边界有什么问题。因此,如果这不是解决方案,请解释发生了什么问题。 :)

    【讨论】:

      猜你喜欢
      • 2012-12-13
      • 2014-12-24
      • 2014-06-12
      • 2019-11-05
      • 2018-04-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多