2

我正在尝试使用MKPolyline我的MKMapView. 它没有给出任何错误,但这条线没有出现在我的地图上。

我几乎尝试了所有方法,但仍然无法正常工作。

[新的]

我现在已经尝试将其余的评论出来。我还将所有内容都移到了同一个控制器中。子级仅向其父级发送带有 CLLocations 的 NSMutableArray。我还认为它可能在与屏幕上显示的不同的 MKMapView 上绘图。但似乎不是,因为我成功地在同一个 MKMapView 上绘制了图钉和 TileOverlay。

我试过这个没有成功:

- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay
{
    NSLog(@"Drawing overlay");
    if(overlay == self.routeLine)
    {
        NSLog(@"Drawing routeLine");
        if(nil == self.routeLineView)
        {
            NSLog(@"Drawing routeLineView");
            NSLog(@"Point Count: %d",self.routeLine.pointCount);
            /*MKMapPoint *tmp = self.routeLine.points;
            for(int i = 0; i < self.routeLine.pointCount;i++)
                NSLog(@"Points: %d\t%@",i, tmp[i]);*/ // Can't find way to output points
            self.routeLineView = [[[MKPolylineView alloc] initWithPolyline:self.routeLine] autorelease];
            self.routeLineView.fillColor = [UIColor redColor];
            self.routeLineView.strokeColor = [UIColor redColor];
            self.routeLineView.lineWidth = 3;
            NSLog(@"After setting line specs");
        }
        return self.routeLineView;
        NSLog(@"Not supposed to be called");
    }
    /*TileOverlayView *view = [[TileOverlayView alloc] initWithOverlay:overlay];
    view.tileAlpha = 0.6;
    return [view autorelease];*/
    return nil;
}

这输出:

绘制叠加
绘制 routeLine
绘制 routeLineView
点数:2
设置线规格后


[最初的问题]

我将反向发布我的代码:

地图视图:viewForOverlay:

- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay
{
    NSLog(@"Drawing overlay");
    if(overlay == self.routeLine)
    {
         NSLog(@"Drawing routeLine");
         if(nil == self.routeLineView)
        {
            NSLog(@"Drawing routeLineView");
            self.routeLineView = [[[MKPolylineView alloc] initWithPolyline:self.routeLine] autorelease];
            self.routeLineView.fillColor = [UIColor redColor];
            self.routeLineView.strokeColor = [UIColor redColor];
            self.routeLineView.lineWidth = 3;
        }
        return self.routeLineView;
    }
    //Not part of the Question
    TileOverlayView *view = [[TileOverlayView alloc] initWithOverlay:overlay];
    view.tileAlpha = 0.6;
    return [view autorelease];
}

这将全部输出到控制台:

绘制叠加
绘制 routeLine
绘制 routeLineView

添加行

-(void)addRouteLine:(MKPolyline *)route
{
    self.routeLine = route;
    NSLog(@"addRouteLine"); //Successfully prints in console
    [myMapView addOverlay:self.routeLine];
}

创建线 这发生在子模态视图控制器中

-(void)loadRoute
{
    // 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) * waypoints.count);
    MKPolyline *polyLine;
    for(int idx = 0; idx < waypoints.count; idx++)
    {
        CLLocationCoordinate2D coordinate = ((CLLocation *)[waypoints objectAtIndex:idx]).coordinate;

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

        NSLog(@"Routing Waypoint:\t%f\t%f\t%.02f\t%.02f", point.x, point.y, coordinate.latitude, coordinate.longitude);
        pointArr[idx] = point;
    }

    // create the polyline based on the array of points.
    polyLine = [MKPolyline polylineWithPoints:pointArr count:(waypoints.count)];
    if([self.presentingViewController isKindOfClass:[FirstViewController class]])
        [(FirstViewController*)self.presentingViewController addRouteLine:polyLine];

    free(pointArr);
}

这将正确输出数据。我目前有两个“路由航点”。我的输出是:

路由航点:145449142.044444 161268376.039062 -34.07 15.06
路由航点:142917646.563556 157564926.760068 -29.86 11.67

这清楚地表明这两个点是具有实际坐标等的有效点。


所以它创建了具有有效坐标的折线。调用该方法来绘制该折线。然后它也到达了将折线添加为视图的地步。

为什么地图上不显示?请帮忙!

4

0 回答 0