0

Currently, i'm using SWRevealViewController to create a sidebar and google map api for my project.

https://github.com/John-Lluch/SWRevealViewController

Here's the problem. Everytime I switch between ViewControllers using the sidebar and back to the MapViewController which contains GMSMapView, the GMSMapView is reloaded to my location (I set it in my viewDidLoad method).

How can I prevent MapViewController reloads everytime like that ?


What I've tried so far: Make the MapViewController becomes a singleton and set it to destinationViewController but it doesn't work. All I got is a black screen.

my code for the custom segue:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{

    if ( [segue isKindOfClass: [SWRevealViewControllerSegue class]] )
    {
        SWRevealViewControllerSegue* rvcs = (SWRevealViewControllerSegue*) segue;
        SWRevealViewController* rvc = self.revealViewController;

        rvcs.performBlock = ^(SWRevealViewControllerSegue* rvc_segue, UIViewController* svc, UIViewController* dvc)
        {
            if ([segue.identifier isEqualToString:@"SegueToMapViewController"])
            {
                dvc = [MapViewController sharedMap]; //singleton
            }
            UINavigationController* nc = [[UINavigationController alloc] initWithRootViewController:dvc];
            [nc setNavigationBarHidden:YES];
            [nc setToolbarHidden:YES];
            [rvc setFrontViewController:nc animated:YES];
        };
    }
}
Pham Hoan
  • 2,107
  • 2
  • 20
  • 34

2 Answers2

0

Make sure that you don't change the map camera position in viewWillAppear or viewDidAppear.

If that's not the case, you can save the camera position before you leave the view controller and restore it when the controller is shown again.

  • I do thing about it but it's like a tricky way to fix the problem because the `GMSMapView` still reload map data, thus, it costs time and user mobile data. – Pham Hoan Apr 11 '14 at 09:50
0

It has been a while from your question, but I run into a similar problem these days and I think I found the way to do preserve the state of the GMSMapView (although I don't know if SWRevealViewController would make any change).

As you say, the idea is to have a "singleton", but instead of the controller I made it for the GMSMapView. Try to make the GMSMapView a property of the root UINavigationController by making a subclass of it and initialize the map on the ViewDidLoad of the subclass.

Interface:

#import <UIKit/UIKit.h>
#import <GoogleMaps/GoogleMaps.h>

@interface TEST1NavigationController : UINavigationController

@property GMSMapView *mapView;

@end

Class:

#import "TEST1NavigationController.h"

...

@implementation TEST1NavigationController

    - (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:40.7056308
                                                       longitude:-73.9780035
                                                            zoom:15];

    self.mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera];
    self.mapView.myLocationEnabled = YES;
}

...

Then, in each individual controller you can ask to the custom navigation controller for that GMSMapView and work with it.

#import "TESTMAP1ViewController.h"
#import "TEST1NavigationController.h"

@interface TESTMAP1ViewController ()

@end

@implementation TESTMAP1ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    //Retrieve the mapView and assign it to the main view
    self.view = [self mapView];

    // Creates a marker in the center of the map.
    GMSMarker *marker = [[GMSMarker alloc] init];
    marker.position = CLLocationCoordinate2DMake(40.7056308,-73.9780035);
    marker.title = @"Sometitle";
    marker.snippet = @"Sometext";
    marker.map = [self mapView];

}

-(GMSMapView*)mapView{
    //Returns the view from the Navigation Controller
    return [((TEST1NavigationController*)[self navigationController]) mapView];
}

...

This way the state should be preserved inside the controllers of the navigation controller.

Ruben R Aparicio
  • 663
  • 4
  • 10