无法将Google Maps Map分配给自定义视图 [英] Can't assign a google maps map to custom view
问题描述
因此,我按照本指南进行操作将Google地图添加到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
这篇关于无法将Google Maps Map分配给自定义视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!