无法分配谷歌地图的地图,以自定义视图(Can't assign a google maps

2019-08-17 04:10发布

所以我跟着本指南对如何将谷歌地图添加到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拖动它来创建在调用MapView的出口viewController 。 所以我改变的代码行从

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_;
}

Answer 1:

在您的项目试试这个:

//
//  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


Answer 2:

请执行下列操作:

  1. 更改您的自定义视图类,以GMSMapView在IB的身份检查
  2. 创建出口mapView这个自定义视图中查看控制器(注意是出口型也GMSMapView

所以,现在当你开始你的应用程序这种情况下将被创建,它会显示地图(伦敦附近)的默认位置。 现在你只需要创建一个摄像头,并把它添加到地图对象。 在您的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 

这就是它,你不需要实例变量mapView_从谷歌的示例代码,也没有从谷歌样本的任何其他代码。 请享用!



Answer 3:

在你的代码有:

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

CGRectZero将其大小为零。 您应该改用self.mapView.frame。

还需要添加添加mapView_为MapView类的子视图:

[self.mapView addSubview:mapView_];


文章来源: Can't assign a google maps map to custom view