0

私のアプリでは、Google マップを使用してユーザーの隣の場所を検索しています。JSON 応答ファイルのクエリと解析は、URL に 1 種類の場所 (食べ物など) しか含まれていない場合に正常に機能します。/ 別のタイプ (food|bar など) を追加すると、NSDictionary 行でアプリがクラッシュします。コードは次のとおりです。

NSString *urlString = [NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/place/search/json?location=%f,%f&radius=%@&types=food|restaurant&sensor=true&key=%@", currentLocation.coordinate.latitude, currentLocation.coordinate.longitude, [NSString stringWithFormat:@"%i", 1000], kGOOGLE_API_KEY];//|bar|cafe|casino|gym|amusement_park|night_club|park|restaurant|shopping_mall|stadium

    NSURL *requestURL = [NSURL URLWithString:urlString];
    NSURLRequest *request = [NSURLRequest requestWithURL:(requestURL)];

    //response
    NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    NSError *jsonParsingError = nil;
    NSDictionary *locationResults = [NSJSONSerialization JSONObjectWithData:response options:0 error:&jsonParsingError]; //ERROR IS HERE

    NSLog(@"%@",locationResults);

エラーが発生している場所を指摘しました (Xcode のブレークポイントがそれを指しています)。私のコードに何か問題がありますか、それとも不足していますか?

4

1 に答える 1

0

驚くべきことに、URLを次のように変更すると機能します。

    NSString *urlString = [NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/place/search/json?location=%f,%f&radius=%@&types=food|hospital&sensor=true&key=%@", currentLocation.coordinate.latitude, currentLocation.coordinate.longitude, [NSString stringWithFormat:@"%i", 1000], kGOOGLE_API_KEY];//|bar|cafe|casino|gym|amusement_park|night_club|park|restaurant|shopping_mall|stadium
    urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 

ただし、なぜこれに依存するのかわかりません。誰でも私に説明できますか?ありがとう!上記のコメントに貢献してくれたすべての人に感謝します。

于 2013-07-23T16:21:08.410 に答える