2014-10-28 55 views
1

我正在處理顯示本地商業搜索的項目。我正在使用YELP來搜索本地業務。根據YELP Documentation我已經創建了查詢。但它僅基於位置給出結果。如何在iOS中按名稱搜索本地商戶?

我在嘗試使用Google Place API,但沒有得到想要的結果。

我YELP要求 - http://api.yelp.com/v2/search/?term=restaurant&location=nyc&limit=20&offset=1 我的谷歌廣場API請求 - https://maps.googleapis.com/maps/api/place/textsearch/json?query=hotels+in+nyc&sensor=true&key=AIzaSyCHwd5OgRXdeuTWV46SHdMLq2lXL20t22U

  1. 我怎樣才能獲得通過的公司名稱&位置以及使用任何YELP或谷歌廣場API結果呢?
  2. 哪一個更適合使用YELP或Google Place API?
+0

有什麼不對的地方谷歌API?它迴應否? – 2014-10-28 06:25:40

+0

是的,它給出了迴應,但給出了紐約市所有餐廳的名單。 我想在紐約搜索特定的餐廳「XYZ」。此外,我無法得到restuarant圖標。 – 2014-10-28 06:40:11

回答

1

我解決使用Google Places API我的問題 -

感謝This Answer

我們得到JSON/XML響應

  1. 搜索酒店靠近城市:

https://maps.googleapis.com/maps/api/place/textsearch/json?query=hotels+in+Pune&sensor=true&key=AddYourOwnKeyHere

在城市
  • 搜索特定的地方:
  • https://maps.googleapis.com/maps/api/place/textsearch/json?query=[SearchPlaceName]+in+[CityName]&sensor=true&key=AddYourOwnKeyHere

    通過給定類型的
  • 搜索特定城市的地方:
  • https://maps.googleapis.com/maps/api/place/textsearch/json?query=[SearchPlaceName]+in+[CityName]&type=[PlaceType]&sensor=true&key=AddYourOwnKeyHere

    • 檢索圖像/餐廳/地方的圖標 -

    按照Documentation

    我們可以使用photo_reference &要求如 -

    https://maps.googleapis.com/maps/api/place/photo?maxwidth=400&photoreference=CoQBegAAAFg5U0y-iQEtUVMfqw4KpXYe60QwJC-wl59NZlcaxSQZNgAhGrjmUKD2NkXatfQF1QRap-PQCx3kMfsKQCcxtkZqQ&key=AddYourOwnKeyHere 
    
    0

    1)我使用了Yelp API。網址爲特殊業務 - http://api.yelp.com/v2/business/ 全球搜索 - http://api.yelp.com/v2/search 搜索後,您必須正確地傳遞數據在API搜索網址。網址簽名通知NSStringWithFormat。並且不要忘記OAuth密鑰!我的要求:

    -(void)searchBy:(NSString *)categoryFilter inLocationCity:(NSString *)aLocationCity { 
    
    NSString *urlString = [NSString stringWithFormat:@"%@?term=%@&location=%@", 
             YELP_SEARCH_URL, 
             categoryFilter, 
             aLocationCity]; 
    
    NSURL *URL = [NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; 
    
    OAConsumer *consumer = [[OAConsumer alloc] initWithKey:OAUTH_CONSUMER_KEY 
                   secret:OAUTH_CONSUMER_SECRET]; 
    
    OAToken *token = [[OAToken alloc] initWithKey:OAUTH_TOKEN 
                 secret:OAUTH_TOKEN_SECRET]; 
    
    id<OASignatureProviding, NSObject> provider = [[OAHMAC_SHA1SignatureProvider alloc] init]; 
    NSString *realm = nil; 
    
    OAMutableURLRequest *request = [[OAMutableURLRequest alloc] initWithURL:URL 
                       consumer:consumer 
                        token:token 
                        realm:realm 
                     signatureProvider:provider]; 
    
    [request prepare]; 
    
    NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self]; 
    
    if (conn) { 
        self.urlRespondData = [NSMutableData data]; 
    } 
    

    }

    還添加方法NSURLConnectionDelegate:

    - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { 
    
    [self.urlRespondData setLength:0]; 
    

    }

    - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)d { 
    [self.urlRespondData appendData:d]; 
    

    }

    - (void)connectionDidFinishLoading:(NSURLConnection *)connection { 
    
    NSError *e = nil; 
    NSDictionary *resultResponseDict = [NSJSONSerialization JSONObjectWithData:self.urlRespondData 
                        options:NSJSONReadingMutableContainers 
                        error:&e]; 
    if (self.resultArray && [self.resultArray count] > 0){ 
    
        [self.resultArray removeAllObjects]; 
    } 
    
    if (!self.resultArray) { 
        self.resultArray = [[NSMutableArray alloc] init]; 
    } 
    DLog(@"YELP response %@", resultResponseDict); 
    
    if (resultResponseDict && [resultResponseDict count] > 0) { 
    
        if ([resultResponseDict objectForKey:@"businesses"] && 
         [[resultResponseDict objectForKey:@"businesses"] count] > 0) { 
    
         for (NSDictionary *venueDict in [resultResponseDict objectForKey:@"businesses"]) { 
    
          Venue *venueObj = [[Venue alloc] initWithDict:venueDict]; 
          [self.resultArray addObject:venueObj]; 
         } 
        } 
    } 
    
    [self.delegate loadResultWithDataArray:self.resultArray]; 
    

    }

    +0

    感謝您的回覆,但這是'searchBy:inLocationCity:'將搜索位於'xyz'位置的餐廳。但我想在'xyz'位置搜索'abc'餐廳。 我如何得到這樣的結果? – 2014-10-28 13:14:43

    +1

    設置特殊座標,特殊的restaurant.id,Yelp輸出事件編號爲 – NilsHolgerson 2014-10-28 13:23:01

    +0

    ok。我會嘗試座標。但是如果可能的話,你可以告訴'restaurant.id'的參數名稱? – 2014-10-28 13:56:48

    0
    -(instancetype)initWithDict:(NSDictionary *)dict { 
    
    self = [super init]; 
    
    if (self) { 
    
        self.name = [dict objectForKey:@"name"]; 
        self.venueId = [dict objectForKey:@"id"]; 
        self.thumbURL = [dict objectForKey:@"image_url"]; 
        self.ratingURL = [dict objectForKey:@"rating_img_url"]; 
        self.yelpURL = [dict objectForKey:@"url"]; 
        self.venueId = [dict objectForKey:@"id"]; 
        self.reviewsCount =[[dict objectForKey:@"review_count"] stringValue]; 
        self.categories = [dict objectForKey:@"categories"][0][0]; 
        self.distance = [dict objectForKey:@"distance"]; 
        self.price = [dict objectForKey:@"deals.options.formatted_price"]; 
        self.address = [[[dict objectForKey:@"location"] objectForKey:@"address"] componentsJoinedByString:@", "]; 
        NSArray *adr = [[dict objectForKey:@"location"] objectForKey:@"display_address"]; 
        self.displayAddress = [adr componentsJoinedByString:@","]; 
    } 
    return self; 
    

    }

    與Yelp的響應值法...你只需要ID。座標需要你的位置......當你得到一些場地,看到他們的身份與日誌或打印。