无法将谷歌地图分配给自定义视图 [英] Can't assign a google maps map to custom view
问题描述
所以我按照本指南了解如何将谷歌地图添加到 iOS 应用程序,一切正常.但是我想要做的是分配地图,而不是 self.view
而是我拖到故事板里面的自定义视图(?)另一个视图,因为在将地图添加到 时>self.view
我不能添加其他元素,比如按钮,或者好吧,我可能可以,但我不知道如何.
So I followed this guide on how to add a Google map to an iOS application and it all works fine. But what I want to do is assign the map, not to self.view
but a custom view I dragged out on to the storyboard inside(?) the other view, because when adding the map to self.view
I can't add other elements, like buttons, or well, I probably can but I don't know how.
代码:
// 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 的出口.所以我改变了代码行
So i created a UIView
inside the view currently containing the map and ctrl dragged it to create an outlet called mapView in that viewController
. So i changed the line of code from
self.view = _mapView;
到
self.mapView = _mapView;
但这似乎根本不起作用,只是空白.self.view
和 self.mapsView
都是 UIView
的实例,为什么这不起作用?
but this does not seem to work at all, just blank. Both self.view
and self.mapsView
are instances of UIView
, so why does this not work?
更新:
这就是我的 viewDidLoad atm 的样子:
This is what my viewDidLoad looks like 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_;
}
推荐答案
在您的项目中试试这个:
Try this in your project:
//
// 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
这篇关于无法将谷歌地图分配给自定义视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!