如何在iOS中按名称,位置搜索本地商家?

时间:2014-10-28 06:06:59

标签: ios ios7 google-places-api yelp

我正在开展项目,我们正在展示本地商业搜索。我正在使用YELP搜索本地商家。根据{{​​3}}我创建了查询。但它仅根据位置给出结果。

我正在尝试使用Google Place API但未获得所需的结果。

我的YELP请求 - YELP Documentation 我的Google Place API请求 - http://api.yelp.com/v2/search/?term=restaurant&location=nyc&limit=20&offset=1

  1. 如何按商家名称获取结果&位置以及使用任何YELP或Google Place API?
  2. 哪一个更适合使用YELP或Google Place API?

3 个答案:

答案 0 :(得分:1)

我使用Google Places API -

解决了我的问题

感谢This Answer

我们获得JSON / XML响应

  1. 搜索城市附近的酒店:
  2.   

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

    1. 搜索城市中的特定位置:
    2.   

      https://maps.googleapis.com/maps/api/place/textsearch/json?query=[SearchPlaceName]+in+[CityName]&sensor=true&key=AddYourOwnKeyHere

      1. 按给定类型搜索城市中的特定地点:
      2.   

        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
        

答案 1 :(得分: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];

}

答案 2 :(得分: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。坐标需要你的位置......当你得到一些场地时,可以通过Log或print看到他们的id。