2011-07-29 56 views

回答

0

您需要一個API來爲您提供這些東西,標準SDK中沒有一個。

+0

什麼樣的api – user748001

2

您可以使用谷歌的服務,像這樣並把它在一個循環中,每次改變pointOfInterest字符串:

CLLocationCoordinate2D coordinate = location.coordinate; 
NSString *pointOfInterest = @"banks"; 
NSString *URLString = [NSString stringWithFormat:@"http://ajax.googleapis.com/ajax/services/search/local?v=1.0&rsz=small&sll=%f,%f&q=%@",coordinate.latitude,coordinate.longitude, pointOfInterest]; 

URLString = [URLString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:URLString]]; 

// Perform request and get JSON back as a NSData object 
NSError *error = nil; 
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&error]; 
if(error != nil) { 
UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:@"Error" 
                 message:[error localizedDescription] 
                 delegate:self 
               cancelButtonTitle:@"Done" 
               otherButtonTitles:nil] autorelease]; 
     [alert show]; 
} 
else { 
// Get JSON as a NSString from NSData response 
    NSString *jsonString = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding]; 
    //Return the values in NSDictionary format 
    SBJsonParser *parser = [[SBJsonParser alloc] init]; 

    NSDictionary *jsonResponse = [parser objectWithString:jsonString error:nil]; 
    NSDictionary *responseData = [jsonResponse objectForKey:@"responseData"]; 
    NSArray *results = [responseData objectForKey:@"results"]; 
} 

你可以得到JSON API這裏:

https://github.com/stig/json-framework/

0

你應該有代表銀行,餐廳或公共汽車的對象(示例中的電臺)。您可能希望它實現MKAnnotation協議,因爲您可能希望將它們添加到MKMapView中。每個對象都需要一個座標屬性(CLLocationCoordinate2D)。當我做這樣的事情時,我還添加了距離屬性(CLLocationDistance)。在viewcontroller中實例化這些對象時,您可以將它們添加到數組中。

現在,當用戶的應用程序更新時,您讓每個對象計算從該位置到自身的距離。

例如:

- (void)calculateDistance:(CLLocation *)location { 
    CLLocation *stationLocation = [[CLLocation alloc] initWithLatitude:coordinate.latitude longitude:coordinate.longitude]; 
    distance = [location distanceFromLocation:stationLocation]; 
    [stationLocation release]; 
} 

一旦你讓每一個對象計算出它的距離,你現在可以排序的陣列與遠處的物體。

[stations sortUsingSelector:@selector(compareDistance:)]

這要求你的對象有這樣的方法來實現:

- (NSComparisonResult)compareDistance:(Station *)aStation { 
    if (distance < aStation.distance) return NSOrderedAscending; 
    if (distance > aStation.distance) return NSOrderedDescending; 
    return NSOrderedSame; 
} 

現在你應該有與代表銀行等按距離排序的對象你的數組。

[stations objectAtIndex:0]將關閉到您的位置。

相關問題