Google Maps zoom out to GPS and markers
Showing All Markers with screen bound:
Here I am trying to provide you one simple example, hope this will help you.Using this, you can show any number of markers on screen.
Example:
let path = GMSMutablePath()for var i in 0 ..< array.count //Here take your "array" which contains lat and long.{ let marker = GMSMarker() let lat = Double(array.objectAtIndex(i).valueForKey(lattitude) as! String) let long = Double(arrayr.objectAtIndex(i).valueForKey(longitude) as! String) marker.position = CLLocationCoordinate2DMake(lat!,long!) path.addCoordinate(marker.position) marker.map = self.mapView}let bounds = GMSCoordinateBounds(path: path)self.mapView!.animateWithCameraUpdate(GMSCameraUpdate.fitBounds(bounds, withPadding: 30.0))
Can you try the below code, this is converted from ObjC code here is the documentation of includingCoordinate
let bounds = GMSCoordinateBounds()bounds.includingCoordinate(self.startMarker.position)bounds.includingCoordinate(self.endMarker.position)bounds.includingCoordinate(yourCurrentLocationPosition)self.mapView.animateWithCameraUpdate(GMSCameraUpdate(fitBounds:bounds, padding:20.0f))
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { showMarkers(locations) //stop updating location when you find suitable}func showMarkers(userLocation: [CLLocation]){ let location1: CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: self.startLatitude, longitude: self.startLongitude) let location2: CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: self.endLatitude, longitude: self.endLongitude) let location3: CLLocationCoordinate2D = userLocation[0].coordinate var locationArray = [] locationArray.append(location1) locationArray.append(location2) locationArray.append(location3) var bounds = GMSCoordinateBounds() for location in locationArray { let latitude = location.valueForKey("latitude") let longitude = location.valueForKey("longitude") let marker = GMSMarker() marker.position = CLLocationCoordinate2D(latitude: latitude, longitude: longitude) marker.map = self.mapView bounds = bounds.includingCoordinate(marker.position) } let update = GMSCameraUpdate.fitBounds(bounds, withPadding: 100) mapView.animateWithCameraUpdate(update)}
Note:
locationArray
contains three locations ie. Marker 1, Marker 2, user location.