How to set center location of GMSMapView object defined at Storyboard

ankakusu picture ankakusu · Dec 13, 2013 · Viewed 7k times · Source

I want to initialise the center of the map to a custom location. Here is the code that I use for initialization:

-(void)viewDidLoad{
    [super viewDidLoad];
    //mapView
    GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:41.0109
                                                            longitude:28.998
                                                                 zoom:6];
    self.mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera];
    [self.mapView setCenter:CGPointMake(41.0109,28.998)];
}

When I run this code, I saw a map centered to the Greenwich rather than the position that I gave to the camera(41.0109,28.998).

When I debug the code and print out the position of self.mapView.camera this is the output: GMSCameraPosition 0x17742ee0: target:(41.011, 28.998) bearing:0.000 zoomLevel:6.000 viewingAngle:0.000 lookAhead:0.000. So I successfully set the location of the camera, why does it opening with the location of Greenwich?

Edit:

Please note that, mapView is a subview (UIView) that I added to the main page and I was trying to set and modify the center location. Here is a snapshot of my storyboard: enter image description here

When I try to modify mapView, I cannot set the location of the map. However, if I use the "view" which is the main view of the window, I can successfully change the center of the map.

Why is the reason for that?

How can I set the startup position of GMSMapView?

Edit 2 The source code for this project can be found here

Answer

iltercengiz picture iltercengiz · Dec 19, 2013

You ended up with two GMSMapView objects. The first one is the one that you created in Interface Builder and the second one is the one that you created programmatically with GMSCameraPosition object in -viewDidLoad method.

Seems like self.mapsView references the view that you created in Interface Builder. When you assign a new GMSMapView object with this line;

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

you are actually losing the one that is created in Interface Builder, so your attempt to change the camera position is not working, because you're sending the message to the wrong object.

Also because of not adding the second GMSMapView object to the view hierarhcy after you created it programmatically, in -viewDidLoad method, you are not seeing it in your app. Although it sets its camera position correctly.

So you have two options;

1- Remove the mapView from interface builder (and unbind its outlet) and don't touch to the code but replace this line;

[self.mapView setCenter:CGPointMake(41.0109,28.998)];

with this line;

[self.view addSubview:self.mapView];

So that you'll have a map view created programmatically.

2- Don't touch to the storyboard, but remove this line;

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

So you'll be creating a map view in Interface Builder, and configuring it.

Then it would be all fine.