簡體   English   中英

在iOS6中的地圖視圖上保留疊加線

[英]Persist overlay line over map view in iOS6

我有地圖視圖,我喜歡使用疊加方法在上面繪制線,當我按下按鈕時,我添加了兩個位置經度和緯度來在它們之間繪制線,但是當我第二次按下時,我提供了新的一對坐標,並在地圖上顯示了新的線但是以前的疊加路徑消失了,但是我也需要在同一張地圖上使用之前的路徑

這是我的代碼。

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

請用同樣的方式或其他替代方式向我提供解決方案。

比你 。

這是因為您只允許viewForOverlay函數返回self.routeline的視圖,而您只有其中之一。 每次對viewForOverlay調用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;
}

您可能需要做更多的事情,例如首先檢查疊加層實際上是一條折線,但這應該足以使您前進。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM