How to search local business by name, location in iOS?

∥☆過路亽.° 提交于 2019-12-09 23:47:28

问题


I am working on project in which we are displaying local business search. I am using YELP to search local business. As per YELP Documentation i have created query. But it gives result based on location only.

I am trying with Google Place API but not getting desired result.

My YELP request - http://api.yelp.com/v2/search/?term=restaurant&location=nyc&limit=20&offset=1 My Google Place API request - https://maps.googleapis.com/maps/api/place/textsearch/json?query=hotels+in+nyc&sensor=true&key=AIzaSyCHwd5OgRXdeuTWV46SHdMLq2lXL20t22U

  1. How can i get result by business name & location as well using any YELP or Google Place API?
  2. Which one is better to use YELP or Google Place API?

回答1:


I solve my problem using Google Places API -

Thanks to This Answer.

We get JSON/XML response

  1. Search hotels near City:

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

  1. Search specific place in city:

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

  1. Search specific place in city by given type:

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

  • To retrieve image/icons for restaurant/place -

As per Documentation.

We can use photo_reference & request like -

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



回答2:


1) I used Yelp API. Url for special business - http://api.yelp.com/v2/business/ For global search - http://api.yelp.com/v2/search After search you must correctly pass data in api search url. Notice of url signature in NSStringWithFormat. And don't forget OAuth keys! My request:

-(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];
}

}

Also add methods 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];

}




回答3:


-(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;

}

Method with yelp response values...You need just id. Coordinates need for you location...When you get some venues see theirs id with Log or print.



来源:https://stackoverflow.com/questions/26601838/how-to-search-local-business-by-name-location-in-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!