在mapview上绘制折线不显示

时间:2011-11-16 11:44:17

标签: iphone ios ipad mkmapview polyline

我正在尝试使用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
  设置行规格后


[初步问题]

我将以相反的方式发布我的代码:

的MapView: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

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


因此它创建了具有有效坐标的折线。调用方法绘制折线。然后它也会到达将折线添加为视图的位置。

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

0 个答案:

没有答案