【问题标题】:Persist overlay line over map view in iOS6在 iOS6 中的地图视图上保持覆盖线
【发布时间】:2013-03-18 11:36:39
【问题描述】:

我有地图视图,我喜欢使用叠加方法在其上画线,当我按下按钮时,我添加了两个位置纬度和经度以在它们之间画线,但是当我第二次按下我提供的新坐标对和新线时在地图上出现,但以前的覆盖路径消失了,但我也需要同一张地图上的上一条路径

这是我的代码。

- (IBAction)refreshMapMethod:(id)sender

{


 int kk=[[NSUserDefaults standardUserDefaults]integerForKey:@"ham"];




    if (kk==1)
    {
         CLLocationCoordinate2D coordinateArray[2];
        coordinateArray[0] = CLLocationCoordinate2DMake(12.915181,+77.626055);
        coordinateArray[1] = CLLocationCoordinate2DMake(12.892156, +77.582188);
        self.routeLine = [MKPolyline polylineWithCoordinates:coordinateArray count:2];
        [self.myMapView setVisibleMapRect:[self.routeLine boundingMapRect]];

        [self.myMapView addOverlay:self.routeLine];
        [[NSUserDefaults standardUserDefaults]setInteger:2 forKey:@"ham" ];


    }


    if (kk==2)
    {
        CLLocationCoordinate2D coordinateArray[2];
        coordinateArray[0] = CLLocationCoordinate2DMake(12.892156,+77.426055);
        coordinateArray[1] = CLLocationCoordinate2DMake(12.892156, +77.582188);
        self.routeLine = [MKPolyline polylineWithCoordinates:coordinateArray count:2];
        [self.myMapView setVisibleMapRect:[self.routeLine boundingMapRect]];

        [self.myMapView addOverlay:self.routeLine];
        [[NSUserDefaults standardUserDefaults]setInteger:3 forKey:@"ham" ];


    }
    if (kk==3)
    {
        CLLocationCoordinate2D coordinateArray[2];
        coordinateArray[0] = CLLocationCoordinate2DMake(12.892156, +77.382188);
        coordinateArray[1] = CLLocationCoordinate2DMake(12.892156, +77.282188);
        self.routeLine = [MKPolyline polylineWithCoordinates:coordinateArray count:2];
        [self.myMapView setVisibleMapRect:[self.routeLine boundingMapRect]];

        [self.myMapView addOverlay:self.routeLine];
        [[NSUserDefaults standardUserDefaults]setInteger:3 forKey:@"ham" ];


    }



    CLLocationCoordinate2D zoomLocation;
    zoomLocation.latitude= 12.915181;
    zoomLocation.longitude=77.626055;

    MKCoordinateRegion viewRegion=MKCoordinateRegionMakeWithDistance(zoomLocation, 3*METERS_PER_MILE, 3*METERS_PER_MILE);

    [self.myMapView setRegion:viewRegion animated:YES];


 [[NSUserDefaults standardUserDefaults]setInteger:2 forKey:@"change" ];


}

调用覆盖方法..

-(MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id<MKOverlay>)overlay


{

    if(overlay == self.routeLine)
    {
        if(nil == self.routeLineView)
        {
          self.routeLineView = [[MKPolylineView alloc] initWithPolyline:self.routeLine];



            self.routeLineView.fillColor = [UIColor redColor];
            self.routeLineView.strokeColor = [UIColor redColor];
            self.routeLineView.lineWidth = 5;

        }

        return self.routeLineView;
    }

    return nil;
}

请以相同的方式或其他任何替代方式为我提供解决方案。

比你。

【问题讨论】:

    标签: iphone path mkmapview


    【解决方案1】:

    这是因为您只允许 viewForOverlay 函数返回 self.routeline 的视图,而您只有其中一个。对viewForOverlay 的所有其他调用都将返回 nil,因此不会被绘制。您需要做的是绘制所有叠加层。

    -(MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id<MKOverlay>)overlay
    {
        MKPolylineView* routeLineView = [[MKPolylineView alloc] initWithPolyline:(MKPolyLine)overlay];
        routeLineView.fillColor = [UIColor redColor];
        routeLineView.strokeColor = [UIColor redColor];
        routeLineView.lineWidth = 5;
    
        return routeLineView;
    }
    

    您可能需要做更多的事情,例如首先检查叠加层实际上是一条折线,但这应该足以让您继续前进。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-20
      • 2013-06-25
      相关资源
      最近更新 更多