2010-08-04 50 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]; 
}]; 
相關問題