gpt4 book ai didi

objective-c - 向 MKMapView 添加多个叠加层

转载 作者:可可西里 更新时间:2023-11-01 05:42:11 31 4
gpt4 key购买 nike

我在理解如何向 MKMapView 添加多个叠加层时遇到一些问题。

我跟踪了一个数组路由。跟踪的路线是 CLLocations 数组。所以我有一个数组数组。

我可以走一条路线并将其绘制在 map View 上。但是,我需要将所有路线绘制到 map View 上。

下面是我如何为 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 委托(delegate)调用:

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

但它不起作用。问题是在委托(delegate)方法中我不知何故需要知道要返回哪个叠加层。

所以如果我可以按照以下方式做一些事情:

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

最佳答案

routeLinerouteLineView 变量一次只能指向一个叠加层和叠加层 View ,因此理论上您需要一组此类变量。

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

我建议删除 routeLinerouteLineView 变量。

然后您只需在本地创建一个覆盖对象,并在 viewForOverlay 中返回请求的 overlay 参数的 View 。

loadRoute 方法中,您可以循环遍历routesArray 并为每个路由创建一个本地覆盖对象,然后调用addOverlay

还有一种更简单的方法来构建一个包含所有路线的 map 矩形,这样您就可以设置 map 的区域以显示所有路线。

例子:

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

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

应该是:

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

它应该使用MKMapPoint的大小(不是CLLocationCoordinate2D)。
它们不是一回事(即使结构恰好具有相同的字节大小)。

关于objective-c - 向 MKMapView 添加多个叠加层,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10924874/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com