title: Populating a map with JSON data
body: <p>I'm populating a mapkit map with data from a <code>JSON</code> web service. Each row of data has a set of coords and is added to the map. Each row also has a URL. The problem with my code is that in the annotation callout accessory button for each map is using the same URL (always the last line of data from the array). The link is the very last link from the dictionary. In the <code>NSLOG</code> output below the 4th row is the link used for each callout. Each callout should have its own URL of course. The <code>dealAnnotation.title = [currentDeal objectForKey:@"vendor"];
  </code> is displaying the correct vendor name for each map object. It's just the URL that always displays the last URL from the dictionary.</p>
  
  <p>Here's the log:</p>
  
  <pre><code>2013-02-27 11:17:35.077 link populated from map is http://www.http://www.****link1
  2013-02-27 11:17:35.078 link populated from map is http://www.http://www.****link5
  2013-02-27 11:17:35.079 link populated from map is http://www.http://www.****link3
  2013-02-27 11:17:35.079 link populated from map is http://www.http://www.****link4
  </code></pre>
  
  <p>Here's my code:</p>
  
  <p>The MKAnnotation with the right callout button method <code>pushToSafari</code></p>
  
  <pre><code>    -(MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id &lt;MKAnnotation&gt;) annotation{
  
      if ([annotation isMemberOfClass:[MKUserLocation class]]) return nil;
  
      MKPinAnnotationView *annView=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"currentloc"];
     // annView.pinColor = MKPinAnnotationColorGreen;
  
  
      UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
      [rightButton setTitle:annotation.title forState:UIControlStateNormal];
      [rightButton addTarget:self
                      action:@selector(pushToSafari)
            forControlEvents:UIControlEventTouchUpInside];
      annView.rightCalloutAccessoryView = rightButton;
  
     // annView.animatesDrop=TRUE;
      annView.canShowCallout = YES;
      annView.calloutOffset = CGPointMake(-5, 5);
  
      //add custom yd pin
      annView.image = [UIImage imageNamed:@"icon-pin-2.png"];
  
      return annView;
  }
  </code></pre>
  
  <p>the code to populate the map with data. This is the line <code>link = [currentDeal objectForKey:@"link"];
  </code> that is setting the last url from the JSON for every callout button</p>
  
  <pre><code>#pragma mark - Populate Map
  - (void)populateMap:(MKMapView*)map withDeals:(NSArray*) deals
  {
      NSLog(@"DEALS" );
      for (NSDictionary *currentDeal in deals) {
          CLLocationCoordinate2D  ctrpoint;
          ctrpoint.latitude = [[[currentDeal objectForKey:@"coords"] objectForKey:@"lat"] doubleValue];
          ctrpoint.longitude =[[[currentDeal objectForKey:@"coords"] objectForKey:@"lng"] doubleValue];
          MKPointAnnotation  *dealAnnotation   = [[MKPointAnnotation  alloc] init];
          dealAnnotation.coordinate = ctrpoint;
          dealAnnotation.title = [currentDeal objectForKey:@"vendor"];
          link = [currentDeal objectForKey:@"link"];
  
          NSLog(@"link populated from map is %@",link);
  
          NSDictionary *currDict = @{
          @"EUR": @"€",
          @"GBP": @"₤",
          @"USD": @"$",
          @"BRL": @"R$"
          };
  
          NSString *currName = [currentDeal objectForKey:@"currency"];
          NSString *currency = [currDict objectForKey:currName];
  
              dealAnnotation.subtitle = [NSString stringWithFormat:@"%@%i",currency,[[currentDeal objectForKey:@"price"] integerValue ]];
  
          NSLog(@"current deal currency sym is %@",[currentDeal objectForKey:@"id"]);
  
  
          [map setDelegate:self];
          [map addAnnotation:dealAnnotation];
      }
  }
  </code></pre>
  
  <p>the viewDidAppear: method with the JSON code:</p>
  
  <pre><code>- (void)viewDidAppear:(BOOL)animated
  {
      [super viewDidAppear:animated];
      NSLog(@"MAP VIEW APPEARED");
  
      CLLocation *location = [locationManager location];
      // Configure the new event with information from the location
      CLLocationCoordinate2D coordinate = [location coordinate];
  
      NSLog(@"%f %f",coordinate.latitude,coordinate.longitude );
      if ( (double ) coordinate.latitude == 0 &amp;&amp;  (double )  coordinate.longitude == 0 ){
          CustomAlertView *alert = [[CustomAlertView alloc]initWithTitle:@"No GPS Connection" message:@"GPS data is currently unavailable. Please ensure that Location Services are turned on in Settings." delegate:self cancelButtonTitle:@"Ok" otherButtonTitles: nil];
          [alert show];
          return;
      }
  
  
      CLLocationDegrees currentLongitude=coordinate.longitude;
      CLLocationDegrees currentLatitude=coordinate.latitude;
  
      CLLocationCoordinate2D center = CLLocationCoordinate2DMake(currentLatitude, currentLongitude);
      [mapView setCenterCoordinate:center];
      NSString *query = [NSString stringWithFormat:@"http://www.****.com/coords=45.4640,9.1916&amp;country=%@&amp;maxdistance=3000&amp;api.ofilter=track:iphone",APP_ID,lang];
      NSString *locationJsonString = [NSString
                                      stringWithContentsOfURL:[NSURL URLWithString:query]                            encoding:NSStringEncodingConversionAllowLossy|NSUTF8StringEncoding
                                      error:nil];
  
      SBJSON *parser = [[SBJSON alloc] init];
      NSDictionary *results = [parser objectWithString:locationJsonString error:nil];
      NSString *currentCity = [[[results objectForKey:@"results"] objectAtIndex:0] objectForKey:@"key"];
      NSLog(@"Current city is : %@",currentCity);
  
  
      NSString *dealSearch = [NSString stringWithFormat:@"http://****coords=45.4640,9.1916&amp;maxdistance=20&amp;api.ofilter=track:iphone",APP_ID,currentCity];
  
      NSString *dealsInCurrentLocationJsonString = [NSString stringWithContentsOfURL:[NSURL URLWithString:dealSearch] encoding:NSStringEncodingConversionAllowLossy|NSUTF8StringEncoding error:nil];
  
      //    SBJSON *parser2 = [[SBJSON alloc] init];
      NSDictionary *dealResults = [parser objectWithString:dealsInCurrentLocationJsonString error: nil];
  
      NSArray *listOfDeals = [dealResults objectForKey:@"results"];
  
      [self populateMap:mapView withDeals:listOfDeals];
  
      NSLog(@"dLongitude : %f", currentLongitude);
      NSLog(@"dLatitude : %f", currentLatitude);
  }
  </code></pre>
  
  <p>and the method for the right callout button touch:</p>
  
  <pre><code>  -(IBAction)pushToSafari {
      [[UIApplication sharedApplication] openURL:[NSURL URLWithString:link]];
  
      NSLog(@"link touched from offers around me is %@",link);
  }
  </code></pre>
  
  <p>thanks for any help</p>