0

I have a problem with my MKPolyline.

I have added this code but I see no line. I get the coordinates form a webserver with a http-request. I save the coordinates as a double and show with that my annotations.

in the viewcontroller.h

@property (nonatomic, retain) MKPolyline *routeLine; 
@property (nonatomic, retain) MKPolylineView *routeLineView; 

in the viewcontroller.m

CLLocationCoordinate2D coordinateArray[7];
coordinateArray[0] = CLLocationCoordinate2DMake(theCoordinate0.latitude, theCoordinate0.longitude);
coordinateArray[1] = CLLocationCoordinate2DMake(theCoordinate1.latitude, theCoordinate1.longitude);
coordinateArray[2] = CLLocationCoordinate2DMake(theCoordinate2.latitude, theCoordinate2.longitude);
coordinateArray[3] = CLLocationCoordinate2DMake(theCoordinate3.latitude, theCoordinate3.longitude);
coordinateArray[4] = CLLocationCoordinate2DMake(theCoordinate4.latitude, theCoordinate4.longitude);
coordinateArray[5] = CLLocationCoordinate2DMake(theCoordinate5.latitude, theCoordinate5.longitude);
coordinateArray[6] = CLLocationCoordinate2DMake(theCoordinate6.latitude, theCoordinate6.longitude);

self.routeLine = [MKPolyline polylineWithCoordinates:coordinateArray count:7];
[self.mapView setVisibleMapRect:[self.routeLine boundingMapRect]]; //If you want the route to be visible

[self.mapView addOverlay:self.routeLine];

-(MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id<MKOverlay>)overlay
{
    if(overlay == self.routeLine)
    {
        if(nil == self.routeLineView)
        {
            self.routeLineView = [[[MKPolylineView alloc] initWithPolyline:self.routeLine]autorelease];
            self.routeLineView.fillColor = [UIColor redColor];
            self.routeLineView.strokeColor = [UIColor redColor];
            self.routeLineView.lineWidth = 10;

        }

        return self.routeLineView;
    }

    return nil;
}
4

1 回答 1

0

您是否已验证您的地图视图已连接到屏幕上的地图视图(Interface Builder 中的 IBOutlet)?接下来要检查的是您是否设置了委托。viewForOverlay通过在函数中放置断点或调试语句来检查这一点。

PS 使用该代码,您将只能画一条线。它依赖于self.routeLine并且self.routeLineView您只有其中一个。

于 2013-03-30T07:06:36.193 回答