2013-04-26 86 views
1

我想在iOS地圖視圖的點擊點處添加一個小的子視圖,以便在滾動和縮放地圖視圖時添加子視圖時也可以縮放和滾動。任何幫助?我試過的代碼如下:向MKMapView添加子視圖(不是覆蓋圖)

- (void)viewDidLoad 
{ 
    UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(foundTap:)]; 
    tapRecognizer.numberOfTapsRequired = 1; 
    tapRecognizer.numberOfTouchesRequired = 1; 
    [self.myMapView addGestureRecognizer:tapRecognizer]; 
} 

- (IBAction)foundTap:(UITapGestureRecognizer *)recognizer 
{ 
    CGPoint point = [recognizer locationInView:self.myMapView]; 
    dotimage = [[UIView alloc]initWithFrame:CGRectMake(point.x,point.y , 10, 10)]; 
    dotimage.backgroundColor = [UIColor redColor]; 
    [self.myMapView addSubview:dotimage]; 
} 

視圖dotimage未移動並且滾動地圖視圖。

回答

2

你的做法是錯誤的,你不能認爲添加在地圖子視圖縮放,你必須在自來水中添加自定義針,自定義針應該看起來像要添加您的看法..

你可以試試在你下面

- (void)viewDidLoad 
{ 
     UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(addCustomView:)]; 
     [recognizer setNumberOfTapsRequired:1]; 
     [map addGestureRecognizer:recognizer]; 
     [recognizer release]; 
} 

- (void)addCustomView:(UITapGestureRecognizer*)recognizer 
{ 
    CGPoint tappedPoint = [recognizer locationInView:map]; 
    //Get the coordinate of the map where you tapped 
    CLLocationCoordinate2D coord= [map convertPoint:tappedPoint toCoordinateFromView:map]; 

    //Add Annotation 
    /* Create a custom annotation class which takes coordinate */ 
    CustomAnnotation *ann=[[CustomAnnotation alloc] initWithCoord:coord]; 
    [map addAnnotation:ann]; 

} 

然後代碼map delegate功能

-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{ 
    if([annotation isKindOfClass:[CustomAnnotation class]]) 
    { 
     //Do your annotation initializations 

     // Then return a custom image that looks like your view like below 
     annotationView.image=[UIImage imageNamed:@"customview.png"]; 
    } 
} 

一切順利..

+0

感謝您的回覆。 – anoop 2013-04-26 05:10:51

+0

首先感謝您的回覆,我的想法是,當用戶觸摸地圖視圖時,點擊點出現一個小uiview。通過連接這個視圖座標我想繪製多邊形覆蓋。也可以移動任何添加的視圖,然後覆蓋也改變對應於新的座標,直到這工作良好。但問題是,當我縮放或滾動mapview覆蓋是對應於mapview的改變,但添加的子視圖保持在相同的地方。我需要在多邊形座標位置。 – anoop 2013-04-26 06:19:52