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

Asked

Viewed 49 times

0

I’m using Google’s Maps API for iOS. I was doing some testing and I set up a UIView as a subclass of GMSMapView and kept a pointer for her 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 did its implementation in another class, this way:

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

Getting back in ViewController.m in the 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 there had been no initialization and no creation of Marker.

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

1 answer

1

Turns out you can’t just assign to meuMapa another GMSMapView, besides this first being a IBOutlet.

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

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

And then create to add the map as subview of this 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 method criaMapa to be able to directly receive the map dimensions, so you do not need to define the frame outside.

  • Iboutlet is a Uiview added by the Build Interface, and the Gmsmapview class is assigned to it, so I just assign it the map information in Viewcontroller itself and it works... I thought I could assign a Gmsmapview started on another object to it too.... but is there a way to have a map "factory"? I’ll give you one more search.... Vlw!

  • Dude, I did a test just instantiating and passing Marker... and it makes more sense :D. It’s possible to do this with Gmsmarker. Vlw!

  • 1

    Yes, it is the other way really more practical, since the map has already been created by Builder interface, because there is no need to create twice. Either create it dynamically or by the interface, the after assigns the features and etc.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.