8

I am using Google Map service GMSMapview in my iOS Application. In that I have two CLLocationCoordinates. One is Current Location and other is Destination Location. I am trying to fit the map within those two places.

But the camera position is not set within those two points. I tried with the following code,

CLLocationCoordinate2D start = CLLocationCoordinate2DMake(newLoc.coordinate.latitude, newLoc.coordinate.longitude);
CLLocationCoordinate2D end = CLLocationCoordinate2DMake(_retailerVO.R_Latitude, _retailerVO.R_Longitude);
GMSCoordinateBounds *gBounds =
[[GMSCoordinateBounds alloc] initWithCoordinate:start coordinate:end];
GMSCameraPosition *gCamera = [mapView_ cameraForBounds:gBounds insets:UIEdgeInsetsZero];
mapView_.camera = gCamera;

Is there any way to achieve what I am looking for? All suggestions are appreciated.

Maniganda saravanan
  • 2,188
  • 1
  • 19
  • 35

2 Answers2

10

In Swift

 let bounds = GMSCoordinateBounds(coordinate: sourcePosition, coordinate: endPosition)
 let camera: GMSCameraUpdate = GMSCameraUpdate.fit(bounds)
 //  let cameraWithPadding: GMSCameraUpdate = GMSCameraUpdate.fit(bounds, withPadding: 100.0) (will put inset the bounding box from the view's edge)

 self.mapView.animate(with: camera)
Aditya
  • 783
  • 1
  • 8
  • 25
5

Add padding with bounds, it works for me, hope it helps you,

GMSCoordinateBounds *bounds = [[GMSCoordinateBounds alloc] initWithCoordinate:start coordinate:end];

[self.viewMapView animateWithCameraUpdate:[GMSCameraUpdate fitBounds:bounds withPadding:100.0f]];
Patel Jigar
  • 2,141
  • 1
  • 23
  • 30
  • If you don't mind could you please tell me is there any way to keep infowindow (Annotation) on the map even we tap over the map? Which means avoid removing infowindow from the map. – Maniganda saravanan Apr 26 '17 at 11:01
  • you can set GMSMarker's iconView as per your design and requirement – Patel Jigar Apr 26 '17 at 13:04
  • just create a view as per your design and set it to marker's iconView – Patel Jigar Apr 26 '17 at 13:05
  • 1
    in case someone looking to fit more than two Coordinates, use [bounds includingCoordinate: locationCoordinate] for all the coordinates – EarlySun Mar 04 '18 at 09:15
  • @PatelJigar I have created the custom marker's icon view and add it to the map, but sometimes the marker view is cut in edges. Can you please suggest me the possible solution. Map have more than 2 markers – Nimisha Patel Apr 04 '18 at 13:26