Search code examples
iosobjective-cviewresizegoogle-maps-sdk-ios

Resizing Google Maps MapView iOS


I've attempted to use the code from the GoogleMapsSDKDemos to resize the mapview to a smaller rectangle on the screen, but it doesn't appear to have any effect. Here is my code:

mapView = [[GMSMapView alloc] initWithFrame:CGRectZero]; mapView.camera = [GMSCameraPosition cameraWithLatitude:38.98549782690282 longitude:-76.94636067188021 zoom:17];

self.view = [[UIView alloc] initWithFrame:CGRectZero];
mapView.autoresizingMask =
UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
mapView.frame = self.view.frame;
mapView.clipsToBounds = YES;
mapView.myLocationEnabled = YES;
mapView.settings.tiltGestures = NO;
mapView.settings.zoomGestures = NO;

mapView.frame = CGRectMake(0,0, 10, 25);

[self.view addSubview:self.mapView];

That second-to-last line seemingly should limit the MapView to just a small square, but it still takes up the entire screen.


Solution

  • I would try something like this:

    GMSCameraPosition* camera = [GMSCameraPosition 
        cameraWithLatitude: 38.98549782690282 
        longitude: -76.94636067188021 
        zoom: 17];
    GMSMapView* mapView = 
        [GMSMapView mapWithFrame: GRectMake(0,0, 10, 25) camera: camera];
    

    Note that the comments on the GMSMapView class in GMSMapView.h say:

    This class should not be instatiated directly, instead it must be created with [GMSMapServices mapWithFrame:camera:]