【问题标题】:Google Places API returning no results?Google Places API 没有返回结果?
【发布时间】:2013-10-22 18:15:53
【问题描述】:

我正在关注本教程:http://www.raywenderlich.com/13160/using-the-google-places-api-with-mapkit,但由于某种原因,我的应用正在返回:

谷歌数据:( )

这是我的 .h 文件:

#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>

#define kBgQueue dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)

#define kGOOGLE_API_KEY @"API PLACED HERE, LEFT BLANK FOR STACKOVERFLOW"

@interface ViewController : UIViewController <MKMapViewDelegate,     CLLocationManagerDelegate>
{
CLLocationManager *locationManager;
CLLocationCoordinate2D currentCentre;
int currenDist;
}

@property (strong, nonatomic) IBOutlet MKMapView *mapView;
@end

还有我的实现文件:

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
[super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

//Make this controller the delegate for the map view.
self.mapView.delegate = self;

// Ensure that you can view your own location in the map view.
[self.mapView setShowsUserLocation:YES];

//Instantiate a location object.
locationManager = [[CLLocationManager alloc] init];

//Make this controller the delegate for the location manager.
[locationManager setDelegate:self];

//Set some parameters for the location object.
[locationManager setDistanceFilter:kCLDistanceFilterNone];
[locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)toolBarButtonPress:(UIBarButtonItem *)sender {
UIBarButtonItem *button = (UIBarButtonItem *)sender;
NSString *buttonTitle = [button.title lowercaseString];
[self queryGooglePlaces:buttonTitle];

}

-(void) queryGooglePlaces: (NSString *) googleType {
NSString *url = [NSString     stringWithFormat:@"https://maps.googleapis.com/maps/api/place/search/json?  location=%f,%f&radius=%@&types=%@&sensor=true&key=%@", currentCentre.latitude, currentCentre.longitude, [NSString stringWithFormat:@"%i", currenDist], googleType,     kGOOGLE_API_KEY];
url = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

NSLog(@"%@", url);

//Formulate the string as a URL object.
NSURL *googleRequestURL=[NSURL URLWithString:url];

// Retrieve the results of the URL.
dispatch_async(kBgQueue, ^{
    NSData* data = [NSData dataWithContentsOfURL: googleRequestURL];
    [self performSelectorOnMainThread:@selector(fetchedData:) withObject:data  waitUntilDone:YES];
});
}

-(void)fetchedData:(NSData *)responseData {
//parse out the json data
NSError* error;
NSDictionary* json = [NSJSONSerialization
                      JSONObjectWithData:responseData

                      options:kNilOptions
                      error:&error];

//The results from Google will be an array obtained from the NSDictionary object with the  key "results".
NSArray* places = [json objectForKey:@"results"];

//Write out the data to the console.
NSLog(@"Google Data: %@", places);
}

-(void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated {
//Get the east and west points on the map so you can calculate the distance (zoom level)  of the current map view.
MKMapRect mRect = self.mapView.visibleMapRect;
MKMapPoint eastMapPoint = MKMapPointMake(MKMapRectGetMinX(mRect),  MKMapRectGetMidY(mRect));
MKMapPoint westMapPoint = MKMapPointMake(MKMapRectGetMaxX(mRect), MKMapRectGetMidY(mRect));

//Set your current distance instance variable.
currenDist = MKMetersBetweenMapPoints(eastMapPoint, westMapPoint);

//Set your current center point on the map instance variable.
currentCentre = self.mapView.centerCoordinate;
}

#pragma mark - MKMapViewDelegate methods.
- (void)mapView:(MKMapView *)mv didAddAnnotationViews:(NSArray *)views {
MKCoordinateRegion region;
region =  MKCoordinateRegionMakeWithDistance(locationManager.location.coordinate,1000,1000);


[mv setRegion:region animated:YES];
}

@end

我的最终格式化 URL 的控制台日志是:

https://maps.googleapis.com/maps/api/place/search/json? location=HIDDENLAT,HIDDENLONG&radius=995&types=bar&sensor=true&key=HIDDENAPI

我已经替换了上面生成的 lat、long 和 API 值,但它们返回为正确的值?

我发现的另一个 SO 答案说要添加:

url = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

我做了,但这对我不起作用??

任何想法为什么这不起作用!?我把头发拉出来试图把它弄出来!

谢谢!

【问题讨论】:

    标签: ios json google-places-api


    【解决方案1】:

    对于未来的答案寻求者,这可能是许多问题,包括您的 API 密钥、搜索半径或搜索“类型”或 JSON 解析问题。但是,代码应该与 Gary Stewart 在上面发布的完全一致。他甚至只是问问题就帮我找到了答案……

    在 queryGooglePlaces 方法中的 URL 字符串之后添加NSLog(@"%@", url);,就像他在上面所做的那样。这会将 URL 请求记录到您的控制台,以便您确保它按预期编译。如果是,但您仍然没有取回数据,请复制您从控制台生成的 URL 并在 Web 浏览器中打开它。在生成页面的底部,它会告诉您为什么没有获取数据。

    来自 Google 的开发者文档:

    搜索响应对象中的“状态”字段包含 请求的状态,并且可能包含调试信息以帮助 您追踪请求失败的原因。 “状态”字段可能包含 以下值:

    OK表示没有发生错误;这个地方成功 检测到并返回至少一个结果。 ZERO_RESULTS 表示 搜索成功但未返回任何结果。这可能会发生 如果搜索在远程位置通过 latlng。 OVER_QUERY_LIMIT 表示您已超出配额。 REQUEST_DENIED 表示您的请求被拒绝,一般来说 因为缺少传感器参数。 INVALID_REQUEST 一般 表示所需的查询参数(位置或半径)是 不见了。

    我的问题是我发送的搜索“类型”是“早餐”,而不是简单的“食物”。愚蠢的我,“早餐”将是一个关键字,而不是一个类型。

    (顺便说一下支持的类型列表:https://developers.google.com/places/documentation/supported_types

    希望这有助于发现您的问题。祝你好运!

    【讨论】:

      【解决方案2】:

      改变

      @"https://maps.googleapis.com/maps/api/place/search/json?  location=%f,%f&radius=%@&types=%@&sensor=true&key=%@"
      

      @"https://maps.googleapis.com/maps/api/place/search/json?location=%f,%f&radius=%@&types=%@&sensor=true&key=%@"
      

      (即,url 模板字符串中的 ? 后面没有空格)

      【讨论】:

      • 抱歉,我不知道为什么我在那个链接中放了空格,但生成的 URL 上没有空格?? [链接]maps.googleapis.com/maps/api/place/search/…
      • 好的,最新的更新是,如果我从我的 API 控制台使用“浏览器 API 密钥”,它可以完美运行,所以任何想法我的 IOS 可能有什么问题!?我什至生成了一个新的尝试但仍然没有:(
      • @GaryStewart 你解决过这个问题吗?我的 URL 正在完美生成,但我仍然收到令人麻木的“Google 数据:()”响应。尝试了各种不同的 API 密钥!
      猜你喜欢
      • 2019-02-04
      • 2014-01-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多