1

I'm using the below code to display addresses from an array (responseObject) as annotations on my mapview. It works, and the pin is dropped successfully from my location string, however it only shows a pin for the most recent address added to the array. How can I change my code so that it shows pins on the map for all addresses in my array instead of just the most recent one? Apologies if this is a newb question. Thanks!

viewcontroller.m

 NSMutableDictionary *viewParams = [NSMutableDictionary new];
    [viewParams setValue:@"u000" forKey:@"view_name"];
    [DIOSView viewGet:viewParams success:^(AFHTTPRequestOperation *operation, id responseObject) {

        self.addressData = [responseObject mutableCopy];

        NSString *location = self.addressData[0][@"address"];

        CLGeocoder *geocoder = [[CLGeocoder alloc] init];
    [geocoder geocodeAddressString:location
                 completionHandler:^(NSArray* placemarks, NSError* error){
                    if (placemarks && placemarks.count > 0) {
                         CLPlacemark *topResult = [placemarks objectAtIndex:0];
                         MKPlacemark *placemark = [[MKPlacemark alloc] initWithPlacemark:topResult];

                         MKCoordinateRegion region = self.mapView.region;
                    //     region.center = placemark.region.center;
                         region.span.longitudeDelta /= 8.0;
                         region.span.latitudeDelta /= 8.0;

                        [self.mapView setRegion:region animated:YES];
                      [self.mapView addAnnotation:placemark];

                        MKPointAnnotation *point = [[MKPointAnnotation alloc] init];
                        point.coordinate = placemark.coordinate;
                        point.title = self.addressData[0][@"users_name"];
                        point.subtitle = self.addressData[0][@"userbio"];

                        [self.mapView addAnnotation:point];
Brittany
  • 1,359
  • 4
  • 24
  • 63

1 Answers1

0

You are accessing only one object ?

NSString *location = self.addressData[0][@"address"];

Edited

  1. I think you should handle your data, separated with your view. i.e. implement geocoder related code in the mapView:viewForAnnotation: method in your map view delegate. Then you should be able to create the annotations one by one and use [self.mapView addAnnotations] for all of them
  2. For your code, which I believe is inspired by this answer, you should be able to iterate through all location addresses by something like

    for (NSMutableDictionary *loc in self.addressData) {
        NSString *loc = location[@"address"];
    
        CLGeocoder *geocoder = [[CLGeocoder alloc] init];
        ......
    }
    

    Forgive me if the syntax is wrong for Objective C.

Community
  • 1
  • 1
zc246
  • 1,514
  • 16
  • 28
  • Ok so with that line, I thought I was accessing one object (address), correct. However there are multiple address fields stored in responseObject. How then, would I write that line if I want to display all address fields on my mapview, instead of just the most recent address addition? – Brittany Nov 08 '15 at 22:54
  • You can simply iterate through all locations in your `self.addressData`, check the edited answer – zc246 Nov 09 '15 at 00:00
  • Fixed - thank you! I did however have to change the name of the mutable dictionary so it didn't match the NSString. Other than that, works like a charm. Thank you! – Brittany Nov 09 '15 at 01:20