【问题标题】:currentplacewithcallback not being called in google place iOS apigoogle place iOS api中没有调用currentplacewithcallback
【发布时间】: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


【解决方案1】:

一种可能性:您可能没有保留对_placesClient 的引用。

单独的未完成回调不会使 GMSPlacesClient 实例保持活动状态,因此如果您不确保您的程序保留对它的引用,则可能永远不会触发回调,这与您看到的症状相匹配。

【讨论】:

    【解决方案2】:

    我不确定确切的问题,但在 iPhone6 中运行它调用回调方法后,它也可能是操作系统版本的问题。适用于 iOS 的 Google Place API 仅在最新版本的操作系统中受支持。我得到了@ZeMoon 回答的火花。

    谢谢大家的回复

    【讨论】:

    • 适用于 iOS 的 Google Places API 适用于 iOS 7.0 及更高版本。你事先用什么来测试它?
    • 是的,你是对的,我使用的是旧版本,所以它产生了问题谢谢 plexer
    【解决方案3】:

    我遇到了同样的问题,就像你一样,它似乎只适用于 iPhone 6 模拟器及更高版本,所以这可能是由于模拟设备上假定的 iOS 版本。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-14
      • 1970-01-01
      • 2017-03-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多