向MKMapView添加多个叠加层

4

我有些困难理解如何在MKMapView中添加多个叠加图层。

我有一个跟踪的路线数组。跟踪的路线是CLLocations的数组。因此,我有一个数组的数组。

我能够将一条路线绘制在地图视图上。但是,我需要将所有路线都绘制在地图视图上。

这是我创建1条路线的MKPolyline的方式:

-(void) loadRoute
{
    //So we grab an array that holds all the locations of one route. 
            NSArray *locations = [[NSArray alloc] initWithArray:[routesArray objectAtIndex:0]];

            // while we create the route points, we will also be calculating the bounding box of our route
            // so we can easily zoom in on it. 
            MKMapPoint northEastPoint; 
            MKMapPoint southWestPoint; 

            // create a c array of points. 
            MKMapPoint* pointArr = malloc(sizeof(CLLocationCoordinate2D) *[locations count]);

            for(int idx = 0; idx < [locations count]; idx++)
            {
                CLLocation *tempLoc = (CLLocation*)[locations objectAtIndex:idx];

                CLLocationDegrees latitude  = tempLoc.coordinate.latitude;
                CLLocationDegrees longitude = tempLoc.coordinate.longitude;

                // create our coordinate and add it to the correct spot in the array 
                CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(latitude, longitude);

                MKMapPoint point = MKMapPointForCoordinate(coordinate);

                // if it is the first point, just use them, since we have nothing to compare to yet. 
                if (idx == 0) {
                    northEastPoint = point;
                    southWestPoint = point;
                }
                else 
                {
                    if (point.x > northEastPoint.x) 
                        northEastPoint.x = point.x;
                    if(point.y > northEastPoint.y)
                        northEastPoint.y = point.y;
                    if (point.x < southWestPoint.x) 
                        southWestPoint.x = point.x;
                    if (point.y < southWestPoint.y) 
                        southWestPoint.y = point.y;
                }
                pointArr[idx] = point;
            }


            self.routeLine = [MKPolyline polylineWithPoints:pointArr count:[locations count]];

//Zoom in to fit route on screen
            _routeRect = MKMapRectMake(southWestPoint.x, southWestPoint.y, northEastPoint.x - southWestPoint.x, northEastPoint.y - southWestPoint.y);

            // clear the memory allocated earlier for the points
            free(pointArr);

}

以下是返回覆盖层的MapKit代理调用:

以下是返回覆盖层的MapKit代理调用:

#pragma mark MKMapViewDelegate

- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay
{
    MKOverlayView* overlayView = nil;

    if(overlay == self.routeLine)
    {
        //if we have not yet created an overlay view for this overlay, create it now. 
        if(nil == self.routeLineView)
        {
            self.routeLineView = [[MKPolylineView alloc] initWithPolyline:self.routeLine];
            self.routeLineView.fillColor = [UIColor redColor];
            self.routeLineView.strokeColor = [UIColor redColor];
            self.routeLineView.lineWidth = 3;
        }
        overlayView = self.routeLineView;
    }

    return overlayView;
}

以上所有代码都很好运行。我无法想象如何加载更多的叠加层来展示其他路线。

我尝试在loadRoute方法中,对所有路线进行遍历,并为每个路线创建一个MKOverlayView。将所有这些MKOverlayView存储在一个数组中,然后执行以下操作:

[self.mapView addOverlays:array];

但它不起作用。问题在于,在委托方法中,我需要知道返回哪个叠加层。

因此,如果我能够做一些类似于:

- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay
{
    int tag = overlay.tag;
    return (MKOverlayView*)[arrayOfViews objectAtIndex:tag];
}

它本来应该很好用。但不幸的是,它并不像那样工作 :(

任何帮助都将非常感激!

编辑:

我在ViewDidLoad中使用这个来添加覆盖层:

[self loadRoute];

        // add the overlay to the map
        if (nil != self.routeLine) {
            [self.mapView addOverlay:self.routeLine];
        }

        // zoom in on the route. 
        [self zoomInOnRoute];

这是在头部:

// the data representing the route points. 
    MKPolyline* _routeLine;

    // the view we create for the line on the map
    MKPolylineView* _routeLineView;

    // the rect that bounds the loaded points
    MKMapRect _routeRect;

除了上面显示的代码之外,self.routeLineself.routeLineView还有什么用途?请展示调用addOverlayaddOverlays的代码。 - user467105
那么routeLine和routeLineView除了在问题中展示的代码中没有被使用过,对吗? - user467105
1个回答

4
routeLinerouteLineView变量一次只能指向一个覆盖层和覆盖层视图,因此理论上您需要一个这样的变量数组。
但是,根据所示代码,您首先无需保留对它们的引用。
建议删除routeLinerouteLineView变量。
然后,您只需在本地创建一个覆盖对象,并在viewForOverlay中返回所请求的overlay参数的视图。
loadRoute方法中,您可以遍历routesArray,对于每个路线创建一个本地覆盖对象,并调用addOverlay
还有一种更简单的方法可以构建一个包含所有路线边界的地图矩形,以便将地图区域设置为显示它们。
例如:
-(void) loadRoute
{
    _routeRect = MKMapRectNull;

    for (int raIndex = 0; raIndex < routesArray.count; raIndex++) 
    {
        //So we grab an array that holds all the locations of one route. 
        NSArray *locations = [[NSArray alloc] initWithArray:
                                 [routesArray objectAtIndex:raIndex]];
        //note we are getting object at raIndex (not 0) above

        //...no change to existing code here...

        //self.routeLine = [MKPolyline polylineWithPoints:pointArr count:[locations count]];
        //replace above line with this...
        MKPolyline *pl = [MKPolyline polylineWithPoints:pointArr count:[locations count]];
        [mapView addOverlay:pl];

        //Zoom in to fit route on screen
        //_routeRect = MKMapRectMake(southWestPoint.x, southWestPoint.y, northEastPoint.x - southWestPoint.x, northEastPoint.y - southWestPoint.y);
        //replace above line with this to create a rect around ALL routes...
        if (MKMapRectIsNull(_routeRect))
            _routeRect = pl.boundingMapRect;
        else
            _routeRect = MKMapRectUnion(_routeRect, pl.boundingMapRect);

        // clear the memory allocated earlier for the points
        free(pointArr);
    }    
}

viewDidLoad中,只需调用loadRoute而不要调用addOverlayviewForOverlay方法变得更加简单:
- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay
{
    MKPolylineView *pv = [[MKPolylineView alloc] initWithPolyline:overlay];
    pv.fillColor = [UIColor redColor];
    pv.strokeColor = [UIColor redColor];
    pv.lineWidth = 3;
    return pv;
}

顺便提一下,在 loadRoute 函数中,这一行代码:
MKMapPoint* pointArr = malloc(sizeof(CLLocationCoordinate2D) *[locations count]);

should be:

MKMapPoint* pointArr = malloc(sizeof(MKMapPoint) *[locations count]);

它应该使用MKMapPoint的大小(而不是CLLocationCoordinate2D)。
它们并不相同(即使这些结构体恰好具有相同的字节大小)。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接