我是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
你有嘗試新的子視圖,並在該子視圖中添加'mapView'? –
我會嘗試子視圖 – Noor
讓我知道如果完成? –