Google Place Search API in IOS



我已经在IOS中实现了谷歌地点搜索API,并在开发者控制台中启用了API,并使用以下代码,但它显示错误"此IP,网站或移动应用程序无权使用此API密钥。从 IP 地址 122.173.223.114 收到的请求,引用为空"

重新生成 API 密钥后,它显示

API 密钥已过期,一段时间后它显示相同的上述错误。请帮助某人。

  -(void) queryGooglePlaces: (NSString *) googleType {
// Build the url string to send to Google. NOTE: The kGOOGLE_API_KEY is a constant that should contain your own API key that you obtain from Google. See this link for more info:
// https://developers.google.com/maps/documentation/places/#Authentication
NSString *url = [NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/place/search/json?location=%f,%f&radius=%@&types=%@&sensor=true&key=%@", appDel.objLocationManager.location.coordinate.latitude, appDel.objLocationManager.location.coordinate.longitude, [NSString stringWithFormat:@"%i", appDel.currenDist],googleType, kGOOGLE_API_KEY];

//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
if (responseData==nil) {
}else{
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: %@", json);
}
 }

我使用 AFNetworker class 试试这个,

#define kGoogleAutoCompleteAPI @"https://maps.googleapis.com/maps/api/place/autocomplete/json?key=%@&input=%@"

-(void)getAutoCompletePlaces:(NSString *)searchKey
{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    // set request timeout
    manager.requestSerializer.timeoutInterval = 5;
    NSString *url = [[NSString stringWithFormat:kGoogleAutoCompleteAPI,GoogleDirectionAPI,searchKey] stringByReplacingOccurrencesOfString:@" " withString:@"+"];
    NSLog(@"API : %@",url);
    [manager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject)
     {
         NSLog(@"JSON: %@", responseObject);
         [MBProgressHUD hideHUDForView:self.view animated:YES];
         NSDictionary *JSON = responseObject;
         predictions = [NSMutableArray array];
         // success
         AutomCompletePlaces *places = [AutomCompletePlaces modelObjectWithDictionary:JSON];
         [arrSuggestionData removeAllObjects];
         if (!arrSuggestionData) {
             arrSuggestionData = [NSMutableArray array];
         } 
         for (Predictions *pred in places.predictions)
         {
             [arrSuggestionData addObject:pred.predictionsDescription];
         }
         [self.Tbl_suggestion reloadData];
     } failure:^(AFHTTPRequestOperation *operation, NSError *error)
     {
         NSLog(@"Error: %@", error);
     }];
}

最新更新