I want to revive this and this question because the problem is still persisting for me writing a new question.
This is my 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; }
This is my personal version (rather crude) of SVGeocoder using CLGeocoder for direct geocoding with retro-resistance for iOS <5.1
I use this solution because of Googleβs conditions that prevent the use of the map APIs without displaying the result on a Google map.
The problem is the same as in the previous questions: CLGeocoder returns only one label, and the journal prints nice
"tags [count] = 1".
My question is, does anyone know if there is another way to get preliminary geocoding or some other magic thing (Apple map application shows several markers for the same request, for example "via roma")?
EDIT FOR ROB SOLUTIONS
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); }]; }
This is an interesting solution that gives a different point of view. Unfortunately, even if I set a region around the world, I still get a good magazine
response.mapItems [count] = 1
The request was βvia romaβ, which is a very common street name in Italy, so much so that I think we can find it in almost any Italian city.
Maybe I'm doing something wrong?
EDIT 2 - new test:
convert World Rect to CLRegion, code from here
NSString *address = [parameters objectForKey:kGeocoderAddress];
... and I get the usual "label [count] = 1"