dhaval shah
dhaval shah

Reputation: 4549

Swift, Google map fit bound for all the markers

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

Below is my code

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

    })

   }

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.

Upvotes: 5

Views: 8264

Answers (3)

Gangireddy Rami Reddy
Gangireddy Rami Reddy

Reputation: 989

Hope this helps, Cheers!

if self.allSelectedActiveMarkers.count > 0 {
               let firstLocation = CLLocationCoordinate2D(latitude: self.allSelectedActiveMarkers.first?.currentLat ?? 0.0, longitude: self.allSelectedActiveMarkers.first?.currentLong ?? 0.0)
                 var bounds = GMSCoordinateBounds(coordinate: firstLocation, coordinate: firstLocation)
                 for marker in self.allSelectedActiveMarkers {
                   bounds = bounds.includingCoordinate(marker.position)
                 }
                let update = GMSCameraUpdate.fit(bounds, withPadding: CGFloat(20))
                self.mapView.animate(with: update)
             }

Upvotes: 0

urvashi bhagat
urvashi bhagat

Reputation: 1183

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

Upvotes: 21

Kay_N
Kay_N

Reputation: 997

In your code you have used:

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

If you read the reference in the docs, you will understand that right now you are just specifying SouthEast and SOUTHWest coordinates.
For a bounding box you will need a pair of SE/NW or NE/SW coordinates. So you need to change that line to:

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

Hope this helps, Cheers!

PS: In your question, you haven't specified exactly what is the problem with bounds. So if this is not the solution, please explain what's going wrong. :)

Upvotes: 1

Related Questions