Google Maps SDK not displaying properly in UIView?

2019-01-20 10:55发布

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?

3条回答
不美不萌又怎样
2楼-- · 2019-01-20 11:26

I am not sure what is wrong with the code above. It looks fine for me. But to move the camera to certain location. You can animate the camera there as well.

var newLocation = CLLocationCoordinate2DMake(latitude, longitude)
googleMapView.animateToLocation(newLocation)

Hope that it works for you.

查看更多
混吃等死
3楼-- · 2019-01-20 11:38

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)

查看更多
Emotional °昔
4楼-- · 2019-01-20 11:46

Finally figured it out...

removed:

self.googleMapView = mapView;

replaced:

[self.googleMapView addSubview:mapView];
查看更多
登录 后发表回答