Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set visible region/zoom level for google maps IOS to show all markers added to the mapview

I have used Google maps in ios app, I wanna set zoom level dynamically depends on the search i made over map. Basically am adding pins by searching with city names or lat/long query. after every search am adding pins & i need to show all added markers by the recent search i made.

like image 830
Ranganatha G V Avatar asked Oct 22 '13 10:10

Ranganatha G V


2 Answers

@IBOutlet weak var mapView: GMSMapView!

let camera = GMSCameraPosition.cameraWithLatitude(23.0793, longitude:  
72.4957, zoom: 5)
mapView.camera = camera
mapView.delegate = self
mapView.myLocationEnabled = true

*** arry has dictionary object which has value of Latitude and Longitude. ***
let path = GMSMutablePath()

for i in 0..<arry.count {

   let dict = arry[i] as! [String:AnyObject]
   let latTemp =  dict["latitude"] as! Double
   let longTemp =  dict["longitude"] as! Double

   let marker = GMSMarker()
   marker.position = CLLocationCoordinate2D(latitude: latTemp, longitude: longTemp)
   marker.title = "Austrilia"
   marker.appearAnimation = kGMSMarkerAnimationNone
   marker.map = self.mapView

  path.addCoordinate(CLLocationCoordinate2DMake(latTemp, longTemp))

} 

 let bounds = GMSCoordinateBounds(path: path)
 self.mapView!.animateWithCameraUpdate(GMSCameraUpdate.fitBounds(bounds, withPadding: 50.0))
like image 82
Dipang Avatar answered Nov 15 '22 05:11

Dipang


See this answer for a simple way to iterate over a given array of markers and then set the bounds accordingly.

like image 30
friedbunny Avatar answered Nov 15 '22 05:11

friedbunny