2014-10-28 19 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。坐标需要你的位置......当你得到一些场地,看到他们的身份与日志或打印。