2013-12-22 29 views
1

我有一個約5行的表視圖,其中一個數組存儲緯度,另一個存儲經度
現在,如果我選擇第二行,應該調用這兩個數組中的2對象並顯示在地圖上。
有沒有辦法做到這一點?
或者我將不得不手動輸入沒有數組的座標?
如何在NSArray中添加MKMapView中的座標?

回答

1

實現UITableViewDelegate的didSelectRowAtIndexPath:,當它被調用時,從數組中獲取緯度和經度,並向地圖添加註釋。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    CGFloat latitude = latitudes[indexPath.row]; 
    CGFloat longitude = longitudes[indexPath.row]; 

    CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(latitude, longitude); 

    MKPointAnnotation *point = [[MKPointAnnotation alloc] init]; 
    point.coordinate = coordinate; 
    point.title = @"Title"; 

    [self.mapView addAnnotation:point]; 
}