2013-02-26 84 views
2

所以我跟着this guide关于如何添加谷歌地图到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样子大气压:

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

回答

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 
+0

我认为这仍然会有“基本视图”作为使用地图的视图。这不是当前的问题了。如果您有兴趣,请参阅原帖上的评论。感谢您的努力! – Millenjo 2013-02-27 09:18:59

0

在你的代码有:

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

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

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

[self.mapView addSubview:mapView_]; 
+0

试过这个,看我的原始文章我的代码更新。可悲的是,没有区别。 – Millenjo 2013-02-26 11:36:06

+0

这里有一个链接到我的“项目”,https://github.com/millenjo/maps.git – Millenjo 2013-02-26 11:54:28

11

执行以下操作:

  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_,也没有从谷歌样本的任何其他代码。请享用!

+0

说明不再需要实例变量的奖励点,即自定义视图占据位置。一旦我删除并调整了所有的引用,我得到了我的自定义视图接受gMap方法。由于它是@属性声明,所以不需要实例化。 – 2014-12-22 17:16:56