iphone mapkit应用程序执行本地商业搜索

时间:2010-08-04 11:31:11

标签: iphone objective-c google-maps

使用makkit框架开发iphone应用程序。我已经在应用程序中集成了地图视图。想要使用一些api在一个地区(本地搜索)中执行搜索,我已经尝试过探索google java-script API和ajax api,但是无法确定我的解决方案,任何帮助都会受到赞赏。

3 个答案:

答案 0 :(得分:4)

以下是我用于Google搜索API的部分代码。 您需要访问Google Labs API并获取可用于搜索的密钥。 还有一个GData库,但我无法让它用于本地搜索,所以我只使用了HTML / JSON版本。 我的代码向您展示了如何开始解码返回的JSON,我切断了循环,因为它做了很多其他的东西。

这是Google AJAX APi

的链接

我建议进行API调用,然后设置一个断点,您可以在其中查看JSON结果的字典,了解它的结构。

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

答案 1 :(得分:0)

我刚刚发布了一些简单的iOS类,这些类使用Google的Local Search API通过名称或地址搜索获取地图区域中位置的位置信息。有detailed instructions herethe GitHub repository is here

希望这些信息可以让新开发人员轻松地在iPhone应用中使用Google Local API获取纬度和广度。经度和经营其他地方。

答案 2 :(得分:0)

MapKit提供MKLocalSearch API。

我们可以使用此API执行搜索用户按姓名,地址或类型描述的位置,例如咖啡或剧院。

供参考:

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