1

我想在 iOS 地图视图的点击点添加一个小子视图,这样当我滚动和缩放地图视图时,添加的子视图也会缩放和滚动。有什么帮助吗?我尝试过的代码如下:

- (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不随地图视图移动和滚动。

4

1 回答 1

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"]; 
    }
}

一切顺利..

于 2013-04-26T04:45:08.937 回答