I am trying to get current place based on google place API iOS, other API for place picker, add place is calling callback methods, but when i am trying to get current place with below code, it not giving any error or not even calling callback method.
_placesClient = [GMSPlacesClient sharedClient];
[_placesClient currentPlaceWithCallback:^(GMSPlaceLikelihoodList *likelihoodList, NSError *error) {
if (error != nil) {
NSLog(#"Current Place error %#", [error localizedDescription]);
return;
}
int cnt =0;
for (GMSPlaceLikelihood *likelihood in likelihoodList.likelihoods) {
cnt++;
if(cnt==1)
{
GMSPlace* place = likelihood.place;
NSLog(#"Current Place name %# at likelihood %g", place.name, likelihood.likelihood);
NSLog(#"Current Place address %#", place.formattedAddress);
NSLog(#"Current Place attributions %#", place.attributions);
NSLog(#"Current PlaceID %#", place.placeID);
CLLocationCoordinate2D center = CLLocationCoordinate2DMake(place.coordinate.latitude,place.coordinate.longitude);
// CLLocationCoordinate2D center = CLLocationCoordinate2DMake([lat doubleValue],[lng doubleValue]);
CLLocationCoordinate2D northEast = CLLocationCoordinate2DMake(center.latitude + 0.001, center.longitude + 0.001);
CLLocationCoordinate2D southWest = CLLocationCoordinate2DMake(center.latitude - 0.001, center.longitude - 0.001);
GMSCoordinateBounds *viewport = [[GMSCoordinateBounds alloc] initWithCoordinate:northEast
coordinate:southWest];
GMSPlacePickerConfig *config = [[GMSPlacePickerConfig alloc] initWithViewport:viewport];
_placePicker = [[GMSPlacePicker alloc] initWithConfig:config];
[_placePicker pickPlaceWithCallback:^(GMSPlace *place, NSError *error) {
if (error != nil) {
NSLog(#"Pick Place error %#", [error localizedDescription]);
}
if (place != nil) {
[_placesClient reportDeviceAtPlaceWithID:place.placeID];
NSLog(#"Place name %#", place.name);
NSLog(#"Place address %#", place.formattedAddress);
NSLog(#"Place placeID %#", place.placeID);
NSLog(#"Place number %#", place.phoneNumber);
NSLog(#"Place website %#", place.website);
self.placeInfo = [[SHPlaceInfo alloc]init];
self.placeInfo.address=place.formattedAddress;
self.placeInfo.phnNumber=place.phoneNumber;
self.placeInfo.website = [NSString stringWithFormat:#"%#",place.website];
self.placeInfo.idStr = place.placeID;
self.placeInfo.lat =[NSString stringWithFormat:#"%f", place.coordinate.latitude];
self.placeInfo.lng =[NSString stringWithFormat:#"%f", place.coordinate.longitude];
self.placeInfo.type = [place.types componentsJoinedByString:#","];
self.placeInfo.name = place.name;
[self savePlaceDatainDatabase:self.placeInfo];
// [self getNearDetailPlaces];
//if place id gives phone number then need to use below flow and remove api call above getNearDetailPlaces
selectedPlaceDetailViewController *placeDetailVC=[self.storyboard instantiateViewControllerWithIdentifier:#"selectedPlaceDetailViewController"];
placeDetailVC.placeInfo = self.placeInfo;
placeDetailVC.strCalledFromPage=#"Home";
[self.navigationController pushViewController: placeDetailVC animated:YES];
} else {
NSLog(#"No place selected");
}
}];
}
}
}];
One possibility: you might not be retaining a reference to _placesClient.
The outstanding callback alone won't keep the GMSPlacesClient instance alive, so if you don't ensure your program keeps a reference to it the callback might never be fired, which matches the symptom you see.
I am not sure with exact issue, but after running this in iPhone6 its calling that callback method,it can be issue of OS version also.Google Place API for iOS is only supported in latest version of OS.and i have got this spark from #ZeMoon's answer.
Thank you all for reply
I had the same issue, and just like you it seems to work only on iPhone 6 simulator and up, so it is probably due to the assumed iOS version on the simulated devices.
Related
I'm using Google places api for iOS.I've implemented current place feature. Here's the code(it's same as google provided in guide).
[_placesClient currentPlaceWithCallback:^(GMSPlaceLikelihoodList *likelihoodList, NSError *error) {
if (error != nil) {
NSLog(#"Current Place error %#", [error localizedDescription]);
return;
}
for (GMSPlaceLikelihood *likelihood in likelihoodList.likelihoods) {
GMSPlace* place = likelihood.place;
NSLog(#"Current Place name %# at likelihood %g", place.name, likelihood.likelihood);
NSLog(#"Current Place address %#", place.formattedAddress);
NSLog(#"Current Place attributions %#", place.attributions);
NSLog(#"Current PlaceID %#", place.placeID);
}
}];
but it takes about 3-4 minutes to execute the callback. I've followed all the steps they provided in guide.
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
I am working on a project which can fetch the list of nearby places (from my current place). I use Google Places API and what I have tried is shown below. I see new view which has a mapview with mark position of most prominent places and table which contains list of these places. I need to fetch list of the places so that I can render it on my tableview.
- (IBAction)pickPlace:(UIButton *)sender {
CLLocationCoordinate2D center = CLLocationCoordinate2DMake(51.5108396, -0.0922251);
CLLocationCoordinate2D northEast = CLLocationCoordinate2DMake(center.latitude + 0.001, center.longitude + 0.001);
CLLocationCoordinate2D southWest = CLLocationCoordinate2DMake(center.latitude - 0.001, center.longitude - 0.001);
GMSCoordinateBounds *viewport = [[GMSCoordinateBounds alloc] initWithCoordinate:northEast coordinate:southWest];
GMSPlacePickerConfig *config = [[GMSPlacePickerConfig alloc] initWithViewport:viewport];
_placePicker = [[GMSPlacePicker alloc] initWithConfig:config];
[_placePicker pickPlaceWithCallback:^(GMSPlace *place, NSError *error) {
if (error != nil) {
NSLog(#"Pick Place error %#", [error localizedDescription]);
return;
}
if (place != nil) {
NSLog(#"Place name %#", place.name);
NSLog(#"Place address %#", place.formattedAddress);
NSLog(#"Place attributions %#", place.attributions.string);
} else {
NSLog(#"No place selected");
}
}];
}
Then you shouldn't use GMSPlacePicker, because it offers the UI to you for your user to make a single selection. If you want a list of places that you can display yourself then you should use the REST interface provided by google to get the required details and update your UI.
It is doable to get list of nearby places (from my current place) with Google Places API. I used the following method to achieve the same:
[GMSPlaceClient currentPlaceWithCallback:]
An array of nearby places with likelihood is returned.
I have an array of coordinates that I step through with a for loop. I would like to place annotations on a map for each location and have the subtitle for the callout be the address of the coordinate, found by using reverseGeocodeLocation In the for loop, I call the reverseGeocodeLocation method, and inside the completion block I create the annotation and display it on the map. However, when I run the app, only one annotation shows up. I went in the debugger, and the completion block is only getting called once (for two calls to the reverseGeocodeLocation method). Any suggestions to fix this?
My for loop:
for(int i = 0; i < [locations count]; i++)
{
CLLocation *location = [locations objectAtIndex:i];
__block NSString *info;
NSLog(#"Resolving the Address");
[geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error)
{
NSLog(#"Found placemarks: %#, error: %#", placemarks, error);
if (error == nil && [placemarks count] > 0)
{
placemark = [placemarks lastObject];
info = [NSString stringWithFormat:#"%# %# %# %#, %#",
placemark.subThoroughfare, placemark.thoroughfare,
placemark.postalCode, placemark.locality,
placemark.administrativeArea];
[self remainderOfMethod:location withAddress:info atIndex:i];
}
else
{
NSLog(#"%#", error.debugDescription);
}
} ];
}
And the method called at the completion block:
- (void) remainderOfMethod: (CLLocation *)location withAddress:(NSString *)info atIndex: (int)i
{
MKPointAnnotation* annotation = [[MKPointAnnotation alloc] init];
if (location != nil)
{
[annotation setSubtitle:[NSString stringWithFormat:#"%#", info]];
annotation.coordinate = location.coordinate;
[self.mapView addAnnotation:annotation];
}
}
Thanks!
From the official Apple documentation:
After initiating a reverse-geocoding request, do not attempt to
initiate another reverse- or forward-geocoding request
You can find the docs here: https://developer.apple.com/library/ios/documentation/CoreLocation/Reference/CLGeocoder_class/Reference/Reference.html#//apple_ref/occ/instm/CLGeocoder/reverseGeocodeLocation:completionHandler:
One way to solve it is to do only one request at a time in a recursive method that pops a location from a stack (or array) on each iteration.
Even in that case, consider what Apple has to say about it:
Geocoding requests are rate-limited for each app, so making too many
requests in a short period of time may cause some of the requests to
fail
So you may want to request geocoding on demand, for example when a user taps on an annotation.
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.