Adding Multiple Pins to MKMapView

Discussion in 'iOS Programming' started by RagingGoat, May 5, 2013.

  1. RagingGoat macrumors 6502

    Joined:
    Jun 21, 2010
    #1
    I have a map view that shows the users current location and I want to have it show several annotations. I know how to add them individually but I'm thinking I can use an array to store the coordinates and use that array to create the annotations but I'm not sure how to store them in the array and use that array to create all of the annotations. Can anyone help?

    Here is how I'm doing it now.

    Code:
    - (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
    {
        CLLocationCoordinate2D loc = [userLocation coordinate];
        MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(loc, 700000, 700000);
        [worldView setRegion:region animated:YES];
        [locationManager stopUpdatingLocation];
        locationManager.delegate = nil;
        
        MKPointAnnotation *point = [[MKPointAnnotation alloc] init];
        point.coordinate = CLLocationCoordinate2DMake(37.7867266, -87.608209);
        point.title = @"Cates Farm";
        point.subtitle = @"Hwy 425 Henderson, KY 42420";
        [worldView addAnnotation:point];
        
        MKPointAnnotation *point1 = [[MKPointAnnotation alloc] init];
        point1.coordinate = CLLocationCoordinate2DMake(37.0703517, -88.1237899);
        point1.title = @"Broadbent B & B Foods";
        point1.subtitle = @"257 Mary Blue Road Kuttawa, KY 42055";
        [worldView addAnnotation:point1];
    }
    
     
  2. waterskier2007 macrumors 68000

    waterskier2007

    Joined:
    Jun 19, 2007
    Location:
    White Lake, MI
    #2
    You could just use the array and loop through it like

    Code:
    for (NSDictionary *dict in array){
         //use each item, assuming they are dictionaries. (but they dont have to be)
         //init a MKPointAnnotation and add it to the annotations like you were before.
    }
    
    However you could just do it when the view loads, so that its not adding the annotations every time the user moves
     
  3. Duncan C macrumors 6502a

    Duncan C

    Joined:
    Jan 21, 2008
    Location:
    Northern Virginia
    #3

    You SHOULD just build the annotations array when the view loads. That code absolutely, positively does not belong where it is now, in the did update user location method. Putting it there will cause all kinds of problems. don't do that.
     
  4. RagingGoat thread starter macrumors 6502

    Joined:
    Jun 21, 2010
    #4
    I'm not sure why I didn't put that annotation code in the viewDidLoad method to begin with but thanks for reminding me.
     
  5. RagingGoat thread starter macrumors 6502

    Joined:
    Jun 21, 2010
    #5
    So, here is what I'm trying and the app is crashing on the marketAnnotation.coordinate line.

    Code:
    - (void)viewDidLoad
    {
        [locationManager startUpdatingLocation];
        [worldView setShowsUserLocation:YES];
        [locationManager stopUpdatingLocation];
        
        CLLocationCoordinate2D cates = CLLocationCoordinate2DMake(37.7867266, -87.608209);
        NSValue *catesFarm = [[NSValue alloc]init];
        catesFarm = [NSValue valueWithMKCoordinate:cates];
        
        NSMutableArray *marketLocations = [[NSMutableArray alloc]init];
        
        NSMutableArray *coordinates = [[NSMutableArray alloc]initWithObjects:catesFarm, nil];
        
    //    NSMutableArray *latitude = [[NSMutableArray alloc]initWithObjects:@"37.7867266", @"37.0703517", @"37.1610806", @"37.318367", @"37.3559204", @"37.4154066", @"37.4757622", nil];
        
    //    NSMutableArray *longitude = [[NSMutableArray alloc]initWithObjects:@"-87.608209", @"-88.1237899", @"-87.9148629", @"-87.5074402", @"-87.5448032", @"-87.8003148", @"-87.9515986", nil];
        
        NSMutableArray *title = [[NSMutableArray alloc]initWithObjects:@"Cates Farm", @"Broadbent B & B Foods", @"Cayce's Pumpkin Patch", @"Metcalfe Landscaping", @"Brumfield Farm Market", @"Dogwood Valley Farm", @"Country Fresh Meats & Farmers Market", nil];
        
        NSMutableArray *subtitle = [[NSMutableArray alloc]initWithObjects:@"Hwy 425 Henderson, KY 42420", @"257 Mary Blue Road Kuttawa, KY 42055", @"153 Farmersville Road Princeton, KY 42445", @"410 Princeton Road Madisonville, KY 42431", @"3320 Nebo Road Madisonville, KY 42431", @"4551 State Route 109N Clay, KY 42404", @"9355 US Hwy 60 W Sturgis, KY 42459", nil];
        
        CLLocationCoordinate2D location;
        MKPointAnnotation *marketAnnotation;
        
        for (int x = 0; x <= [coordinates count]; x++)
        {
            // for (int y = 0; y <= [longitude count]; y++)
            // {
                marketAnnotation = [[MKPointAnnotation alloc]init];
                // location.latitude = [[latitude objectAtIndex:x]floatValue];
                // location.longitude = [[longitude objectAtIndex:y]floatValue];
                marketAnnotation.coordinate = [[coordinates objectAtIndex:x]MKCoordinateValue];
                marketAnnotation.title = [title objectAtIndex:y];
                marketAnnotation.subtitle = [subtitle objectAtIndex:y];
                [marketLocations addObject:marketAnnotation];
            // }
        }
        
        [worldView addAnnotations:marketLocations];
    }
    
     
  6. RagingGoat thread starter macrumors 6502

    Joined:
    Jun 21, 2010
    #6
    I also tried this, but I only get two annotation pins showing up on the map instead of the seven I'm expecting.

    Code:
    - (void)viewDidLoad
    {
        [locationManager startUpdatingLocation];
        [worldView setShowsUserLocation:YES];
        [locationManager stopUpdatingLocation];
        
        NSMutableArray *marketLocations = [[NSMutableArray alloc]init];
        
        NSMutableArray *latitude = [[NSMutableArray alloc]initWithObjects:@"37.7867266", @"37.0703517", @"37.1610806", @"37.318367", @"37.3559204", @"37.4154066", @"37.4757622", nil];
        
        NSMutableArray *longitude = [[NSMutableArray alloc]initWithObjects:@"-87.608209", @"-88.1237899", @"-87.9148629", @"-87.5074402", @"-87.5448032", @"-87.8003148", @"-87.9515986", nil];
        
        NSMutableArray *title = [[NSMutableArray alloc]initWithObjects:@"Cates Farm", @"Broadbent B & B Foods", @"Cayce's Pumpkin Patch", @"Metcalfe Landscaping", @"Brumfield Farm Market", @"Dogwood Valley Farm", @"Country Fresh Meats & Farmers Market", nil];
        
        NSMutableArray *subtitle = [[NSMutableArray alloc]initWithObjects:@"Hwy 425 Henderson, KY 42420", @"257 Mary Blue Road Kuttawa, KY 42055", @"153 Farmersville Road Princeton, KY 42445", @"410 Princeton Road Madisonville, KY 42431", @"3320 Nebo Road Madisonville, KY 42431", @"4551 State Route 109N Clay, KY 42404", @"9355 US Hwy 60 W Sturgis, KY 42459", nil];
        
        CLLocationCoordinate2D location;
        MKPointAnnotation *marketAnnotation;
        
        for (int x = 0; x <= [coordinates count]; x++)
        {
                marketAnnotation = [[MKPointAnnotation alloc]init];
                location.latitude = [[latitude objectAtIndex:x]floatValue];
                location.longitude = [[longitude objectAtIndex:x]floatValue];
                marketAnnotation.coordinate = location;
                // marketAnnotation.title = [title objectAtIndex:y];
                // marketAnnotation.subtitle = [subtitle objectAtIndex:y];
                [marketLocations addObject:marketAnnotation];
        }
        
        [worldView addAnnotations:marketLocations];
    }
    
    
     
  7. TheWatchfulOne macrumors 6502

    Joined:
    Jun 19, 2009
    #7
    According to the documentation, the coordinate property (which is declared in the MKAnnotation protocol) is read only.

    As an alternative way to set the coordinate values, you could have latitude and longitude as seperate properties and then make your implementation of the coordinate property return those values encapsulated in a CLLocationCoordinate2D.
     
  8. RagingGoat thread starter macrumors 6502

    Joined:
    Jun 21, 2010
    #8
    Here is my current code. It is crashing on the location.latitude line.

    Code:
    - (void)viewDidLoad
    {
        [locationManager startUpdatingLocation];
        [worldView setShowsUserLocation:YES];
        [locationManager stopUpdatingLocation];
        
        NSMutableArray *marketLocations = [[NSMutableArray alloc]init];
        
        NSMutableArray *lat = [[NSMutableArray alloc]initWithObjects:@"37.7867266", @"37.0703517", @"37.1610806", @"37.318367", @"37.3559204", @"37.4154066", @"37.4757622", nil];
        
        NSMutableArray *lon = [[NSMutableArray alloc]initWithObjects:@"-87.608209", @"-88.1237899", @"-87.9148629", @"-87.5074402", @"-87.5448032", @"-87.8003148", @"-87.9515986", nil];
        
        NSMutableArray *title1 = [[NSMutableArray alloc]initWithObjects:@"Cates Farm", @"Broadbent B & B Foods", @"Cayce's Pumpkin Patch", @"Metcalfe Landscaping", @"Brumfield Farm Market", @"Dogwood Valley Farm", @"Country Fresh Meats & Farmers Market", nil];
        
        NSMutableArray *subtitle1 = [[NSMutableArray alloc]initWithObjects:@"Hwy 425 Henderson, KY 42420", @"257 Mary Blue Road Kuttawa, KY 42055", @"153 Farmersville Road Princeton, KY 42445", @"410 Princeton Road Madisonville, KY 42431", @"3320 Nebo Road Madisonville, KY 42431", @"4551 State Route 109N Clay, KY 42404", @"9355 US Hwy 60 W Sturgis, KY 42459", nil];
        
        CLLocationCoordinate2D location;
        MKPointAnnotation *marketAnnotation;
        
        for (int x = 0; x <= [lat count]; x++)
        {
                marketAnnotation = [[MKPointAnnotation alloc]init];
                location.latitude = [[lat objectAtIndex:x]floatValue];
                location.longitude = [[lon objectAtIndex:x]floatValue];
                marketAnnotation.coordinate = location;
                marketAnnotation.title = [title1 objectAtIndex:x];
                marketAnnotation.subtitle = [subtitle1 objectAtIndex:x];
                [marketLocations addObject:marketAnnotation];
        }
        
        [worldView addAnnotations:marketLocations];
    }
    
     
  9. TheWatchfulOne macrumors 6502

    Joined:
    Jun 19, 2009
    #9
    What is the value of x when it crashes? Can you use the debugger to step through the code and find out? Also, can you paste the actual error here? Also, can we see the declarations for your latitude and longitude properties?
     
  10. RagingGoat thread starter macrumors 6502

    Joined:
    Jun 21, 2010
    #10
    x has a value of 7 when it crashes, which is what it should have. The error is Thread 1: signal SIGABRT.
     
  11. TheWatchfulOne macrumors 6502

    Joined:
    Jun 19, 2009
    #11
    When I count the objects in your array, I count 7 which means an index value of 7 is beyond the end of the array. An array index starts with 0.

    You might try adjusting your condition for exiting the loop. Something like this:

    Code:
     for (int x = 0; x <=[B] ([/B][lat count][B]-1)[/B]; x++)
     
  12. RagingGoat thread starter macrumors 6502

    Joined:
    Jun 21, 2010
    #12
    I adjusted it to be:

    Code:
    for (int x = 0; x < [lat count]; x++)
    
     
  13. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #13
    Code:
    for (int x = 0; [COLOR="Red"]x < [lat count][/COLOR]; x++)
    It's more common to use < rather than <= and then having to subtract 1.

    EDIT:
    Too slow. Looks like the OP already decided on that.
     
  14. RagingGoat thread starter macrumors 6502

    Joined:
    Jun 21, 2010
    #14
    Thanks for the help guys! I figured it would have to be something simple I just wasn't seeing.
     
  15. TheWatchfulOne macrumors 6502

    Joined:
    Jun 19, 2009
    #15
    It's more simple too which is actually what I normally strive for. :cool:
     
  16. RagingGoat thread starter macrumors 6502

    Joined:
    Jun 21, 2010
    #16
    Just curious, instead of me having to go find the coordinates of a bunch of addresses, is there a better way to get them and put them in?
     
  17. waterskier2007 macrumors 68000

    waterskier2007

    Joined:
    Jun 19, 2007
    Location:
    White Lake, MI
    #17
    I believe you may want to look at the documentation for MKLocalSearch which allows you to do a search with a string like "Liquor Store, Los Angeles, CA" and it will return results. You may also be able to do it with addresses. Not really sure
     

Share This Page