1

I'm relatively new to iOS dev and completely new to using the Google Maps API.

I have two separate views that I want to have a mapview. The first one is a scene that allows you to find a location on the map, enter in an address, or use your current location. In the next scene, I want to use the same location that the user input from the previous scene, but rather than the regular map I want to use the satellite view, and there will be some additional tools to manipulate the map to serve the purposes of my app.

I need to have two separate scenes, as the interfaces are very different, but I'm having trouble sending the information from one mapview to the other. Here's a few methods that I've tried(two are commented out):

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"showServiceArea"]) {
    ServiceAreaViewController *destViewController = segue.destinationViewController;
    destViewController.camera = [GMSCameraPosition cameraWithLatitude:curLocation.location.coordinate.latitude longitude:curLocation.location.coordinate.longitude zoom:17]; //Setting up a camera in the next scene using the curLocation coordinates from the current view
    //destViewController.view = _mapView;  //Setting the next view controller's view to the current mapView and changing the mapType to satellite
    //_mapView.mapType = kGMSTypeSatellite;
    //destViewController.serviceAreaMapView = _mapView;  //copying the current mapview to a mapview on the next scene
    //destViewController.serviceAreaMapView.mapType = kGMSTypeSatellite;
}

}

Every method that I've tried has thrown an exception and caused my app to crash. Here's the output from the crash of the above code:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIViewController setCamera:]: unrecognized selector sent to instance 0x17e73eb0'

For the first two commented out methods, I figured that by the time the next scene was loaded, the data I had assigned to the next view was being deleted. I'm not sure what is wrong with my current code.

Could anybody point me in the right direction to produce a mapView in a following scene using the current scene's data? Thanks.

Jake T.
  • 4,308
  • 2
  • 20
  • 48

1 Answers1

3

I dont think you should pass UI between two controllers. You should only pass the non-UI data to your second controllers. Especially your UI data are probably weak, which would probably become nil. In addition, prepareForSegue happens before your second controller's view is loaded, whatever UI data you pass to the second controller will not be set.

The correct way to pass data between is to pass non-UI data. For example, if you have a UITextView in your first controller, you should not do secondController.textView = firstController.textView;, instead, you should pass the non-UI attributes. So you need to make a NSString *myText; in your controllers, then you can do secondController.myText = self.myText; in your first controller's prepareForSegue method. When your second controller's view is loaded (your UITextView will be loaded) in viewWillLoad() or viewDidLoaded() method, you can set your UITextView with the myText data in your second controller: self.textView = myText

In your case, you should not pass the mapView from your first controller to the second controller, you should declared variables to hold your mapView in your second controller, then you can pass the non-UI mapView attribute data to the strong variables in your second controller.

ztan
  • 6,861
  • 2
  • 24
  • 44
  • Thanks, that's what I ended up doing (Passing camera coordinates and marker info into variables, then creating new objects with the same attributes in the next scene). My concern was using up additional memory for an additional map, when the map was my single biggest memory eater. Turns out that having a new mapview didn't actually increase the memory too significantly. I wonder if Google Maps has some kind of built in optimization that only creates one mapview, and just changes attributes when you swap screens? – Jake T. Feb 11 '15 at 21:28
  • I dont think two Google Maps view will be very bad for your memory, but if you really worry about memory, you can deallocate the map view in your first controller, check out this answer: http://stackoverflow.com/a/16379401/4195406 Also, why you need two Google Map Views in two different controllers instead of re rendering your map view? – ztan Feb 11 '15 at 21:46
  • I'm a novice both with obj-c/iOS and the google maps sdk, so if there's a better way, I'm just not familiar with it. Basically, I'm using one screen to enter in an address and go to that location, then when you go to the next screen, it turns into a satellite view and you can add some points around the location to mark out spots on the map. The enter address bar disappears and some new user interface buttons appear. If you know a better way to do this than passing the location coordinates along, I'd lvoe to be pointed in the right direction. – Jake T. Feb 13 '15 at 04:23
  • You might want to add a search button, when user click the button, it shows the search bar (alpha of the search bar to 1.0), when user finish entering place name, make the search bar disappear (set the alpha of the search bar to 0.0), then change the map view to satellite view animate your map view camera to that location. It is totally your design decision, I just show you one way to do it. – ztan Feb 13 '15 at 21:55