Revisiting Location/Venue Search - MKLocalSearch
As mentioned in a previous post about search functionality (http://thecloudshapes.com/post/127340553897/search-functionality) we have been using Foursquare API to search for Venues around the user’s location.
To make this a viable function we can integrate later into our app we have to be able to handle more than one type of search query.
For instance, the user may enter ’Starbucks’ and we could search for all business locations with that name. But they may also enter something more generic which points to the same results. So entering ‘coffee’, along with other results would also show nearby Starbucks locations.
So far to achieve this, we have been using the Foursquare API, Foursquare over time, have curated a very big database of businesses, more concentrated over some types of businesses and in certain geographic locations than others. Other options were Google Places or Facebook APIs, each with its own issues.
Google Places is unfortunately not free, not for deployment at least. Facebook on the other hand requires Facebook login, same as Foursquare. This is not so bad though. Although we have been hesitant in integrating a Foursquare login integration on our app, we were always planning on developing a Facebook login for our app, Facebook being immensely more popular than Foursquare. But there is another option available to us which we haven’t taken a look yet. Our very own Apple Places API available through MapKit. It’s free without limits to API calls unlike Google and Foursquare. However, it is only available for use for iOS. So, we have to find a way to standardize locations in our database when start getting checkins from both iOS and Android devices when we develop for Android.
At the early stage I have run into some bugs trying to integrate it into the project by replacing our search currently done by Foursquare API with MKLocalSearch from MapKit. So I created a separate test project.
I created a single view application with simply a text field and a button. The search functionality is handled by an object from each of the two classes - MKLocalSearch (lets call it MKS) and MKLocalSearchRequest (call it MKSR).
The MKSR object is allocated and instantiated. There are two fields on this object that we have to set. The first is the natural language query, which is the string that user enters (handles all the types of searches that i mentioned earlier). The second is a region which is the region which to look in. The region parameter is optional and entering no region results in a global search.
We create a MKS object and initialize it with the MKSR. These searches are asynchronous, so the method that fires the search requires a completion handler. The completion handler contains a response and an error variable. The response variable contains the results or our search, which in this case we simply NSLog in our completion block. The error contains the error which has occurred (hopefully nil). All this is code is connected to Button, so clicking the button activates the search.
And, it works. I get the following output when I search with ‘Starbucks’
2016-03-03 12:13:19.505 Local Search[8502:146934] <MKMapItem: 0x7f8949d3b9f0> { isCurrentLocation = 0; name = Starbucks; phoneNumber = "\U200e+1 (416) 531-9208"; placemark = "Starbucks, 657 Dupont St, Toronto ON M6G 1Z4, Canada @ <+43.67151050,-79.42129830> +/- 0.00m, region CLCircularRegion (identifier:'<+43.67151050,-79.42129830> radius 49.91', center:<+43.67151050,-79.42129830>, radius:49.91m)"; timeZone = "America/Toronto (EST) offset -18000"; url = "http://www.starbucks.ca"; } 2016-03-03 12:13:19.506 Local Search[8502:146934] <MKMapItem: 0x7f8949c1bfb0> { isCurrentLocation = 0; name = Starbucks; phoneNumber = "\U200e+1 (416) 535-9984"; placemark = "Starbucks, 494 Bloor St W, Toronto ON M5S 1X8, Canada @ <+43.66554130,-79.40990800> +/- 0.00m, region CLCircularRegion (identifier:'<+43.66554130,-79.40990800> radius 49.91', center:<+43.66554130,-79.40990800>, radius:49.91m)"; timeZone = "America/Toronto (EST) offset -18000"; url = "http://www.starbucks.ca"; } 2016-03-03 12:13:19.507 Local Search[8502:146934] <MKMapItem: 0x7f8949e93700> { isCurrentLocation = 0; name = Starbucks; phoneNumber = "\U200e+1 (416) 653-8181"; placemark = "Starbucks, 687 St Clair Ave W, Toronto ON M6C 1B2, Canada @ <+43.68142060,-79.42581830> +/- 0.00m, region CLCircularRegion (identifier:'<+43.68142060,-79.42581830> radius 49.91', center:<+43.68142060,-79.42581830>, radius:49.91m)"; timeZone = "America/Toronto (EST) offset -18000"; url = "http://www.starbucks.ca"; } 2016-03-03 12:13:19.508 Local Search[8502:146934] <MKMapItem: 0x7f8949ed9160> { isCurrentLocation = 0; name = Starbucks; phoneNumber = "\U200e+1 (416) 361-9705"; placemark = "Starbucks, 1-123 Front St W, Toronto ON M5J 2M2, Canada @ <+43.64490922,-79.38345507> +/- 0.00m, region CLCircularRegion (identifier:'<+43.64490922,-79.38345507> radius 14150.46', center:<+43.64490922,-79.38345507>, radius:14150.46m)"; timeZone = "America/Toronto (EST) offset -18000"; url = "http://www.starbucks.ca"; } 2016-03-03 12:13:19.536 Local Search[8502:146934] <MKMapItem: 0x7f8949edacc0> { isCurrentLocation = 0; name = Starbucks; phoneNumber = "\U200e+1 (416) 926-0166"; placemark = "Starbucks, 1 Bedford Rd, Toronto ON M5R 2B5, Canada @ <+43.66854190,-79.39717410> +/- 0.00m, region CLCircularRegion (identifier:'<+43.66854190,-79.39717410> radius 49.91', center:<+43.66854190,-79.39717410>, radius:49.91m)"; timeZone = "America/Toronto (EST) offset -18000"; url = "http://www.starbucks.ca"; } 2016-03-03 12:13:19.537 Local Search[8502:146934] <MKMapItem: 0x7f8949edcfd0> { isCurrentLocation = 0; name = Starbucks; phoneNumber = "\U200e+1 (416) 341-0101"; placemark = "Starbucks, 205 College St, Toronto ON M5T 1P9, Canada @ <+43.65855350,-79.39552950> +/- 0.00m, region CLCircularRegion (identifier:'<+43.65855350,-79.39552950> radius 49.91', center:<+43.65855350,-79.39552950>, radius:49.91m)"; timeZone = "America/Toronto (EST) offset -18000"; url = "http://www.starbucks.ca"; } 2016-03-03 12:13:19.538 Local Search[8502:146934] <MKMapItem: 0x7f8949edeca0> { isCurrentLocation = 0; name = Starbucks; phoneNumber = "\U200e+1 (416) 977-6454"; placemark = "Starbucks, 407 Yonge St, Toronto ON M5B 1S9, Canada @ <+43.65952840,-79.38207650> +/- 0.00m, region CLCircularRegion (identifier:'<+43.65952840,-79.38207650> radius 49.91', center:<+43.65952840,-79.38207650>, radius:49.91m)"; timeZone = "America/Toronto (EST) offset -18000"; url = "http://www.starbucks.ca"; } 2016-03-03 12:13:19.539 Local Search[8502:146934] <MKMapItem: 0x7f8949ee0590> { isCurrentLocation = 0; name = Starbucks; phoneNumber = "\U200e+1 (416) 934-0881"; placemark = "Starbucks, 1250 Bay St, Toronto ON M5R 2B1, Canada @ <+43.67073800,-79.39017680> +/- 0.00m, region CLCircularRegion (identifier:'<+43.67073800,-79.39017680> radius 49.91', center:<+43.67073800,-79.39017680>, radius:49.91m)"; timeZone = "America/Toronto (EST) offset -18000"; url = "http://www.starbucks.ca"; } 2016-03-03 12:13:19.540 Local Search[8502:146934] <MKMapItem: 0x7f8949ee2600> { isCurrentLocation = 0; name = Starbucks; phoneNumber = "\U200e+1 (416) 588-7979"; placemark = "Starbucks, 941 College St, Toronto ON M6H 1A6, Canada @ <+43.65342680,-79.42650130> +/- 0.00m, region CLCircularRegion (identifier:'<+43.65342680,-79.42650130> radius 49.91', center:<+43.65342680,-79.42650130>, radius:49.91m)"; timeZone = "America/Toronto (EST) offset -18000"; url = "http://www.starbucks.ca"; } 2016-03-03 12:13:19.541 Local Search[8502:146934] <MKMapItem: 0x7f8949ee49b0> { isCurrentLocation = 0; name = Starbucks; phoneNumber = "\U200e+1 (416) 483-6850"; placemark = "Starbucks, 43 Eglinton Ave E, Toronto ON M4P 1A1, Canada @ <+43.70672730,-79.39715580> +/- 0.00m, region CLCircularRegion (identifier:'<+43.70672730,-79.39715580> radius 49.91', center:<+43.70672730,-79.39715580>, radius:49.91m)"; timeZone = "America/Toronto (EST) offset -18000"; url = "http://www.starbucks.ca"; }
Now I have to integrate it into the Cloudshapes project and see what I am messing up. I am likely to post the link to the Git Repo of the Local Search project I created in the future.
https://github.com/XEQTIONR/Local-Search














