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

萝らか妹 提交于 2019-12-04 18:11:53
Rahul Mane

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

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

}

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

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