Positioning camera that shows two places in GMSMapview iOS
Asked Answered
T

2

9

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.

Tarazi answered 26/4, 2017 at 10:28 Comment(0)
N
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]];
Nectarine answered 26/4, 2017 at 10:34 Comment(5)
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.Tarazi
you can set GMSMarker's iconView as per your design and requirementNectarine
just create a view as per your design and set it to marker's iconViewNectarine
in case someone looking to fit more than two Coordinates, use [bounds includingCoordinate: locationCoordinate] for all the coordinatesEndue
@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 markersStepchild
H
11

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)
Horizon answered 2/1, 2019 at 5:31 Comment(0)
N
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]];
Nectarine answered 26/4, 2017 at 10:34 Comment(5)
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.Tarazi
you can set GMSMarker's iconView as per your design and requirementNectarine
just create a view as per your design and set it to marker's iconViewNectarine
in case someone looking to fit more than two Coordinates, use [bounds includingCoordinate: locationCoordinate] for all the coordinatesEndue
@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 markersStepchild

© 2022 - 2024 — McMap. All rights reserved.