0

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

这是我的代码。

- (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;
}

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

比你 。

4

1 回答 1

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

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

于 2013-03-18T19:47:18.577 回答