iphone - 使用 JSON 数据填充 map

标签 iphone ios objective-c mapkit

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

这是日志:

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

用数据填充 map 的代码。这是行 link = [currentDeal objectForKey:@"link"]; 为每个标注按钮设置 JSON 中的最后一个 url

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

带有 JSON 代码的 viewDidAppear: 方法:

- (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);
}

谢谢你的帮助

最佳答案

这一行:

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

看起来它将一些名为“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 

如果附件 View 是 UIButton 或扩展 UIControl 的东西,则在单击附件 View 时将调用它。然后您将有权访问注释 View 并可以访问它的注释并获取该特定注释的链接。因此,删除 [rightButton addTarget:self...] 行以使其正常工作。

关于iphone - 使用 JSON 数据填充 map ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15111180/

相关文章:

iphone - GCD : How to write and read to variable from two threads

ios - 在 Objective-C 中处理闭包中的快速抛出

iphone - 钛作为移动开发平台的主要局限性是什么?

iphone - 由于 wifi 没有互联网,让 iPhone 应用程序使用运营商数据

ios - 如何从我的应用程序打开 IOS Linkedin 学习应用程序

ios - 是否可以更改 QLPreviewController 的背景颜色?

ios - 如何在同一个 UILabel 中添加多行

ios - 使用 API 推送通知

iphone - Flash 编程 (Adobe Air) 与 Objective C 对比?

ios - 如何在 Objective C 中单击按钮时更新 UILabel 的 centerXAnchor