iOS   发布时间:2022-05-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了objective-c – 向MKMapView添加多个叠加层大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

概述

我在理解如何向MKMapView添加多个叠加层时遇到一些麻烦. 我有跟踪的阵列路线.跟踪的路由是CLLOCATIOns数组.所以我有一个数组数组. 我可以采取一条路线并在地图视图上绘制它.但是,我需要将所有路径绘制到地图视图中. 以下是我如何为1条路线创建MKPolyline: -(void) loadRoute { //So we grab an array that holds all
我在理解如何向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委托调用

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

解决方法

routeLine和routeLineView变量一次只能指向一个叠加层和叠加层视图,因此理论上你需要一个这样的变量数组.

但是,根据显示代码,您不需要首先保留对这些的引用.

我建议删除routeLine和routeLineView变量.

然后,您只需在本地创建一个覆盖对象,并在viewForOverlay中返回所请求的覆盖参数的视图.

在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,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,不要调用addOverlay.

viewForOverlay方法变得更加简单:

- (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中,这一行:

@H_836_27@mKMapPoint* pointArr = malloc(sizeof(CLLOCATIOnCoordinate2D) *[LOCATIOns count]);

应该:

@H_836_27@mKMapPoint* pointArr = malloc(sizeof(MKMapPoint) *[LOCATIOns count]);

它应该使用MKMapPoint的大小(而不是CLLOCATIOnCoordinate2D).它们不是一回事(即使结构碰巧是相同的字节大小).

大佬总结

以上是大佬教程为你收集整理的objective-c – 向MKMapView添加多个叠加层全部内容,希望文章能够帮你解决objective-c – 向MKMapView添加多个叠加层所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。