How to pass map data to be displayed by another class?

0

I'm using the Google Maps API for iOS. I was doing some tests and set a UIView as subclass of GMSMapView and kept a pointer to it in ViewController , getting:

#import <UIKit/UIKit.h>
#import <GoogleMaps/GoogleMaps.h>
#import <CoreLocation/CoreLocation.h>
@interface ViewController : UIViewController<GMSMapViewDelegate, CLLocationManagerDelegate>
{
    CLLocationManager *locationManager;
}
@property (strong, nonatomic) IBOutlet GMSMapView *meuMapa;

@end

And I made its implementation in another class, like this:

-(GMSMapView*)criaMapa{
 // Create a GMSCameraPosition that tells the map to display the
    // coordinate -33.86,151.20 at zoom level 6.
    GMSCameraPosition *camera = [GMSCameraPosition
                                 cameraWithLatitude:-22.530351
                                 longitude:-43.071199
                                 zoom:17];
    _mapaCriado = [GMSMapView mapWithFrame:CGRectZero camera:camera];

    #pragma mark - Coloca Pinos no Mapa

    // Uma posição em Niteroi
    GMSMarker *marker = [[GMSMarker alloc] init];
    marker.position = CLLocationCoordinate2DMake(-22.530303,-43.071033);
    marker.title = @"Ponto B";
    marker.snippet = @"Detalhes";

    marker.map = _mapaCriado;

    return _mapaCriado;
}

Returning in ViewController.m in method -(void)viewDidLoad I get the map that was created in another class:

- (void)viewDidLoad {
    CriaMapas mapFact = [[CriaMapas alloc]init];
    self.meuMapa = [mapFact criaMapa];
}

But the map is started with nothing, as if it had not started and no Marker creation.

How could I do this: Create the map in another class and return it to be displayed?

    
asked by anonymous 13.02.2015 / 04:30

1 answer

1

It turns out that you can not just assign my_map to another GMSMapView , in addition to being IBOutlet .

Since the creation of your map is done dynamically, you also need to add the map to your view in this same way. So this IBOutlet can be replaced by a View base that will get the map:

@property (strong, nonatomic) IBOutlet UIView *mapaContainer;

And then create the add map as subview from above:

- (void)viewDidLoad {
    CGRect rect = [mapaContainer frame];
    CriaMapas mapFact = [[CriaMapas alloc] init];

    GMSMapView meuMapa = [mapFact criaMapa];
    [meuMapa setFrame:CGRectMake(0.0f, 0.0f, [rect.size width], [rect.size height])];

    [self.mapaContainer addSubview:meuMapa];
}

You can also change your criaMapa method to be able to directly receive map dimensions, so you do not need to set frame on the outside.

    
13.02.2015 / 12:12