我有些困难理解如何在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.routeLine
和self.routeLineView
还有什么用途?请展示调用addOverlay
或addOverlays
的代码。 - user467105