Domanda

I feel I'm being a total idiot here but I've had a total brain fart and am new to XCode. I have this code (below) to open up Google Maps for the user to be able to navigate to a specific destination depending on which button they click. The page this is on is a fixture list, with navigation an option next to the away matches, am I correct in saying that I wouldn't need a new view controller as the google maps should just pop up anyway on button press?

Also would I need to create an IBOutlet in the header to create the button press segue. Obviously I will not put this code in the - (void)viewDidLoad as I only want it to show depending on the button press

- (void)getDirections {
    CLLocationCoordinate2D start = { (startLatitude), (startLongitude) };
    CLLocationCoordinate2D end = { (endLatitude), (endLongitude) };

    NSString *googleMapsURLString = [NSString stringWithFormat:@"http://maps.google.com/?saddr=%1.6f,%1.6f&daddr=%1.6f,%1.6f", start.latitude, start.longitude, end.latitude, end.longitude];

    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:googleMapsURLString]];

}
È stato utile?

Soluzione

If I understand ok you need to open Google Maps to give directions for different matches.

First of all, you're right regarding having or not a new view controller, you don't need to create a new view controller, since you're calling

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:googleMapsURLString]];

And this should open GoogleMaps to display directions given your origin & destinations coordinates are OK. If there is no possible direction GoogleMaps still will open and will tell you no directions were found.

What you should do is:

  1. Keep an array with your matches destination coordinates.
  2. Be aware of your current coordinates (if you're trying to provide directions from your current position to the match's place).
  3. Create an IBAction to handle the buttons press action, as following:

    -(IBAction)buttonPressed:(id)sender {//YOUR CODE TO OPEN URL HERE};

  4. According to the sender you can detect what button was pressed. You have different ways to do that. One possible way is to add a tag to each button from Interface Builder and later read the tag to determine which button was pressed. Another alternative if you have a fixed amount of button, say 3 for example is to create IBOutlets for each button and compare 'sender' with your IBOutlet. Once you know which button is the sender for the buttonPressed IBAction you need to recover the coordinates associated with that button / match.

  5. Remember to link your touch up inside events on your buttons with the IBAction.
  6. If you choose to create an IBOutlet for every button remember to link the buttons with its outlet.

I hope this helps!

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top