添加谷歌地图sdk为ios后我看不到地图

时间:2013-11-12 14:20:25

标签: ios objective-c google-maps google-maps-sdk-ios

我想在UIViewController上面添加一个谷歌地图。但除了我定义的一些标记外,我看不到任何东西。问题看起来像这样: enter image description here 以下是相关代码:

- (void)viewDidLoad {
previousContext = [EAGLContext currentContext];
[super viewDidLoad];


GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-33.868
                                                        longitude:151.2086
                                                             zoom:10];

mapView_ = [GMSMapView mapWithFrame:CGRectMake(0, 0, 1136, 640) camera:camera];
//mapView_ = [GMSMapView mapWithFrame:CGRectZero camera:camera];
mapView_.settings.compassButton = YES;
mapView_.settings.myLocationButton = YES;
mapView_.delegate = self;
// Listen to the myLocation property of GMSMapView.
[mapView_ addObserver:self
           forKeyPath:@"myLocation"
              options:NSKeyValueObservingOptionNew
              context:NULL];

[EAGLContext setCurrentContext: previousContext];

//add makers
CLLocationCoordinate2D position = CLLocationCoordinate2DMake(39.900285, 116.274020);
GMSMarker *marker = [GMSMarker markerWithPosition:position];
marker.title = @"aaa";
marker.snippet = @"population : 5";
marker.infoWindowAnchor = CGPointMake(0.5, 0.5);
marker.map = mapView_;
marker.icon = [UIImage imageNamed:@"tempmarker.png"];

CLLocationCoordinate2D position2 = CLLocationCoordinate2DMake(39.860285, 116.274020);
GMSMarker *marker2 = [GMSMarker markerWithPosition:position2];
marker2.title = @"bbb";
marker2.snippet = @"population : 5";
marker2.infoWindowAnchor = CGPointMake(0.5, 0.5);
marker2.map = mapView_;
marker2.icon = [UIImage imageNamed:@"tempmarker2.png"];



//add buttons:directRoom, rank, achievement, mission, home
....

[self.view insertSubview: mapView_ atIndex: 0];

[EAGLContext setCurrentContext:nil];
// Ask for My Location data after the map has already been added to the UI.
dispatch_async(dispatch_get_main_queue(), ^{
    mapView_.myLocationEnabled = YES;
});
}

那么,如何处理这个问题,非常感谢!

2 个答案:

答案 0 :(得分:1)

供参考,请参阅Google的演示(下方复制)。它与您发布的内容类似,但您可以尝试以下几点:

  1. 确保您的API密钥有效且API可以获取磁贴
  2. 检查您要插入的地图视图的z-index顺序:[self.view insertSubview: mapView_ atIndex: 0];插入其他所有内容。试试[self.view addSubview:mapView_]
  3. 演示代码,来自Google(https://developers.google.com/maps/documentation/ios/):

    #import <GoogleMaps/GoogleMaps.h>
    #import "DemoViewController.h"
    
    @implementation DemoViewController
    
    - (void)viewDidLoad {
      [super viewDidLoad];
      GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-33.868
                                                              longitude:151.2086
                                                                   zoom:6];
      GMSMapView *mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera];
    
      GMSMarker *marker = [[GMSMarker alloc] init];
      marker.position = camera.target;
      marker.snippet = @"Hello World";
      marker.animated = YES;
    
      self.view = mapView;
    }
    
    @end
    

答案 1 :(得分:0)

检查您是否已在应用代理中的didFinishLaunchingWithOption中正确输入了Google API密钥

[GMSServices provideAPIKey:@"yourGoogleAssignedSDKKeyGoesHere"];  

您的xcode应为4.5或更高版本

相关问题