Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Positioning camera that shows two places in GMSMapview iOS

Tags:

ios

gmsmapview

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.

like image 439
Maniganda saravanan Avatar asked Apr 26 '17 10:04

Maniganda saravanan


2 Answers

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)
like image 91
Aditya Avatar answered Sep 28 '22 16:09

Aditya


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]];
like image 20
Patel Jigar Avatar answered Sep 28 '22 16:09

Patel Jigar