Question

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.

Was it helpful?

Solution

after this line..

    [db insertAsiaCountry:strCountry :tempLati :tempLongi];

add

[geoCode cancelGeocode];

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

OTHER TIPS

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.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top