Search code examples
iosobjective-cgoogle-mapsios7google-maps-sdk-ios

iOS Google maps sdk - markers are not pointed while using subview instead of main view


Trying to display a simple Google map with a marker at location "position".

If mapview_ is shown in self.view(Default view of controller) instead of gmapsView (subview of self.view) of class GMSMapView then everything is working fine.

I have gone through some of the posts on SO but couldn't solve the issue. in Interface builder gmapsview class is set to GMSMapView.

Camera Postion setup

 CLLocationCoordinate2D position = CLLocationCoordinate2DMake(
                                                             37.778376,
                                                             -122.409853);
CLLocation *currentLocation = [[CLLocation alloc] initWithLatitude:position.latitude longitude:position.longitude];
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:position.coordinate.latitude
                                                        longitude:position.coordinate.longitude
                                                             zoom:13];  

Map setup

mapView_ = [GMSMapView mapWithFrame:self.gmapsView.frame camera:camera];
mapView_.delegate = self;
self.gmapsView.camera = camera; //gmapsView is a view of class GMSMapView

CGRect rect = self.locationBlockView.frame;

self.gmapsView.delegate = self; //Added to check whether it works
self.gmapsView = mapView_;  

Setting up Marker

GMSMarker *marker = [GMSMarker markerWithPosition:position]; //Adding Marker
marker.map = mapView_;
[mapView_ animateToLocation:position.coordinate];

Solution

  • Re-Initializing the map was causing the issue. Once it is removed it solved the issue. But Re-initializing GMSMapView is causing the issue ? Its an absurd thing, but it solved the issue.

    CLLocationCoordinate2D position = CLLocationCoordinate2DMake(
                                                                 12.778376,
                                                                 -122.409853);
    currentLocation = [[CLLocation alloc] initWithLatitude:position.latitude longitude:position.longitude];
    GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:eventLocation.coordinate.latitude
                                                            longitude:eventLocation.coordinate.longitude
                                                                 zoom:13];
    //[self.gmapsView removeFromSuperview];//removing the subview
    //self.gmapsView = [GMSMapView mapWithFrame:self.gmapsView.frame camera:camera];
    //self.gmapsView.delegate = self;
    
    //self.gmapsView = self.gmapsView; //set map
    
    // [self.view addSubview:self.gmapsView];//Adding back did the magic
    self.gmapsView.delegate = self; //set delegate
    self.gmapsView.camera = camera; //set camera
    
    GMSMarker *marker = [GMSMarker markerWithPosition:position]; //Adding Marker
    marker.map = self.gmapsView;
    [self.gmapsView animateToLocation:currentLocation.coordinate];