无法将Google地图地图分配给自定义视图

时间:2013-02-26 10:59:32

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

所以我按照this guide关于如何将Google地图添加到iOS应用程序中,一切正常。但我想要做的是分配地图,而不是self.view,而是将我拖到内部(?)另一个视图中的故事板的自定义视图,因为将地图添加到self.view我可以添加其他元素,比如按钮,或者我可能可以,但我不知道如何。

代码:

// Start locationmanager
locationManager = [[CLLocationManager alloc] init];
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.delegate = self;
[locationManager startUpdatingLocation];

// Set up the startposition for the camera when the app first starts.
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:59.34702 longitude:18.04053 zoom:10];
mapView_ = [GMSMapView mapWithFrame:CGRectZero camera:camera];
self.view = mapView_;

所以我在当前包含地图的视图中创建了一个UIView,然后用ctrl拖动它以在viewController中创建一个名为mapView的插座。所以我改变了

的代码行
self.view = _mapView;

self.mapView = _mapView;

但这似乎根本不起作用,只是空白。 self.viewself.mapsView都是UIView的实例,为什么这不起作用?

更新

这就是我的viewDidLoad看起来像atm:

- (void)viewDidLoad
{
[super viewDidLoad];

[self.mapView addSubview:mapView_];

// Start locationmanager
locationManager = [[CLLocationManager alloc] init];
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.delegate = self;
[locationManager startUpdatingLocation];

// Standard cameraposition
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:59.34702 longitude:18.04053 zoom:10];
mapView_ = [GMSMapView mapWithFrame:self.mapView.frame camera:camera];


self.mapView = mapView_;
}

3 个答案:

答案 0 :(得分:11)

执行以下操作:

  1. 在IB的身份检查器中将自定义视图类更改为GMSMapView
  2. 在您的视图控制器中为此自定义视图创建插座mapView(注意插座类型也是GMSMapView
  3. 所以现在当你启动你的应用程序时,将创建这个实例,它将在地图上显示默认位置(伦敦附近)。现在,您只需创建一个摄像头并将其添加到地图对象中。在ViewDidLoad方法中添加此代码:

    GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:45.24 longitude:19.84 zoom:6]; 
    // set camera location to Novi Sad, Serbia :-)
    
    [self.mapView setCamera:camera];     // set the camera to map 
    

    多数民众赞成,您不需要Google示例代码中的实例变量mapView_,也不需要Google示例中的任何其他代码。享受!

答案 1 :(得分:0)

在您的代码中,您有:

mapView_ = [GMSMapView mapWithFrame:CGRectZero相机:相机];

CGRectZero将使其大小为零。你应该使用self.mapView.frame。

还需要添加mapView_作为mapView的子视图:

[self.mapView addSubview:mapView_];

答案 2 :(得分:0)

在您的项目中尝试此操作:

//
//  testViewController.m
//  maps
//
//  Created by Millén on 2013-02-25.
//  Copyright (c) 2013 JamWeb. All rights reserved.
//

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

@interface testViewController ()
@property(nonatomic, strong) GMSMapView *gMapView;
@end

@implementation testViewController {
    CLLocation *oldLocation;
}

-(void)loadView
{
    CLLocationDegrees lat = 59.34702;
    CLLocationDegrees lon = 18.04053;

    GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:lat longitude:lon zoom:10];
    self.gMapView = [GMSMapView mapWithFrame:CGRectZero camera:camera];
    self.gMapView.myLocationEnabled = YES;
    self.view = self.gMapView;

    [self updateLocation];
}

- (void)viewDidLoad
{
    [super viewDidLoad];


    // Start locationmanager
    locationManager = [[CLLocationManager alloc] init];
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    locationManager.delegate = self;
    [locationManager startUpdatingLocation];


}

- (IBAction)updateLocation {
    [locationManager startUpdatingLocation];
}



- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    CLLocation *location = [locations lastObject];

    /*
    if(!oldLocation)
    {
        oldLocation = location;
    }

    if (![location isEqual:oldLocation]) {
        GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude: location.coordinate.latitude
                                                                longitude: location.coordinate.longitude
                                                                     zoom:7];

        mapView_ = [GMSMapView mapWithFrame:CGRectZero camera:camera];
        mapView_.myLocationEnabled = YES;
        self.view = mapView_;

        oldLocation = location;
        [locationManager stopUpdatingLocation];
    }
     */



    NSLog(@"lat%f - lon%f", location.coordinate.latitude, location.coordinate.longitude);
}

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

@end