Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Location Search Results overlay

I'm trying to add a Search Bar to one of my apps that lets a user enter address information, and a tableview appears with the possible results (and a keyboard). Essentially the same functionality provided in the Maps app. Is there an easy way to do this?

enter image description here

It would be nice to have a Current Location default & autocomplete as well, but not necessary. I can build the UI from scratch, but I don't know how to get back the search results. Can I use geocoder and parse the results into a tableview?

like image 411
James Weir Avatar asked Dec 28 '12 05:12

James Weir


People also ask

What is overlayview in map?

Overlays are objects on the map that are tied to latitude/longitude coordinates, so they move when you drag or zoom the map. For information on predefined overlay types, see Drawing on the map. The Maps JavaScript API provides an OverlayView class for creating your own custom overlays.

How do I create custom overlays using the maps JavaScript API?

The Maps JavaScript API provides an OverlayView class for creating your own custom overlays. The OverlayView is a base class that provides several methods you must implement when creating your overlays. The class also provides a few methods that make it possible to translate between screen coordinates and locations on the map.

How do I attach an overlay to a Google map?

Additionally, we add a toggleDOM () method, which attaches or detaches the overlay to/from the map. Note: Read the guide on using TypeScript and Google Maps. To trigger the toggle and toggleDom methods, button controls are added to the map.

What is the search property of the location interface?

The search property of the Location interface is a search string, also called a query string; that is, a string containing a '?' followed by the parameters of the URL. Modern browsers provide URLSearchParams and URL.searchParams to make it easy to parse out the parameters from the querystring. A string. See implementation notes.


1 Answers

There is a standard component for doing that, called UISearchDisplayController.

It gives you a UISearchBar and a UITableView for displaying the results. Then you can customize the content and the appearance of the table view according to your needs.

In order to get control over the actions performed by such controller you will need to conform to the UISearchDisplayDelegate protocol.

I suggest you to look carefully at the example app you can find on the doc.

EDIT

In order to implement the autocompletion features you can implement the searchDisplayController:shouldReloadTableForSearchString method of the UISearchDisplayDelegate protocol. It will be called at every character typed by the user.

Assuming that you have a CLGeocoder property called geocoder and that you are holding the placemarks into an NSArray property called placemarks, here's an example of how you can achieve a live autocompletion:

-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {      
    [self.geocoder geocodeAddressString:searchString completionHandler:^(NSArray *placemarks, NSError *error) {
            self.placemarks = placemarks;
            [self.searchDisplayController.searchResultsTableView reloadData];
    }];
    return NO;
}

Everytime the input string for the search change, you perform a forward geocoding using such string. In the completionHandler block you assign the newly found placemarks to your placemarks property and reload the table.

Please not that since the search is asynchronous you will take care of reloading the table in the completion handler and return NO in the delegate method. Returning YES will make the table to reload before the search is over, which is not the behaviour you want.

As a final remark, remember that the code I provided is minimal. In a real-world app you'd better take care of the errors the geocoder may run into, such as kCLErrorGeocodeFoundNoResult, kCLErrorGeocodeFoundPartialResult and kCLErrorGeocodeCanceled, documented here.

like image 85
Gabriele Petronella Avatar answered Sep 19 '22 10:09

Gabriele Petronella