Objective-C - search for streets by user request

I want to allow users to search for the street name and display the results in a UITableView. At the moment, the region is not important, it can be from any region.

I could not find a suitable example in my searches, and I do not know whether to use CLLocation or MKLocalSearch.

Based on the docs, I should use MKLocalSearch:

Although local search and geocoding are similar, they support various use cases. Use geocoding if you want to convert between map coordinates and a structured address, such as an address book address. Use local search if you want to find a set of places that match user input.

But I tried both methods, and it gives me only 1 result (even if there is an NSArray.

This is the CLGeocoder approach:

CLGeocoder *geocoding = [[CLGeocoder alloc] init]; [geocoding geocodeAddressString:theTextField.text completionHandler:^(NSArray *placemarks, NSError *error) { if (error) { NSLog(@"%@", error); } else { NSLog(@"%i", [placemarks count]); for(CLPlacemark *myStr in placemarks) { NSLog(@"%@", myStr); } } }]; 

And this is my attempt at MKLocalSearch:

 MKLocalSearchRequest *request = [[MKLocalSearchRequest alloc] init]; request.naturalLanguageQuery = theTextField.text; request.region = self.region; localSearch = [[MKLocalSearch alloc] initWithRequest:request]; [localSearch startWithCompletionHandler:^(MKLocalSearchResponse *response, NSError *error){ if (error != nil) { [[[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Map Error",nil) message:[error localizedDescription] delegate:nil cancelButtonTitle:NSLocalizedString(@"OK",nil) otherButtonTitles:nil] show]; return; } if ([response.mapItems count] == 0) { [[[UIAlertView alloc] initWithTitle:NSLocalizedString(@"No Results",nil) message:nil delegate:nil cancelButtonTitle:NSLocalizedString(@"OK",nil) otherButtonTitles:nil] show]; return; } self.streets = response; [self.streetsTableView reloadData]; }]; 

In some cases, MKLocalSearch returns more than 1 answer, but they are associated with searches for places where street names are not used.

Thanks in advance.

+8
objective-c xcode cllocation mklocalsearch
source share
3 answers

This is the closest I could get. This is due to the use of google places API Web Service .

Note. Perhaps you can use your Google Maps API, etc. I am sure there are other ways to get this information from various Google APIs.

  NSURL *googlePlacesURL = [NSURL URLWithString:[NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/place/autocomplete/json?input=%@&location=%f,%f&sensor=true&key=API_KEY", formattedSearchText, location.coordinate.latitude, location.coordinate.longitude]]; 

The answer is a JSON object. Convert it to a dictionary.

  NSDictionary *response = [NSJSONSerialization JSONObjectWithData:_googlePlacesResponse options:NSJSONReadingMutableContainers error:&error]; if([[response objectForKey:@"status"] isEqualToString:@"OK"]) { NSArray *predictions = [response objectForKey:@"predictions"]; for(NSDictionary *prediction in predictions) { NSArray *addressTypes = [prediction objectForKey:@"types"]; if([addressTypes containsObject:@"route"]) { //This search result contains a street name. //Now get the street name. NSArray *terms = [prediction objectForKey:@"terms"]; NSDictionary *streetNameKeyValuePair = [terms objectAtIndex:0]; NSLog(@"%@",[streetNameKeyValuePair objectForKey@"value"]); } } } 

Possible types seem

Route → Name of the street Location → Name of the city / place Political → State, etc. Geocode → lat / long

You can populate the table view with those predictions that ONLY contain route as the address type. That might work.

+4
source share

CLGeocoder simply returns the address format. Put this in your code and play with the contents of mapitems

 MKLocalSearchRequest *request = [MKLocalSearchRequest new]; request.naturalLanguageQuery = @"Pizza"; request.region = MKCoordinateRegionMake(location.coordinate, MKCoordinateSpanMake(.01, .01)); MKLocalSearch *search = [[MKLocalSearch alloc]initWithRequest:request]; [search startWithCompletionHandler:^(MKLocalSearchResponse *response, NSError *error) { NSArray *mapItems = response.mapItems; for (MKMapItem *mapItem in mapItems) { MKPointAnnotation *point = [MKPointAnnotation new]; point.coordinate = mapItem.placemark.coordinate;` } }]; 
+2
source share

The returned array contains mapItems , you can mapItems over the array to pull out all the mapItems elements as follows:

 myMatchingItems = [[NSMutableArray alloc] init]; for (MKMapItem *item in response.mapItems){ [myMatchingItems addObject:item]; } 

Each mapItem.placemark.thoroughfare contains a street of the location found.

+1
source share

All Articles