2010-08-04 96 views
2

使用makkit框架开发iphone应用程序。我已将地图视图集成到应用程序中。想要使用一些api在某个地区执行搜索(本地搜索)的一些帮助,我试图探索谷歌java脚本API和ajax api,但不能指出我的解决方案任何帮助,将不胜感激。执行本地商业搜索的iphone mapkit应用程序

回答

4

下面是我用于谷歌搜索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] ; 
0

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

希望这些信息可以让新开发人员在iPhone应用程序中使用Google本地API非常容易获得其他地方的经纬度&经度&。

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