Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

iOS App Maps

Options
  • 26-01-2015 2:46pm
    #1
    Registered Users Posts: 495 ✭✭


    Hi,

    Has anyone ever used xCode or Swift to develop an iOS app?

    The App I want to build is a MK Map with annotations but I am having trouble understanding some basic concepts that perhaps someone can assist with.

    Thanks,
    K


Comments

  • Registered Users Posts: 50 ✭✭EamonnDunne


    Green Mile wrote: »
    Hi,

    Has anyone ever used xCode or Swift to develop an iOS app?

    The App I want to build is a MK Map with annotations but I am having trouble understanding some basic concepts that perhaps someone can assist with.

    Thanks,
    K

    Yeah I've done a reasonable amount of work in xCode and MKMap, however it was in objective c (the core concepts will transfer though).


  • Registered Users Posts: 50 ✭✭EamonnDunne


    Green mile asked the following question via PM:

    "The issue I am having is that I can not get the annotations to open a new View Controller which would contain an information page on the clicked pin."

    In the spirit of sharing knowledge I'm going to respond here, in case someone else comes across this thread in the future.

    There are several different parts to getting this functionality.

    Working on the assumption that you already have your map view controller working, create the second controller for the information page. The you need to create a "segue" between them, if you are not familiar with segues read about them here apple DOT com/library/ios/referencelibrary/GettingStarted/RoadMapiOS/SecondTutorial.html

    The next thing you need to do is then capture the tap on the annotation and prepare the segue/transition to the information view. To do this you need to have the protocol defined in your map view controller like so:

    @interface MyMapViewController () <MKMapViewDelegate>

    This makes several functions available to you, the one you are interested in is
    - (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView  *)view {
        //this if statement is optional in case you have several different types of annotations being drawn on the map
        if ([view isKindOfClass:[MyAnnotationView class]]) {
            MyAnnotationView *annotation = (MyAnnotationView *) view.annotation;
          // the "toInformationView" will need to be replaced with whatever you have called your segue  
            [self performSegueWithIdentifier:@"toInformationView" sender:self];
        }
    }
    

    So that will send you to the information view on an annotation view, however it seems reasonable to assume that the information view is going to display information relevant to that particular annotation. So you need a way to link them. One way that I like is using a NSMutableDictionary to store a relationship between some model object (for the sake of argument lets call it MyInformation).

    So in your - (void)viewDidLoad method create the dictionary and populate it with the lat and lng of the annotation (in this case its a CLLocationCoordinate2D) as the key and your MyInformation model object as the value. Due to CLLocationCoordinate2D being a struct you can't use it as a dictionary key out of the box. You need to wrap it in an NSValue object like so
    CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(somelatfloatvalue, somelngfloatvalue);
    NSValue *locationValue = [NSValue valueWithMKCoordinate:coordinate]; 
    // now add it to the dictionary
     [dictionary setObject:myInfoObject forKey:locationValue];
    

    To retrieve it when the annotation is tapped modify our didSelectAnnotationView method to this:
    - (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view {
    
        if ([view isKindOfClass:[SomeAnnotationView class]]) {
            MyCustomAnnotation *annotation = (MyCustomAnnotation *) view.annotation;
            NSValue *locationValue = [NSValue valueWithMKCoordinate:annotation.coordinate];
            //the below _info property assumes you have a @property in the class of MyInformation tpye
    _info = [dictionary objectForKey:locationValue];
            [self performSegueWithIdentifier:@"toInformationView" sender:self];
        }
    }
    
    

    To pass it to the next segue, create a property of a MyInformation type in the information view and implement the following function
    
    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    
        NSString *segueIdentifier = [segue identifier];
        if ([segueIdentifier isEqualToString:@"toInformationView"]) {
            MyViewController *myViewController =(MyViewController *) [segue destinationViewController];
         
            myViewController.infoObjectProperty = _info;
          
        }
    }
    

    That should be plenty to get you on the right track. One word of caution, if you are new to iOS development you should really be starting out with objective c. There are several reasons why, however one of the most important (as demonstrated in this post) is that the vast majority of learning materials are using objective c (and frankly its not going anywhere soon).

    Post back if you have any problems


Advertisement