【发布时间】:2015-08-17 08:22:43
【问题描述】:
我正在尝试基于 google place API iOS 获取当前位置,其他用于地点选择器的 API,添加位置正在调用回调方法,但是当我尝试使用以下代码获取当前位置时,它不会给出任何错误甚至调用回调方法。
_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");
}
}];
}
}
}];
【问题讨论】:
-
您等待回调被调用多久了?这需要一段时间。
-
我已经等了 5 分钟但仍然没有被调用。
-
是否请求了位置权限?另外,您使用哪种设备来测试应用程序?
-
是的,每当我在设备中安装应用程序并且我使用 iPad2 进行测试时,它都会请求许可
-
在 iOS 上,您需要在应用程序运行时(而不是在安装时)请求权限。您能否将用于请求位置权限的代码添加到上面的代码中,以便我们验证它是否正确?
标签: ios objective-c google-places-api