2015-08-13 44 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

您節省了我的一天 –