google search places autocomplete specific to country in ios - ios

I am working on Google Places AutoComplete Service where i need to filter the places specific to country and found this.
It is working good untill when i pass a parameter components=country:se to filter the autocomplete field its response is REQUEST_DENIED
NSMutableString *url = [NSMutableString stringWithFormat:#"https://maps.googleapis.com/maps/api/place/autocomplete/json?input=%#&components=country:se&sensor=%#&key=%#",
[input stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],
SPBooleanStringForBool(sensor), key];
I need the autocomplete to give suggestion specific to country.
Thanks

This works fine for me.
You can set this by adding the bounds for the region by adding its extreme top left and bottom right location points of the the Geographical area.
like for USA i used.
GMSCoordinateBounds *bounds = [[GMSCoordinateBounds alloc] initWithCoordinate:CLLocationCoordinate2DMake(52.00, 148.00)
coordinate:CLLocationCoordinate2DMake(18.00, -71.00)];
GMSPlacesClient * placesClient = [[GMSPlacesClient alloc] init];
[placesClient autocompleteQuery:fieldValue bounds:bounds filter:nil
callback:^(NSArray *results, NSError *error)
{
if (error != nil)
{
NSLog(#"Autocomplete error %#", [error localizedDescription]);
return;
}
else
{
[self.autoComplete removeAllObjects];
[self.AddressPlaceID removeAllObjects];
for (GMSAutocompletePrediction* result in results)
{
CustomAddress * parseAddress = [[CustomAddress alloc] init];
NSString * adString = [parseAddress removeCountryFromAddress:result.attributedFullText.string];
[self.autoComplete addObject:adString];
[self.AddressPlaceID addObject:result.placeID];
}
[self.autoCompleteTable reloadData];
}
}];
But you can still get the other location results . It will prefer first the geographical bounds you define in the GMCoordinateBounds

Why do not use the iOS SDK to obtain country specific results? Follow this answer.

Related

Apple CLGeocoder,MKLocalSearch result differ from device map result

In my application search with the location string, it ll return the 5 result, but that not match with default device map app.
my code 1 : CLGeocoder
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder geocodeAddressString:searchKey completionHandler:^(NSArray* placemarks, NSError* error){
dispatch_async(dispatch_get_main_queue(), ^{
if (placemarks.count > 0) {
searchLocations = placemarks;
} else {
searchLocations = nil;
}
[searchTableView reloadData];
for (CLPlacemark *placemark in placemarks) {
NSLog(#"place dic %#",placemark.addressDictionary);
}
});
}];
Code 2 : MKLocalSearch
CLLocation *userLoc = (CLLocation *)[[MYLocationManager defaultManager] currentLocation];
MKLocalSearchRequest *request = [[MKLocalSearchRequest alloc] init];
request.region = MKCoordinateRegionMakeWithDistance(userLoc.coordinate, 100000, 100000);
request.naturalLanguageQuery = searchKey;
MKLocalSearch *search = [[MKLocalSearch alloc] initWithRequest:request];
[search startWithCompletionHandler:^(MKLocalSearchResponse * _Nullable response, NSError * _Nullable error) {
NSMutableArray *locs = [[NSMutableArray alloc]init];
for (MKMapItem *placeM in response.mapItems) {
[locs addObject:placeM.placemark];
}
if (locs.count > 0) {
searchLocations = locs;
} else {
searchLocations = nil;
}
dispatch_async(dispatch_get_main_queue(), ^{
[searchTableView reloadData];
});
}];
Both are return a same result
Device map app result :
Device map result differ from coding geo results. please help to solve this.
and my question is what type of search methodology use the default map application?
and how to get same result in coding ?
Please refer this answer. It describe all the steps needed to replicate the above result. I've tested this on simulator.
The result of the query depends on the region you're searching in.
That's the 'Local' in MKLocalSearch.
The location in your code example is the user's location in a region with a side length on 10km.
The region where the built in map searches should be equal to or dependant on the region you're showing in the App at this very moment.
The region in Apples App is probably very different from the explicit region in your App.

iOS GMSPlacesClient: Whether or not the search results affected by the type of Google Maps account?

I am trying to search place using GMSPlacesClient autocompleteQuery by keyword within a specified city or country. My Google Map account is Standard with enabled billing.
Code
GMSVisibleRegion visibleRegion = [GMSMapShareInstance sharedInstance].projection.visibleRegion;
GMSCoordinateBounds *bounds = [[GMSCoordinateBounds alloc] initWithCoordinate:visibleRegion.nearLeft
coordinate:visibleRegion.nearRight];
GMSAutocompleteFilter *filter = [[GMSAutocompleteFilter alloc] init];
filter.type = kGMSPlacesAutocompleteTypeFilterGeocode;
filter.country = #"US";
GMSPlacesClient *placesClient = [GMSPlacesClient sharedClient];
[placesClient autocompleteQuery:searchTerms
bounds:bounds
filter:filter
callback:^(NSArray *results, NSError *error) {
if (error != nil) {
NSLog(#"Search error: %#", [error localizedDescription]);
return;
}
arrSearchedResult = results;
[self.tblSearchResult reloadData];
}];
But it returns results that are seem not exactly. I don't know whether it's affected by account type (Standard, Premium) or not.
Anyone experienced a similar issue? Or have any way to improve search result?
Thanks in advance!
(Sorry for my English)

MKLocalSearch not working like Apple Maps search?

I have created a MapKit and trying to play around with MKLocalSearch. One thing I noticed in comparison to Apple Maps, is that mklocalsearch is restricted to 10 results. So how does Apple Maps display 15 suggestions under the search bar?
Okay, on to an example. Im trying to find "Barcelona." In Apple Maps it will be suggested after writing just "barc" and it will stay on the suggestion list throughout typing barcelona.
Now in my own Map view, I actually have to type in the full Barcelona to get the suggestion: Spain, Barcelona. On my way I get other suggestions, but nothing like Spain, Barcelona and not like Apple maps.
Any insight on how to get it working and to why Apple Maps work differently (spec. the 15 results vs 10 with mklocalseach)
Here is the code called on textField Changes:
- (IBAction)searchFieldChanged:(UITextField *)sender {
if(self.locationTextfield.text.length>0)
self.tableView.hidden = NO;
else
self.tableView.hidden = YES;
NSString *query = self.locationTextfield.text;
// Create and initialize a search request object.
MKLocalSearchRequest *request = [[MKLocalSearchRequest alloc] init];
request.naturalLanguageQuery = query;
request.region = self.mapsView.region;//we dont want region-specific search results!
//request.region = MKCoordinateRegionMakeWithDistance(self.mapsView.userLocation.location.coordinate,40000000, 15000000);
// Create and initialize a search object.
MKLocalSearch *search = [[MKLocalSearch alloc] initWithRequest:request];
// Start the search and display the results as annotations on the map.
[search startWithCompletionHandler:^(MKLocalSearchResponse *response, NSError *error)
{
[placeMarks removeAllObjects];
NSLog(#"p-count: %lu", response.mapItems.count);
for (MKMapItem *item in response.mapItems) {
[placeMarks addObject:item.placemark];
self.tempPlacemark = item.placemark;
NSLog(#"placemark: %#", item.placemark);//.location.coordinate.latitude);
}
//if(placemarks.count==0)
// appDelegate.staticPlacemark = nil;
//[self.mapsView removeAnnotations:[self.mapsView annotations]];
//[self.mapsView showAnnotations:placemarks animated:NO];
[self.tableView reloadData];
}];
}
What you do is a MKLocalSearch using a MKLocalSearchRequest. What Apple in its macOS and iOS map apps does is using the newer MKLocalSearchCompleter class to obtain autocompletion suggestions. These suggestions are used for realtime search and displayed in a UITableView. When the user selects one entry that suggestion is used to initialize a MKLocalSearchRequest to obtain detailled information about this location.

How to get Autocomplete results for places using Google Places API for iOS

I am trying to display autocomplete results using Google Places API for iOS, I might be missing something but here is my code for the task:
- (void)placeAutocomplete {
GMSVisibleRegion visibleRegion = mapView_.projection.visibleRegion;
//GMSCoordinateBounds *bounds = [[GMSCoordinateBounds alloc] initWithCoordinate:visibleRegion.farLeft
// coordinate:visibleRegion.nearRight];
GMSAutocompleteFilter *filter = [[GMSAutocompleteFilter alloc] init];
filter.type = kGMSPlacesAutocompleteTypeFilterGeocode;
GMSPlacesClient *placesClient;
[placesClient autocompleteQuery:#"indira nagar"
bounds:nil
filter:filter
callback:^(NSArray *results, NSError *error) {
if (error != nil) {
NSLog(#"Autocomplete error %#", [error localizedDescription]);
return;
}
for (GMSAutocompletePrediction* result in results) {
NSLog(#"Result '%#' with placeID %#", result.attributedFullText.string, result.placeID);
}
}];
}
I've added the pods(pod 'GoogleMaps'), I even got the map But when i try autocomplete all I am getting is (in the log)"Google Maps SDK for iOS version: 1.10.17867.0" can any one help understand how it works?
This is the issue of my api key so i had to change(create a new one) to resolve this issue

How to get GMSAddress from PlaceID or GMSPlace

I am using Autocomplete of Google Places API for iOS, and it returns placeID. How can I get GMSAddress from the placeID? I currently use lookupPlaceID to get GMSPlace, and then use reverseGeocodeCoordinate to get GMSAddress from coordinate in GMSPlace. But it request Google API 3 times: AutoComplete, lookupPlaceID, reverseGeocodeCoordinate.
Is there any way to get GMSAddress from PlaceID or GMSPlace directly?
This is a bug with 1.1.0 of the Google Maps SDK for iOS. See here.
As the documentation states here, the address property should expose a GMSAddress on GMSPlace instances.
I've just switched to using the REST API. It's not too bad to write a quick wrapper for the API and ditch the Google SDK completely. If you are using Alamofire ping me and I may be able to share what I've written.
I could get the address from placedID in one API call, here is the code:
GMSPlacesClient *placesClient = [[GMSPlacesClient alloc] init];
[placesClient lookUpPlaceID:result.placeID callback:^(GMSPlace *place, NSError *error) {
if (error != nil) {
NSLog(#"Place Details error %#", [error localizedDescription]);
return;
}
if (place != nil) {
NSString *locality = #"";
for (GMSAddressComponent *add in place.addressComponents) {
//locality
NSLog(#"%#",add.type);
NSLog(#"%#",add.name);
//type will be street_name, locality, admin_area, country,
//name will hold the corresponding value
}
} else {
NSLog(#"No place details for %#", result.placeID);
}
}];
Now for the time being, is the code below safe (enough) for this version of the SDK (1.10.5)? What is the probability that this will break in the next 6 months?
NSArray* dic = [place valueForKey:#"addressComponents"];
for (int i=0;i<[dic count];i++) {
if ([[[dic objectAtIndex:i] valueForKey:#"type"] isEqualToString:#"street_number"]) {
NSLog(#"street_number: %#",[[dic objectAtIndex:i] valueForKey:#"name"]);
}

Resources