Swift,谷歌地图适合所有标记 [英] Swift, Google map fit bound for all the markers

查看:110
本文介绍了Swift,谷歌地图适合所有标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过自动缩放使Google地图窗口中的所有标记适合.

I want to fit all the markers in the google map window with auto zoom.

下面是我的代码

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))

    })

   }

在view willappear和viewdidload方法上也添加了快速代码. 有人可以帮帮我,我在上面的代码中做错了吗.

Above swift code is added on view willappear and viewdidload method as well. Can someone please help me what i am doing wrong in the above code.

推荐答案

@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)))

这篇关于Swift,谷歌地图适合所有标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆