Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Maps SDK not displaying properly in UIView?

I'm having some problems displaying google maps in Xcode 6.1

I managed to get the map to display in a section of my UIView, but it displays the world map instead of at a particular coordinate.

Here is the issue:

enter image description here

.h:

@interface MapViewController : UIViewController

@property (strong, nonatomic) IBOutlet UIView *googleMapView;

- (IBAction)mapToMain:(UIButton *)sender;

@end

.m:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude: 32.915134 
    longitude: -117.140269 zoom: 17];

    GMSMapView *mapView = [GMSMapView mapWithFrame:self.googleMapView.bounds
    camera:camera];
    mapView.myLocationEnabled = YES;

    self.googleMapView = mapView;

    GMSMarker *marker = [ [GMSMarker alloc] init];
    marker.position = CLLocationCoordinate2DMake(32.915134, -117.140269);
    marker.title = @"Tet Festival 2015";
    marker.snippet = @"VAYA Tet";
    marker.map = mapView;
}

I saw some suggestions here, but it didn't work:

Cannot put a google maps GMSMapView in a subview of main main view?

Using the Google Maps SDK in views other than the main view

Any suggestions?

like image 700
Pangu Avatar asked Nov 06 '14 01:11

Pangu


2 Answers

You have to add a UIView to your Storyboard... but after that you have to turn this simple UIView into a GMSMapView! Select the view you just added and open the Identity Inspector by selecting the third tab from the left in the Utilities toolbar, change the view’s class name from UIView to GMSMapView, as shown in the screenshot below:

enter image description here

Your Outlet will not be like this

@property (strong, nonatomic) IBOutlet UIView *googleMapView;

it will be like this

@property (strong, nonatomic) IBOutlet GMSMapView *mapView;

Your ViewDidLoad can be like this:

 GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude: 32.915134
                                                            longitude: -117.140269 zoom: 17];

    [self.mapView animateToCameraPosition:camera];

    GMSMarker *marker = [ [GMSMarker alloc] init];
    marker.position = CLLocationCoordinate2DMake(32.915134, -117.140269);
    marker.title = @"Tet Festival 2015";
    marker.snippet = @"VAYA Tet";
    marker.map = self.mapView;

UPDATE

Here you have an example project (insert your Key)

like image 120
TonyMkenu Avatar answered Nov 04 '22 22:11

TonyMkenu


Finally figured it out...

removed:

self.googleMapView = mapView;

replaced:

[self.googleMapView addSubview:mapView];
like image 39
Pangu Avatar answered Nov 05 '22 00:11

Pangu