2012-01-10 35 views
1

我有一個折線圖像,但我想在地圖視圖中添加這個多線圖像。我已經完成了這方面的研究,儘管努力嘗試也沒有發現任何相關的內容我們如何在iPhone地圖視圖中將圖像添加爲多段線?

這裏是我的代碼

CGSize polyimagesize = CGSizeMake(768, 1024); 

    UIGraphicsBeginImageContextWithOptions(polyimagesize, NO, 0); 
    CGContextRef context = UIGraphicsGetCurrentContext(); 
    CGContextSetLineWidth(context, 2); 
    // CGContextSetAlpha(context, 0.6); 
    MKMapPoint *pointArr = malloc(sizeof(CLLocationCoordinate2D) * segments.count); 

    for(NSDictionary *route in segments) { 

     NSString *locations = [route valueForKey:@"Locations"]; 
     double speed = [[route valueForKey:@"Speed"] doubleValue]; 
     NSString *roadNumber = [route valueForKey:@"RoadNumber"]; 

     if ([roadNumber isEqualToString:@"A"] && aRoadFlag == NO) { 
      continue;    
     } 
     else if ([roadNumber isEqualToString:@"N"] && nRoadFlag == NO) { 
      continue; 
     } 
     else if ([roadNumber isEqualToString:@"Others"] && othersRoadFlag == NO) { 
      continue; 
     }    

     if (locations && ([locations length]/16 > 1)) {  

      UIColor *color; 
      if (speed <= 20) { 
       color = [UIColor colorWithRed:222/255.0 green:0/255.0 blue:0/255.0 alpha:1.0]; 
      } 
      else if (speed <= 40) { 
       color = [UIColor colorWithRed:253/255.0 green:91/255.0 blue:2/255.0 alpha:1.0]; 
      } 
      else if (speed <= 60) { 
       color = [UIColor colorWithRed:253/255.0 green:145/255.0 blue:4/255.0 alpha:1.0]; 
      } 
      else if (speed <=80) { 
       color = [UIColor colorWithRed:255/255.0 green:212/255.0 blue:4/255.0 alpha:1.0]; 
      } 
      else if (speed >80) { 
       color = [UIColor colorWithRed:42/255.0 green:176/255.0 blue:39/255.0 alpha:1.0]; 
      } 

      CGContextSetStrokeColorWithColor(context, color.CGColor); 

      for (int i = 0; i <= locations.length - 32; i += 32) { 

       CLLocationCoordinate2D coordinates; 
       coordinates.latitude = hexDecode1([locations substringWithRange:NSMakeRange(i, 16)]); 
       coordinates.longitude = hexDecode1([locations substringWithRange:NSMakeRange(i+16, 16)]); 
       MKMapPoint point1 = MKMapPointForCoordinate(coordinates); 

       NSLog(@"coordinates.latitude=%g",coordinates.latitude); 

       CGPoint point = [mapView convertCoordinate:coordinates toPointToView:self.mapView]; 

       if (i == 0) 
       { 
        CGContextMoveToPoint(context, point.x, point.y); 
       } 
       else{ 
        CGContextAddLineToPoint(context, point.x, point.y); 
       } 

       pointArr[i] = point1; 
       i++; 
      } 


      CGContextStrokePath(context); 
     }   
    } 


    polyimage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

我可以在背景中得出折線圖像,然後得到我的形象這方面。但現在我想在地圖視圖中將此圖像添加爲多段線。

請幫我, 在此先感謝。

回答

1

簡單地閱讀蘋果文檔。

ANYWAY:

//分配在C:

int count = ... // number or points. 
CLLocationCoordinate2D* arrayOfPoints =(CLLocationCoordinate2D*)malloc(sizeof(CLLocationCoordinate2D) * (1+count)); 
.... // fill a std C array... NOTE the +1, as we must add a closing point equal to the first. 

    // now add first to close: 
arrayOfPoints[count] = arrayOfPoints[0]; 

MKPolyline * myPolyLine = [MKPolyline polylineWithCoordinates:arrayOfPoints count: count+1]; 
[myMapView addOverlay: myPolyLine]; 
free(arrayOfPoints); 

...

// You need a call back: 

-(MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id)overlay 
{ 
MKPolylineView *polylineView = [[[MKPolylineView alloc] initWithPolyline:overlay] autorelease]; 
polylineView.strokeColor = [UIColor greenColor]; 
polylineView.lineWidth = 12.0; 
return polylineView;  
} 
+0

謝謝,這是我尋找的最佳答案。我不知道爲什麼我想念蘋果文件。順便謝謝。 – Nit 2012-02-01 03:27:55

+0

很高興你發現它很有用。 – ingconti 2012-08-31 16:24:01

0

看到這個樣本: https://github.com/wlach/nvpolyline

+0

我已經看到,但如果你有成千上萬的無段(路由)比你當您的繪圖正在進行時,地圖不響應(2秒)。因此,我首先創建了折線圖像,並且比我想要將其添加到地圖視圖中。 – Nit 2012-01-10 13:10:54

相關問題