0
votes

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];
2
I don't get you Im afraid. what do you want, what works, what doesn't?Daij-Djan
@Daij-Djan 1. Maps and markers are loading in default view. 2.Maps are loaded in the subview but markers are not shown.Dileep Mettu
gmapsView is of class "GMSMapView". class is set in the IB and have link in the controller.Dileep Mettu
why then do you alloc init a new instance?Daij-Djan
Yes thats the mistake! Initially tried sample code provided by developer.google and later on modified to my need. I have just tried removing the instantiation and viola its working. But logically Even though if its re-instantiated it should work right ? In a normal scenario When an UIView is accessed by re-initialized it works why its failing here ?Dileep Mettu

2 Answers

3
votes

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];
1
votes

Add Google Map To Subview

The Solution Steps

1) Change the class of the UIView in the story to GMSMapVIew.

enter image description here

2) Next I created an IBOutlet for the green UIView container and Connected it.

#import <UIKit/UIKit.h>
#import <GoogleMaps/GoogleMaps.h>

@interface CarLocationViewController : UIViewController  
//An outlet of type GMSMapView
@property (strong, nonatomic) IBOutlet GMSMapView *mapContainerView;
@end

3) Then in my view did load I did the following

- (void)viewDidLoad {
    GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-33.86
                                                            longitude:151.20
                                                                 zoom:14];

    GMSMarker *marker = [[GMSMarker alloc] init];
    marker.position = CLLocationCoordinate2DMake(-33.86, 151.20);
    marker.title = @"Sydney";
    marker.snippet = @"Australia";
    marker.map = self.mapContainerView;

    //set the camera for the map
    self.mapContainerView.camera = camera;

    self.mapContainerView.myLocationEnabled = YES;

}

I am Following This Link http://www.ryanwright.me/cookbook/ios/obj-c/maps/gmap/subview