2

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.

RedBlueThing
  • 42,006
  • 17
  • 96
  • 122
Ameya
  • 1,914
  • 4
  • 29
  • 55

3 Answers3

4

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
  • 8,741
  • 1
  • 19
  • 17
0

MapKit provides the MKLocalSearch API.

We can use this API to perform searches for locations that users describe by name, address, or type, such as coffee or theater.

For reference:

// Create and initialize a search request object.
MKLocalSearchRequest *request = [[MKLocalSearchRequest alloc] init];
request.naturalLanguageQuery = searchText;
request.region = self.map.region;

// Create and initialize a search object.
MKLocalSearch *search = [[MKLocalSearch alloc] initWithRequest:request];

// Start the search and display the results as annotations on the map.
[search startWithCompletionHandler:^(MKLocalSearchResponse *response, NSError *error)
 {
     NSMutableArray *placemarks = [NSMutableArray array];
     for (MKMapItem *item in response.mapItems) {
         [placemarks addObject:item.placemark];
         //For Address
         //NSDictionary *addressDict = item.placemark.addressDictionary;
     }
     [self.map removeAnnotations:[self.map annotations]];
     [self.map showAnnotations:placemarks animated:NO];
 }];
Bhuvan Bhatt
  • 3,276
  • 2
  • 18
  • 23
0

I just published some simple iOS classes that use Google's Local Search API to get location information about places in a map region via a name or address search. There are detailed instructions here, and the GitHub repository is here.

Hopefully, this information will make it very easy for new developers to use the Google Local API in an iPhone app to get the latitude & longitude of businesses & other places.

Victor Van Hee
  • 9,679
  • 7
  • 33
  • 41