views:

83

answers:

1

Developing iphone application using makkit framework. I have got the map view integrated in the application. Wanted some help regarding performing search in a region (local search) using some api , I have tried exploring google java-script API and ajax api but cannot pin point my solution any help would be appreciated.

+1  A: 

Below is a partial bit of code that I used for the google search APIs. You will need to visit Google Labs API and get a key you can use for search. There is also a GData library, but I had trouble getting it to work for local search so I just went with the HTML/JSON version. My code shows you how to start to decode the JSON that is returned, I cut off the loop since it does a bunch of other stuff.

This is the link to the Google AJAX APi.

I recommend making the API call and then setting a breakpoint where you can look at the dictionary of JSON results you get back to see how it's structured.

NSString *searchString = 
        [NSString stringWithFormat:@"http://ajax.googleapis.com/ajax/services/search/local?v=1.0&sll=%f,%f&q=%@", 
         currentLocation.establishedLocation.coordinate.latitude,
         currentLocation.establishedLocation.coordinate.longitude, 
         searchTerms];
        searchString = [searchString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];  // encode it
        //NSString *localSearchResults = [NSString stringWithContentsOfURL:[NSURL URLWithString:searchString]];
        NSError *error = nil;

        NSString * localSearchResults = [NSString stringWithContentsOfURL:[NSURL URLWithString:searchString] encoding:NSUTF8StringEncoding error:&error];

        if (error != nil) {
            NSLog(@"Error retrieving map search results in ActivityLocationViewControler::lookupSearchTerms: ");    
            NSLog(@"%s %d %s", __FILE__, __LINE__, __PRETTY_FUNCTION__, __FUNCTION__);   // http://stackoverflow.com/questions/969130/nslog-tips-and-tricks/969272
            NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        }  


        else {

            NSData *jsonData = [localSearchResults dataUsingEncoding:NSUTF32BigEndianStringEncoding];
            NSError *error = nil;
            NSDictionary *dictionary = [[CJSONDeserializer deserializer] deserializeAsDictionary:jsonData error:&error];    

            // we now magically have an array of results from our search.  Each result has a bunch of data.
            NSArray *resultsArray =  [[dictionary objectForKey:@"responseData"] objectForKey:@"results"] ;
            //NSArray *resultsArray = [dictionary objectForKey:@"responseData"];

            CLLocationCoordinate2D curCoordinate;
            NSDictionary *currentResult;
            BOOL skipThisEntry;

            for (int i = 0; i < [resultsArray count]; i++) {
                currentResult = [resultsArray objectAtIndex:i];     // this is a dictionary of this result

                curCoordinate.latitude = [(NSString *) [currentResult objectForKey:@"lat"] doubleValue] ;
                curCoordinate.longitude = [(NSString *) [currentResult objectForKey:@"lng"] doubleValue] ;
joelm