Google Place api网址iOS

时间:2016-04-04 20:43:07

标签: ios google-places-api

我正在创建一个使用Google Place的应用。之前我曾经使用雅虎的api,并且不得不使用负责雅虎提供的本地搜索的网址。网址如下:

http://local.yahooapis.com/LocalSearchService/V3/localSearch?appid=SF0DVEvV34G4GnXEDU4SXniaDebJ_UvC1G1IuikVz3vpOJrBpyD.VqCJCVJHMh99He3iFz1Rzoqxb0b7Z.0-

现在,由于yahoo的api已经停止,我已决定切换到Google Place。但我找不到要使用的网址。我只是对框架进行了操作并使用了api密钥。我在哪里可以找到Google Place的网址。

1 个答案:

答案 0 :(得分:1)

按照以下提供的链接注册Google Places API:

https://code.google.com/apis/console

参考地方自动搜索的代码链接

https://github.com/AdamBCo/ABCGooglePlacesAutocomplete

NSString *const apiKey = @"*****23xAHRvnOf2BVG8o";
NSString * searchWord = @"search some place "

NSString *urlString = [NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/place/autocomplete/json?input=%@&types=establishment|geocode&radius=500&language=en&key=%@",searchWord,apiKey];

pragma mark - 网络方法

-(void)retrieveGooglePlaceInformation:(NSString *)searchWord withCompletion:(void (^)(BOOL isSuccess, NSError *error))completion {

if (!searchWord) {
    return;
}

searchWord = searchWord.lowercaseString;

self.searchResults = [NSMutableArray array];

if ([self.searchResultsCache objectForKey:searchWord]) {
    NSArray * pastResults = [self.searchResultsCache objectForKey:searchWord];
    self.searchResults = [NSMutableArray arrayWithArray:pastResults];
    completion(YES, nil);

} else {

    NSString *urlString = [NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/place/autocomplete/json?input=%@&types=establishment|geocode&radius=500&language=en&key=%@",searchWord,apiKey];

    NSURL *url = [NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]];
    NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {

        NSDictionary *jSONresult = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];

        if (error || [jSONresult[@"status"] isEqualToString:@"NOT_FOUND"] || [jSONresult[@"status"] isEqualToString:@"REQUEST_DENIED"]){
            if (!error){
                NSDictionary *userInfo = @{@"error":jSONresult[@"status"]};
                NSError *newError = [NSError errorWithDomain:@"API Error" code:666 userInfo:userInfo];
                completion(NO, newError);
                return;
            }
            completion(NO, error);
            return;
        } else {

            NSArray *results = [jSONresult valueForKey:@"predictions"];

            for (NSDictionary *jsonDictionary in results) {

            }

            //[self.searchResultsCache setObject:self.searchResults forKey:searchWord];

            completion(YES, nil);

        }
    }];

    [task resume];
}
}