2

因此,我按照本指南了解如何将 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 将其拖动以在其中创建一个名为 mapView 的插座viewController。所以我改变了代码行

self.view = _mapView;

self.mapView = _mapView;

但这似乎根本不起作用,只是空白。self.view和都是self.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_;
}
4

3 回答 3

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_Google 示例代码中的实例变量,也不需要 Google 示例中的任何其他代码。享受!

于 2013-05-16T17:59:09.407 回答
0

在您的代码中,您有:

mapView_ = [GMSMapView mapWithFrame:CGRectZero camera:camera];

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

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

[self.mapView addSubview:mapView_];
于 2013-02-26T11:05:01.057 回答
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
于 2013-02-26T19:36:09.780 回答