Note that there are some explanatory texts on larger screens.

plurals
  1. POCLGeocoder ever return one placemark
    text
    copied!<p>I want to revive <a href="https://stackoverflow.com/questions/10417022/how-to-get-multiple-placemarks-from-clgeocoder">this</a> and <a href="https://stackoverflow.com/questions/11344188/clgeocoder-only-returning-one-placemark">this</a> question because the problem still persists for me, so I'm writing a new question.</p> <p>This is my code:</p> <pre><code>- (SVGeocoder*)initWithParameters:(NSMutableDictionary*)parameters completion:(SVGeocoderCompletionHandler)block { self = [super init]; self.operationCompletionBlock = block; Class cl = NSClassFromString(@"CLGeocoder"); if (cl != nil) { if (self.geocoder_5_1 == nil) { self.geocoder_5_1 = [[cl alloc] init]; } NSString *address = [parameters objectForKey:kGeocoderAddress]; [self.geocoder_5_1 geocodeAddressString:address completionHandler:^(NSArray *placemarks, NSError *error) { NSMutableArray *svplacemarks = [NSMutableArray arrayWithCapacity:1]; SVPlacemark *placemark; NSLog(@"placemarks[count] = %i", [placemarks count]); for (CLPlacemark *mark in placemarks) { placemark = [[SVPlacemark alloc] initWithPlacemark:mark]; [svplacemarks addObject:placemark]; } self.operationCompletionBlock([NSArray arrayWithArray:svplacemarks],nil,error); }]; } else { self.operationRequest = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://maps.googleapis.com/maps/api/geocode/json"]]; [self.operationRequest setTimeoutInterval:kSVGeocoderTimeoutInterval]; [parameters setValue:@"true" forKey:kGeocoderSensor]; [parameters setValue:[[NSLocale currentLocale] objectForKey:NSLocaleLanguageCode] forKey:kGeocoderLanguage]; [self addParametersToRequest:parameters]; self.state = SVGeocoderStateReady; } return self; } </code></pre> <p>It is my personal version (quite rough) of SVGeocoder using CLGeocoder for forward geocoding with retrocompatibility for iOS &lt; 5.1</p> <p>I use this solution because of the Google terms which prevent the use of the maps API without showing the result on a Google map.</p> <p>The problem is the same one from the previously mentioned questions: CLGeocoder returns only one placemark and the log prints a nice</p> <blockquote> <p>"placemarks[count] = 1".</p> </blockquote> <p>My question is, does anyone know if there is another way to retrieve forward geocoding, or some other magic thing (the Apple map app shows multiple markers for the same query I do, "via roma", for example) ?</p> <hr> <p>EDIT FOR ROB'S SOLUTION</p> <pre><code>Class mkLocalSearch = NSClassFromString(@"MKLocalSearch"); if (mkLocalSearch != nil) { NSString *address = [parameters objectForKey:kGeocoderAddress]; MKLocalSearchRequest *request = [[MKLocalSearchRequest alloc] init]; request.region = MKCoordinateRegionForMapRect(MKMapRectWorld); request.naturalLanguageQuery = address; MKLocalSearch *localsearch = [[MKLocalSearch alloc] initWithRequest:request]; [localsearch startWithCompletionHandler:^(MKLocalSearchResponse *response, NSError *error) { NSMutableArray *svplacemarks = [NSMutableArray arrayWithCapacity:1]; SVPlacemark *placemark; NSLog(@"response.mapItems[count] = %i", [response.mapItems count]); for (MKMapItem *item in response.mapItems) { placemark = [[SVPlacemark alloc] initWithPlacemark:item.placemark]; [svplacemarks addObject:placemark]; } self.operationCompletionBlock([NSArray arrayWithArray:svplacemarks],nil,error); }]; } </code></pre> <p>This is an interesting solution that gives another point of view. Unfortunately, even if I set the region to worldwide, I still get a nice log</p> <blockquote> <p>response.mapItems[count] = 1</p> </blockquote> <p>The query was "via roma", which is a very common street name in Italy, so much so that I think we can find it in practically any Italian city.</p> <p>Maybe I'm doing something wrong?</p> <hr> <p>EDIT 2 - New Test:</p> <p>convert World Rect to CLRegion, code from <a href="https://gist.github.com/esromneb/3402711" rel="nofollow noreferrer">here</a></p> <pre><code> NSString *address = [parameters objectForKey:kGeocoderAddress]; // make a conversion from MKMapRectWorld to a regular CLRegion MKMapRect mRect = MKMapRectWorld; MKMapPoint neMapPoint = MKMapPointMake(MKMapRectGetMaxX(mRect), mRect.origin.y); MKMapPoint swMapPoint = MKMapPointMake(mRect.origin.x, MKMapRectGetMaxY(mRect)); float ewDelta= neMapPoint.x - swMapPoint.x; float nsDelta= swMapPoint.y - neMapPoint.y; MKMapPoint cMapPoint = MKMapPointMake(ewDelta / 2 + swMapPoint.x, nsDelta / 2 + neMapPoint.y); CLLocationCoordinate2D neCoord = MKCoordinateForMapPoint(neMapPoint); CLLocationCoordinate2D swCoord = MKCoordinateForMapPoint(swMapPoint); CLLocationCoordinate2D centerCoord = MKCoordinateForMapPoint(cMapPoint); CLLocationDistance diameter = [self getDistanceFrom:neCoord to:swCoord]; // i don't have the map like showed in the example so i'm trying to center the search area to the hypothetical center of the world CLRegion *clRegion = [[CLRegion alloc] initCircularRegionWithCenter:centerCoord radius:(diameter/2) identifier:@"worldwide"]; [self.geocoder_5_1 geocodeAddressString:address inRegion: clRegion completionHandler:^(NSArray *placemarks, NSError *error) { NSMutableArray *svplacemarks = [NSMutableArray arrayWithCapacity:1]; SVPlacemark *placemark; NSLog(@"placemarks[count] = %i", [placemarks count]); for (CLPlacemark *mark in placemarks) { placemark = [[SVPlacemark alloc] initWithPlacemark:mark]; [svplacemarks addObject:placemark]; } self.operationCompletionBlock([NSArray arrayWithArray:svplacemarks],nil,error); }]; </code></pre> <p>... and I get the usual "placemark [count] = 1"</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload