2015-08-13 41 views
1

我想适应谷歌地图窗口中的所有标记与自动缩放。斯威夫特谷歌地图适合所有的标记

下面是我的代码

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代码是在视图willappear和viewDidLoad方法添加。 有人可以帮助我在上面的代码中做错了什么。

回答

1

在你的代码已经使用:

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

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

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

希望这有助于 干杯! PS:在你的问题中,你还没有详细说明界限的问题。所以如果这不是解决方案,请解释发生了什么问题。 :)

4
@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))) 
+0

这应该是正确的答案 – mkhoshpour

+0

您节省了我的一天 –