2012-06-14 163 views
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; 


}} 
+0

在這方法做仰臥代碼此塊以上?它是在'locationManager:didUpdateLocations'還是'locationManager:didUpdateToLocation:FromLocation'?你應該有兩個,因爲iOS6 +將使用第一個和iOS5-後者。你有嘗試調試嗎?這些代碼塊是否被調用? – Mysteltainn

+0

中寫道的LocationManager:didUpdateToLocation:FromLocation ...是我調試....它被調用! –

回答

0

如果這樣,那麼,你應該嘗試尋找到你CrumbPathView.m看看渲染功能工作正常或不從我這裏看到你複製粘貼&來自蘋果文檔中的代碼嗎?在該文件中,你應該在方法,如果是零,沒有編獲取呈現找到這個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)

+0

@Te阿莫因爲我必須粘貼代碼塊這將是很難,如果張貼在評論閱讀我寫此評論作爲一個答案。 – Mysteltainn