Can't assign a google maps map to custom view

情到浓时终转凉″ 提交于 2020-01-01 17:12:07

问题


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.

Code:

// 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_;

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;

to

self.mapView = _mapView;

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?

Update:

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

回答1:


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



回答2:


Do the following:

  1. Change your custom view Class to GMSMapView in the identity inspector of IB
  2. create outlet mapView to this custom view in you view controller (pay attention that outlet type is also GMSMapView)

So now when you start your app this instance will be created, and it will display default location on the map (near London). Now you only need to create a camera and add it to the map object. Add this code in your ViewDidLoad method:

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 

Thats it, you do not need instance variable mapView_ from Google sample code, nor any other code from Google sample. Enjoy!




回答3:


In your code you have:

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

CGRectZero will make its size zero. You should use self.mapView.frame instead.

Also need to add add mapView_ as subview of mapView:

[self.mapView addSubview:mapView_];


来源:https://stackoverflow.com/questions/15087364/cant-assign-a-google-maps-map-to-custom-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!