2012-01-10 17 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

単にアップルのドキュメントをお読みください。とにかく

// 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

+0

うれしいあなたはそれをfounf founf。 – ingconti

0

は、このサンプルを参照してください。 https://github.com/wlach/nvpolyline

+0

私はそれを見てきましたが、あなたはあなたよりも何千人ものセグメント(ルート)の全くない場合あなたの描画がon.soされているときにマップが応答していない(2秒間)、私は最初にポリラインのイメージを作成し、私はマップビューでそれを追加したい。 – Nit

関連する問題