سؤال

I'm working on an app for iOS and i'm running into some trouble with the google maps sdk.

My app gets a list of coordinates and draws a polyline between them as well as start and finish markers. At the moment, this all occurs successfully. What i'm trying to do now is update the camera to contain the entire path. Here is my code currently:

GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:[[sharedModel startNode] nodeLocation].latitude
                                                        longitude:[[sharedModel startNode] nodeLocation].longitude
                                                             zoom:18];
mapView_ = [GMSMapView mapWithFrame:CGRectZero camera:camera];

GMSMutablePath *path = [GMSMutablePath path];
for (int i = 0; i < [results count]; i++)
{
    [path addCoordinate:[[results objectAtIndex:i] nodeLocation]];
}
GMSPolyline *polyline = [GMSPolyline polylineWithPath:path];
polyline.strokeWidth = 10;
polyline.map = mapView_;

GMSMarker *marker1 = [[GMSMarker alloc] init];
marker1 = [GMSMarker markerWithPosition:[[results objectAtIndex:0] nodeLocation]];//[[sharedModel startNode] nodeLocation]];
marker1.title = @"Start";
marker1.map = mapView_;

GMSMarker *marker2 = [[GMSMarker alloc] init];
marker2 = [GMSMarker markerWithPosition:[[results lastObject] nodeLocation]];
marker2.title = @"Finish";
marker2.map = mapView_;

GMSCoordinateBounds *bounds = [[GMSCoordinateBounds alloc] initWithPath:path];

GMSCameraUpdate *update = [GMSCameraUpdate fitBounds:bounds];

[mapView_ moveCamera:update];
self.view = mapView_;

When I run this, my markers and polyline all show up in the right locations but the map is zoomed out to the max distance it can go (although it is centered over the path). Any advice?

هل كانت مفيدة؟

المحلول

Is this code in loadView? Try maybe creating the map view in loadView, but updating the camera later, maybe in viewDidLoad or viewWillAppear.

Based on the answer to this question I'm thinking that maybe the camera update methods don't work properly from loadView:

Fit bounds not working as expected

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top