0

I am making an application based on geolocalisation. Here is my code :

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

With all the NSLogs I did, I understood that the for int loop is called 4 times as it is supposed to, but the entire code in the loop is not called.

This part of the loop is called only once and I don't get why :

[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];


                    }
                }];

I would like to create multiple annotation views on the map and not only one. I have multiple entries in databases that are downloaded in order to be shown on the Map in the loop for JsonArray but the code after the geocoding is called only once so it shows only the first entry of my database.

Any idea guys ?

Thanks in advance for your help and reading ;) I hope it will help someone else having the same problem too !

AFTER ANSWERS

LOG OF THE 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 Answers1

1

"After initiating a forward-geocoding request, do not attempt to initiate another forward- or reverse-geocoding request." - Quote from Apple Documentation

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

dostrander
  • 737
  • 5
  • 19
  • Thanks for your answer. So how could I solve my problem of placing many different annotations on the map ? –  Sep 15 '14 at 19:32