如何使用crumb路径在地图上绘制路线?

时间:2012-06-14 09:11:43

标签: ios4

我使用过Apple CrumbPath.o和CrumbPathView.o提供的类,但它只支持iphone 5.0,当我尝试使用iphone 4.0相同的代码时,它不会更新路由。 代码:

 if (newLocation)
    {

    if (oldLocation.coordinate.latitude == 0.0) {
        initialLocation = [[[CLLocation alloc]initWithLatitude:newLocation.coordinate.latitude longitude:newLocation.coordinate.longitude]retain];
    }
    // make sure the old and new coordinates are different
    if((oldLocation.coordinate.latitude != newLocation.coordinate.latitude) &&
      (oldLocation.coordinate.longitude != newLocation.coordinate.longitude))
    {    
    if (!crumbs)
    {
        // This is the first time we're getting a location update, so create
        // the CrumbPath and add it to the map.
        //
        crumbs = [[CrumbPath alloc] initWithCenterCoordinate:newLocation.coordinate];
        [mapView addOverlay:crumbs];

        // On the first location update only, zoom map to user location
        MKCoordinateRegion region = 
        MKCoordinateRegionMakeWithDistance(newLocation.coordinate, 2000, 2000);
        [mapView setRegion:region animated:YES];
    }
    else
    {
        // This is a subsequent location update.
        // If the crumbs MKOverlay model object determines that the current location has moved
        // far enough from the previous location, use the returned updateRect to redraw just
        // the changed area.
        //
        // note: iPhone 3G will locate you using the triangulation of the cell towers.
        // so you may experience spikes in location data (in small time intervals)
        // due to 3G tower triangulation.
        // 
        Count++;
        double latitude = 0.000500 * Count;
        double longitude = 0.000020 * Count;
        _bean = [[TempBean alloc]init];
        _bean.lat = newLocation.coordinate.latitude + latitude;
        _bean.lon = newLocation.coordinate.longitude - longitude;



        CLLocation *locationToDraw = [[CLLocation alloc]initWithLatitude:_bean.lat longitude:_bean.lon];
        //                UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Update_Loc" message:[NSString stringWithFormat:@"Lat:%f , Lon:%f",locationToDraw.coordinate.latitude,locationToDraw.coordinate.longitude] delegate:self cancelButtonTitle:@"ok" otherButtonTitles:@"Cancel",nil];
        //                [alert show];
        //                [alert release];

        MKMapRect updateRect = [crumbs addCoordinate:locationToDraw.coordinate];

        if (!MKMapRectIsNull(updateRect))
        {
            // There is a non null update rect.
            // Compute the currently visible map zoom scale
            MKZoomScale currentZoomScale = (CGFloat)(mapView.bounds.size.width / mapView.visibleMapRect.size.width);
            // Find out the line width at this zoom scale and outset the updateRect by that amount
            CGFloat lineWidth = MKRoadWidthAtZoomScale(currentZoomScale);
            updateRect = MKMapRectInset(updateRect, -lineWidth, -lineWidth);
            // Ask the overlay view to update just the changed area.
            [crumbView setNeedsDisplayInMapRect:updateRect];
        }
        [self calDistance:initialLocation SecondCor:locationToDraw];
        [locationToDraw release];
        locationToDraw =nil;
        [_bean release];
        _bean = nil;


}}

1 个答案:

答案 0 :(得分:0)

如果是这种情况那么,你应该尝试查看你的CrumbPathView.m看看渲染功能是否正常工作,我在这里看到你复制的&粘贴Apple Docs的代码吧?在文件中,您应该在方法drawMapRect:zoomScale:inContext:

中找到它
if (path != nil)
{
    CGContextAddPath(context, path);
    CGContextSetRGBStrokeColor(context, 0.0f, 0.0f, 1.0f, 0.6f);
    CGContextSetLineJoin(context, kCGLineJoinRound);
    CGContextSetLineCap(context, kCGLineCapRound);
    CGContextSetLineWidth(context, lineWidth);
    CGContextStrokePath(context);
    CGPathRelease(path);
}

查看path是否为零,如果没有,则无法呈现任何内容。你调试到这个视图了吗?渲染发生在CrumbPathView中,所以你应该看看它发生了什么。

如果path为零,那么您应该检查调用的方法以分配其值,位于if (path != nil)

上方