Search code examples
xcodexcode4nsstringxcode4.2cllocation

How do I pass both latitude and longitude coordinates on button press and drop a pin on next map view (between three views)?


I am seriously struggling on something that has kept me for hours if not days. It'd be awesome if someone can offer input. I figured out how to pass data from a button press to another view, but what I am now trying to do is send longitude and latitude coordinates to the next view as an NSString. Is that possible as an NSString?

I realize at the moment it doesn't work because I've already associates CLLocationCoordinate2D with 'location,' but what else can I do now?

Here's my code:

if ([mlabel.text isEqualToString: @" Arts "])

   {
         CLLocationCoordinate2D location;
         location.latitude = (double) 44.4758;
         location.longitude = (double) -73.2125;

         viewController.stringToDisplay = location;

   }

Solution

  • After my post of this question, I implemented an additional view for a total of 3 and so my initial question is different. As of now, my initial view has a category called Arts. The Arts view is reached with the following:

    My first view, as an example, uses the below to open my "Arts" view.

    -(void)mainButton
    {
      if ([mlabel.text isEqualToString: @" Arts " ])
      {
        [self performSegueWithIdentifier:@"Arts" sender:self];
      }
    }
    

    Once at my Arts view, I have my coordinates for my Arts venue to drop in my third view (map) which look like this:

    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
    {
      FlipsideViewController *viewController = segue.destinationViewController;
      viewController.delegate = self;
    
      NSString *str;
    
         if ([[segue identifier] isEqualToString:@"showAlternate"]) 
         {
          [[segue destinationViewController] setDelegate:self];
         }
    
         if ([mlabel.text isEqualToString: @" Burlington City Arts "])     
         {
          CLLocationCoordinate2D location;
          location.latitude = (double) 44.476625;
          location.longitude = (double) -73.212827;
    
          str = [NSString stringWithFormat:@"%.2f, %.2f", location.latitude, location.longitude];
    
          viewController.stringToDisplay = str;
         }
    }
    

    What is happening in the above code is that I am declaring stringToDisplay to store the coordinates and send it to the third view. I use a label and say that if my label from a pickerview I have equals the phrase "Burlington City Arts", then to send the coordinates from that particular place to my third view via my "str" NSString.

    Finally, the code for my third view for that place I want to drop a pin on:

    if ([self.stringToDisplay isEqualToString: @"44.48, -73.21"])
    
    {
        location.latitude = (double) 44.476625;
        location.longitude = (double) -73.212827;
    
        MapViewAnnotation *newAnnotation = [[MapViewAnnotation alloc] initWithTitle:@"newAnnotation" andCoordinate:location];
        [self.mapView addAnnotation:newAnnotation];
        [mapView setCenterCoordinate:location animated:YES];
    }
    

    My stringToDisplay transmits the str NSString with the coordinates to the map view (third view) in the above code, however, the values were rounded and so what I did was I took the rounded figures and put those in my IF statement. Afterwards, I specified the exact coordinates again for the pin to be dropped on. Ta-da!

    *****UPDATE*****
    

    Rather than reiterating coordinates in views 2 and 3, I realize now that the best is to just re-declare the label for each venue, and to pass that label name to the third view so there is no counfusion with coordinates. Those coordinates are specified only once in the third map view which is fine. I pass the label name in my stringToDisplay string, like this:

    if ([mlabel.text isEqualToString: @" The S.P.A.C.E. Gallery "])
            {
                viewController.stringToDisplay = @" The S.P.A.C.E. Gallery ";
    
            }