2013-02-26 78 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