0

I have written following code to find out the coordinates of list of countries.

  int count=[objCountries.countryName count];
CLGeocoder *geoCode = [[CLGeocoder alloc] init];
for(int i=0;i<count;i++)
{
    NSString *strCountry=[[NSString alloc]initWithString:[objCountries.countryName objectAtIndex:i]];




        [geoCode geocodeAddressString:strCountry completionHandler:^(NSArray *placemarks, NSError *error)
    {
        if (!error)
        {
            CLPlacemark *place = [placemarks objectAtIndex:0];
            CLLocation *location = place.location;
            CLLocationCoordinate2D coord = location.coordinate;

            NSString *tempLati=[[NSString alloc]initWithFormat:@"%g",coord.latitude];
            NSString *tempLongi=[[NSString alloc]initWithFormat:@"%g",coord.longitude];
            NSLog(@"-------------------------");
            NSLog(@"Country : %@",strCountry);
            NSLog(@" Latitude : %@ ",tempLati);
            NSLog(@" Longitude : %@ ",tempLongi);

            [objCountries.countryLatitude addObject:tempLati];
            [objCountries.countryLongitude addObject:tempLongi];
            [db insertAsiaCountry:strCountry :tempLati :tempLongi];
        }
    }];

}

}

In my countryName array there 20 objects available

Problem: It's working Fine first time only. But Second Time when for loop executing [geocode geo....] method is not calling. I Can't understand what to do ? Please Help. Thank You Sir.

Will Smith
  • 339
  • 1
  • 4
  • 13

3 Answers3

1

after this line..

    [db insertAsiaCountry:strCountry :tempLati :tempLongi];

add

[geoCode cancelGeocode];

even if error occurs try allocating geoCoder wihtin the 'for' loop

AppleDelegate
  • 4,269
  • 1
  • 20
  • 27
0

You cant call any other Method in for loop because loop doesn't wait for until the work is finished. In your code you're calling geoCode method in for loop that is wrong. Try to call that method where your first method is being finished.

TheTiger
  • 13,264
  • 3
  • 57
  • 82
  • wait_fences: failed to receive reply: 10004003 is coming. it's my first map application so i can't understand. What to do ? Please Help. can i do this task by another way? How? – Will Smith Sep 27 '12 at 07:50
  • answer modified with link to related question – SPA Sep 27 '12 at 09:00
  • First remove your loop try in with once with break points ... and check which is last method called when your method work is finished ... – TheTiger Sep 27 '12 at 09:12
0

CLGeocoder is not intended to be used in a batch fashion as you are trying, so it is possible that subsequent requests are being rejected by the Apple servers. Have a look at the CLGeocoder reference documentation for guidelines on how to use this.

Copy and paste this data need-a-list-of-all-countries-in-the-world into a spreadsheet and save it as a csv. Add this file to the project. Then your app reads the file and creates the annotations as an array which you then add to the mapview.

SPA
  • 1,279
  • 8
  • 13
  • I want to show pin on all the countries of the world. can i use another method to perform above task? Please Give me Some hint. – Will Smith Sep 27 '12 at 07:53
  • there's a data file available you can use - see link in my answer – SPA Sep 27 '12 at 09:17