2013-10-22 180 views
2

我下面這個教程:http://www.raywenderlich.com/13160/using-the-google-places-api-with-mapkit,但由於某種原因,我的應用程序將返回:Google Places API沒有返回結果?

谷歌數據:( )

這裏是我的.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值,但是它們是作爲正確值返回的嗎?

另一這樣,請回答我發現說,添加:

url = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 

我照辦了,但這並沒有爲我工作?

任何想法,爲什麼這不工作!?我把我的頭髮拉出來,試圖將它拒之門外!

謝謝!

回答

1

變化

@"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模板字符串)

+0

對不起,不知道爲什麼我把這個空間放在那個鏈接裏,但是生成的URL沒有一個? [link] https://maps.googleapis.com/maps/api/place/search/json?location=LAT,LONG&radius=995&types=park&sensor=true&key=KEY [/ link] @Brett –

+0

好的最新更新是,如果我從我的API控制檯使用「瀏覽器API密鑰」,它完美地工作,所以任何想法可能是我的IOS一個錯誤!?我甚至生成了一個新的嘗試,但仍然沒有:( –

+1

@GaryStewart你是否曾經解決過這個問題?我的URL正在完美生成,但我仍然感到麻木的「Google Data:()」響應。嘗試了各種不同的API密鑰! – jeremytripp

2

對於未來的答案者,這可能是一些包括您的API密鑰,搜索半徑或搜索「類型」的問題或JSON解析問題。但是,代碼應該像Gary Stewart上面所公佈的一樣。他甚至幫我找到我的答案,只是通過詢問問題...

在查詢GooglePlaces方法中的URL字符串後添加NSLog(@"%@", url);,如上所述。這會將URL請求記錄到您的控制檯,以確保它可以按預期進行編譯。如果是,但您仍然無法取回數據,請複製您從控制檯生成的網址,然後在網絡瀏覽器中打開它。在生成的頁面底部,它會告訴你爲什麼你沒有獲取數據。

從谷歌的開發者文檔:

搜索響應對象中的「status」字段包含請求的 狀態,並且可能會包含調試信息,以幫助 您跟蹤請求失敗的原因。 「狀態」字段可能包含以下值: :

OK表示未發生錯誤;該地點已成功檢測到 ,並且至少返回了一個結果。 ZERO_RESULTS表示 搜索成功但未返回結果。如果搜索在遠程位置傳遞了一個latlng,則可能發生 。 OVER_QUERY_LIMIT表示您超出了您的配額。 REQUEST_DENIED表示您的請求被拒絕,通常由於缺少傳感器參數而導致 。 INVALID_REQUEST通常 表示缺少所需的查詢參數(位置或半徑) 。

我的問題是,我正在發送搜索「類型」的「早餐」,而不是簡單地「食物」。愚蠢的我,「早餐」將是一個關鍵字,而不是一個類型。

(這裏是支持的類型的方式的列表:https://developers.google.com/places/documentation/supported_types

希望這有助於揭開你的問題。祝你好運!

相關問題