2013-02-27 156 views
0

我正在使用来自JSON Web服务的数据填充mapkit地图。每行数据都有一组坐标,并添加到地图中。每一行也有一个URL。我的代码的问题在于,每个地图的注释标注附件按钮使用相同的URL(始终是数组中的最后一行数据)。链接是词典的最后一个链接。在第4行下面的NSLOG输出中是用于每个标注的链接。当然,每个标注都应该有自己的URL。 dealAnnotation.title = [currentDeal objectForKey:@"vendor"]; 正在为每个地图对象显示正确的供应商名称。这只是始终显示字典中最后一个网址的网址。使用JSON数据填充地图

这里的日志:

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 

这里是我的代码:

的MKAnnotation与正确标注按钮方法pushToSafari

-(MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) 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; 
} 

代码来填充数据的地图。这是该行link = [currentDeal objectForKey:@"link"]; 被设置从JSON最后浏览的网址为每个标注按钮

#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]; 
    } 
} 

的viewDidAppear:方法与JSON代码:

- (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 && (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&country=%@&maxdistance=3000&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&maxdistance=20&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); 
} 

和正确标注按钮触摸的方法:

-(IBAction)pushToSafari { 
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:link]]; 

    NSLog(@"link touched from offers around me is %@",link); 
} 

感谢您的帮助

+0

你为什么称它为JSON?你正在处理数组和字典。数据起源于JSON的事实仅仅与此无关。 – 2013-02-27 12:04:52

+0

@HotLicks谢谢!我做了更改 – hanumanDev 2013-02-27 12:07:49

+2

查看问题时,不要带入不相关的细节,这一点很重要。很高兴知道数据是从JSON中派生出来的,因为它告诉我们字典/数组树会“很好地形成”,但是一旦你解析了JSON,就没有任何关于结果数据的特定JSON,并且将其视为JSON可能会让你感到困惑。 – 2013-02-27 13:11:17

回答

2

这line:

link = [currentDeal objectForKey:@"link"]; 

看起来像它设置了一个名为“链接”的实例变量到一个URL。所以它不是特定于任何注释。只有一个名为link的变量,所以无论它设置的最后一个值是什么。

而且这条线:

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

不检索任何特定注释或任何“链接”,只是检索“全局”的链接变量。所以任何注释都是一样的。

编辑(添加方式,使其工作):

因此,要做到这一点的方法之一是扩大MKPointAnnotation和链接属性添加到。然后,您可以添加使用

dealAnnotation.link = [currentDeal objectForKey:@"link"]; 

然后利用正确的链接:

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control 

当点击附属视图,如果它是一个UIButton或扩展UIControl东西,将被调用。然后,您将可以访问注释视图,并可以访问其注释并获取特定注释的链接。所以删除[rightButton addTarget:self ...]这一行就可以使它工作。

+0

任何想法如何获取已被触动的注释链接?我应该尝试dealAnnotation.link = [currentDeal objectForKey:@“link”];或者其他的东西?谢谢 – hanumanDev 2013-02-27 14:02:21

+1

我已经添加了一种方法使其工作。是的,您需要为每个注释设置链接,然后利用内置委托方法接收点击。如果你喜欢,请接受答案。 – Fraggle 2013-02-27 14:34:21

+0

再次感谢。我只需要弄清楚如何扩展和添加dealAnnotation.link。我将NSString指针“link”添加到MapViewController中导入的MapAnnotation.h文件中,但它尚未识别它。 – hanumanDev 2013-02-27 15:58:33