4

我使用了苹果提供的 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;


}}
4

1 回答 1

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为 nil,如果它是 nil-ed,则什么都不会被渲染。你调试过这个视图吗?渲染发生在 CrumbPathView 中,因此您应该看到其中发生了什么。

如果path是 nil 那么你应该检查被调用来分配它的值的方法,位于上面if (path != nil)

于 2014-09-30T08:20:41.230 回答