2011-06-14 66 views
0

我遵循下面的代碼,輸出可以打印到控制檯,但是如何在MapView上更新?如何更新plist的地址到Mapview?

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    /* We have our address */ 
    NSString *oreillyAddress = @"1005 Gravenstein Highway North, Sebastopol, CA 95472, USA"; 

    /* We will later insert the address and the format that we want our output in, into this API's URL */ 
    NSString *geocodingURL = @"http://maps.google.com/maps/geo?q=%@&output=%@"; 
    /* Insert the address and the output format into the URL */ 
    NSString *finalURL = [NSString stringWithFormat:geocodingURL, oreillyAddress, GOOGLE_OUTPUT_FORMAT_CSV]; 
    /* Now escape the URL using appropriate percentage marks */ 

    finalURL = [finalURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 
    /* Create our URL */ 
    NSURL *urlToCall = [NSURL URLWithString:finalURL]; 
    /* And a request for the connection using the URL */ 
    NSURLRequest *request = [NSURLRequest requestWithURL:urlToCall]; 

    /* We will put all the connection's received data into this instance of the NSMutableData class */ 
    NSMutableData *newMutableData = [[NSMutableData alloc] init]; 
    self.connectionData = newMutableData; 
    [newMutableData release]; 
    NSURLConnection *newConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self]; 
    /* Create the connection and start the downloading of geocoding results */ 
    self.myConnection = newConnection; 
    [newConnection release]; 
} 

- (void) viewDidUnload{ 
    [super viewDidUnload]; 
    [self.myConnection cancel]; 
    self.myConnection = nil; 
    self.connectionData = nil; 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation: 
(UIInterfaceOrientation)interfaceOrientation { 
    /* Support all orientations */ 
    return YES; 
} 

- (void)dealloc { 
    [myConnection cancel]; 
    [myConnection release]; 
    [connectionData release]; 
    [super dealloc]; 
} 
@end 

回答

0

您必須獲取位置的經緯度信息。您必須創建一個採用MKAnnotation協議的類,並使用addAnnotation:方法將其存儲的實例添加到MKMapView對象中。如果位置在地圖的顯示區域內,則地圖視圖對象將調用委託方法mapView:viewForAnnotation:以獲取該註釋的顯示視圖。所以你將不得不通過採用MKMapViewDelegate協議成爲代表。實施mapView:viewForAnnotation:方法以返回MKPinAnnotationView實例或您自己的子類MKAnnotationView

如果位置不在顯示區域內,則使用setRegion:animated:方法移動到該位置。

+0

@Gere Tan對此有幫助嗎?你需要進一步的幫助嗎? – 2011-06-19 18:16:26