在特定位置显示Google地图

时间:2013-10-22 03:36:43

标签: iphone google-maps ios6

我是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

Google地图占据整个屏幕的原因是 self.view = mapView; 。如何添加Google地图,以便不会占据整个屏幕。

我尝试使用SubView,但仍然无法正常工作。代码是:

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

1 个答案:

答案 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。