2013-10-22 206 views
1

我是IOS開發新手,我試圖將Google地圖添加到示例應用。我正在使用教程https://developers.google.com/maps/documentation/ios/。除了谷歌地圖正在整個屏幕上,一切都很好。用於顯示谷歌地圖的代碼是在特定位置顯示Google地圖

#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 

爲什麼谷歌地圖,走的是整個屏幕的原因是因爲self.view = MapView的;。我如何添加Google地圖,以便它不會佔滿整個屏幕。

我試過使用一個子視圖如下,但它仍然無法正常工作。代碼是:

ViewController.h

@interface ViewController : UIViewController 
@property UIView *myView; 
@end 

ViewController.m

#import "ViewController.h" 
#import <GoogleMaps/GoogleMaps.h> 

@interface ViewController() 

@end 

@implementation ViewController{ 
    GMSMapView *mapView_; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 
    CGRect viewRect = CGRectMake(0, 0, 100, 100); 
    _myView = [[UIView alloc] initWithFrame:viewRect]; 
    [self.view addSubview:_myView]; 



    GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-33.86 
                  longitude:151.20 
                   zoom:6]; 
    mapView_ = [GMSMapView mapWithFrame:CGRectZero camera:camera]; 
    mapView_.myLocationEnabled = YES; 
    self.view = mapView_; 

    // Creates a marker in the center of the map. 
    GMSMarker *marker = [[GMSMarker alloc] init]; 
    marker.position = CLLocationCoordinate2DMake(-33.86, 151.20); 
    marker.title = @"Sydney"; 
    marker.snippet = @"Australia"; 
    marker.map = mapView_; 
} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 
@end 
+0

你有嘗試新的子視圖,並在該子視圖中添加'mapView'? –

+0

我會嘗試子視圖 – Noor

+0

讓我知道如果完成? –

回答

1

嘗試更換self.view = mapView_;viewDidLoad方法:

[self.view addSubview:mapView_]; 

這會將您的mapview作爲子視圖添加到當前視圖。

編輯

嘗試設置你的MapView的框架。在[self.view addSubview:mapView_]之前;嘗試:

mapView_.frame = CGRectMake(10,10,100,100)]; 

更改10,10,100,100爲您的mapview需要的框架。

+0

nopes,它不工作:( – Noor

+0

你認爲我需要調用任何drawRect方法來初始化視圖嗎? – Noor

+0

@Noor never。你不會調用drawRect。它會被系統自動調用。 setNeedsDisplay強制重繪。 –

相關問題