2016-03-18 182 views
0

我已經在IOS中實施谷歌地點搜索API,並在開發人員控制檯中啓用API,並使用下面的代碼,但它顯示錯誤「此IP,網站或移動應用程序未經授權使用這個API密鑰。從IP地址122.173.223.114接收到的請求,用空指谷歌地方搜索API在IOS

再生後的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); 
} 
} 
+0

有一些限制OG谷歌廣場API http://stackoverflow.com/questions/5008689/google-web-search -api-returns-only-the-first-100-results-set – kb920

+0

我使用google –

+0

的替代GMSAutocompleteViewController庫做到了這一點,所以GMSAutocompleteViewController中有沒有任何限制? – kb920

回答

1

我做到了與使用AFNetworking類試試這個,

#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); 
    }]; 
}