循环应该运行4次,但只运行一次

时间:2014-09-15 15:34:17

标签: ios objective-c cocoa-touch

我正在制作一个基于地理定位的应用程序。这是我的代码:

-(void)gettingDataSQL {
    NSURL *url = [NSURL URLWithString:@"http://www.xxx.fr/xxx/xxx/script_xxx_localisation.php"];
    NSData *data = [NSData dataWithContentsOfURL:url];

    if (data != nil)
    {
        jsonArray = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];

        NSLog(@"jSonArrayCount = %i", [jsonArray count]);

        for (int i = 0; i<jsonArray.count; i++) {

            NSString *address = [[jsonArray objectAtIndex:i] objectForKey:@"adresse"];
            NSLog(@"address FROM JSON = %@", address);
            NSString *titre = [[jsonArray objectAtIndex:i] objectForKey:@"titre"];
            NSString *stringId = [[jsonArray objectAtIndex:i] objectForKey:@"id"];

            [geocoder geocodeAddressString:address completionHandler:^(NSArray* placemarks, NSError* error){

                // Check for returned placemarks
                if (placemarks && placemarks.count > 0) {
                    NSLog(@"if placemarks && placemarks.count");
                    CLPlacemark *topResult = [placemarks objectAtIndex:0];

                    MKPlacemark *placemark = [[MKPlacemark alloc]initWithPlacemark:topResult];
                  //  [self.mapViewOutlet addAnnotation:placemark];

                    NSLog(@"Placemark posé au longitude : %f et latitude : %f", placemark.location.coordinate.longitude, placemark.location.coordinate.latitude);

                    MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];
                    annotation.coordinate = placemark.location.coordinate;
                    annotation.title = titre;
                    annotation.subtitle = stringId;

                    [mapViewOutlet addAnnotation:annotation];


                }
            }];


            NSLog(@"one placemark well defined via retrieveData:");
        }
    }
}

对于我所做的所有NSLog,我理解for int循环被调用4次,但是循环中的整个代码都没有被调用。

循环的这一部分只被调用一次,我不明白为什么:

[geocoder geocodeAddressString:address completionHandler:^(NSArray* placemarks, NSError* error){

                    // Check for returned placemarks
                    if (placemarks && placemarks.count > 0) {
                        NSLog(@"if placemarks && placemarks.count");
                        CLPlacemark *topResult = [placemarks objectAtIndex:0];

                        MKPlacemark *placemark = [[MKPlacemark alloc]initWithPlacemark:topResult];
                      //  [self.mapViewOutlet addAnnotation:placemark];

                        NSLog(@"Placemark posé au longitude : %f et latitude : %f", placemark.location.coordinate.longitude, placemark.location.coordinate.latitude);

                        MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];
                        annotation.coordinate = placemark.location.coordinate;
                        annotation.title = titre;
                        annotation.subtitle = stringId;

                        [mapViewOutlet addAnnotation:annotation];


                    }
                }];

我想在地图上创建多个注释视图,而不仅仅是一个。我在数据库中有多个条目被下载,以便在JsonArray的循环中显示在地图上,但地理编码后的代码只被调用一次,所以它只显示我数据库的第一个条目。

任何想法的人?

提前感谢您的帮助和阅读;) 我希望它能帮助其他人也有同样的问题!

答案之后

JSON ARRAY的日志:

2014-09-15 21:24:21.544 xxxx[1416:60b] (
        {
        adresse = "115 rue de reuilly";
        cree = "2014-07-13 21:03:23";
        description = "Venez boire!";
        icone = "icone_base.png";
        id = 1;
        "id_annonceur" = 1;
        "id_type" = 3;
        lat = "";
        lon = "";
        note = 0;
        portee = 25;
        titre = "La premiere Leffe gratuite!";
        validite = "";
        vues = 0;
    },
        {
        adresse = "107 rue de reuilly";
        cree = "2014-07-13 22:21:24";
        description = "Le Match n'est pas fini, profitez-en !";
        icone = "icone_base.png";
        id = 2;
        "id_annonceur" = 1;
        "id_type" = 3;
        lat = "";
        lon = "";
        note = 0;
        portee = 25;
        titre = "Karhu a -50%";
        validite = "";
        vues = 0;
    },
        {
        adresse = "Metropole de la Gare";
        cree = "2014-07-19 15:57:51";
        description = "Premier arrive, premier servi!";
        icone = "icone_base.png";
        id = 4;
        "id_annonceur" = 1;
        "id_type" = 1;
        lat = "";
        lon = "";
        note = 0;
        portee = 25;
        titre = "Le Mojito est gratuit!";
        validite = "";
        vues = 0;
    },
        {
        adresse = "2 rue de reuilly";
        cree = "2014-07-19 15:59:12";
        description = "Depechez-vous !";
        icone = "icone_base.png";
        id = 5;
        "id_annonceur" = 1;
        "id_type" = 1;
        lat = "";
        lon = "";
        note = 0;
        portee = 25;
        titre = "La dose du Martini doublee pour 1h!";
        validite = "";
        vues = 0;
    } 

1 个答案:

答案 0 :(得分:1)

“在发起前向地理编码请求后,请勿尝试启动另一个正向或反向地理编码请求。” - 来自Apple文档的引用

https://developer.apple.com/Library/ios/documentation/CoreLocation/Reference/CLGeocoder_class/Reference/Reference.html#//apple_ref/occ/instm/CLGeocoder/geocodeAddressString:completionHandler