2013-02-27 275 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