Question

I want use fitBounds method in GoogleMaps for ios SDK and view does not fit. My varaibles are all right (path, arrays, etc...) because I can see polyline with markers on map. The only thing that does not work is fit to view. Where I have made mistake? thank you.

// Create a 'normal' polyline.
GMSPolyline *polyline = [[GMSPolyline alloc] init];
GMSMutablePath *path = [GMSMutablePath path];

locationInfoArray = [LocationInfoMemoryManager loadLocationDataWithPath:_locationInfoPathString];

for (int i=0; i<locationInfoArray.count; i++) {
    LocationInfo* locationInfo = locationInfoArray[i];
    CLLocationCoordinate2D locationPoint = {locationInfo.latitude, locationInfo.longitude};
    [path addCoordinate:locationPoint];
}


GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:[locationInfoArray[0] latitude]
                                                        longitude:[locationInfoArray[0] longitude]
                                                             zoom:5 ];


mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera];

polyline.path = path;
polyline.strokeColor = [UIColor blueColor];
polyline.strokeWidth = 10.f;
polyline.zIndex = 15;  // above the larger geodesic line
polyline.map = mapView;

GMSMarker *startMarker = [[GMSMarker alloc] init];
startMarker.title = @"Start";
startMarker.snippet = @"Info will be here";
startMarker.position = CLLocationCoordinate2DMake([[locationInfoArray firstObject] latitude], [[locationInfoArray firstObject] longitude]);
startMarker.map = mapView;
startMarker.flat = NO;
//sydneyMarker.rotation = 30.0;
mapView.selectedMarker = startMarker;

GMSMarker *finishMarker = [[GMSMarker alloc] init];
finishMarker.title = @"Finish";
finishMarker.snippet = @"Info will be here";
finishMarker.position = CLLocationCoordinate2DMake([[locationInfoArray lastObject] latitude], [[locationInfoArray lastObject] longitude]);
finishMarker.map = mapView;
finishMarker.flat = NO;
mapView.selectedMarker = finishMarker;

//Here is probably problem

GMSCoordinateBounds *bounds; = [[GMSCoordinateBounds alloc] initWithPath:path];
GMSCameraUpdate *update = [GMSCameraUpdate fitBounds:bounds withPadding:20];
[mapView moveCamera:update];

self.view = mapView;
Was it helpful?

Solution

Is this code in loadView or viewDidLoad? Based on these earlier questions, I think camera updates only work correctly from viewWillAppear:

GMSCameraUpdate zooming out to the max distance rather than around a path

Fit bounds not working as expected

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top