2012-06-06 41 views
25

tôi có một loạt các điểm được vẽ trên bản đồ, nó đã được giải mã:Làm thế nào để vẽ một MKPolyline trên MapView?

- (void) drawRoute:(NSArray *) path { 
    NSInteger numberOfSteps = path.count; 

    CLLocationCoordinate2D coordinates[numberOfSteps]; 
    for (NSInteger index = 0; index < numberOfSteps; index++) { 
     CLLocation *location = [path objectAtIndex:index]; 
     CLLocationCoordinate2D coordinate = location.coordinate; 

     coordinates[index] = coordinate; 
    } 

    MKPolyline *polyLine = [MKPolyline polylineWithCoordinates:coordinates count:numberOfSteps]; 
    [map addOverlay:polyLine]; 
} 

nơi "bản đồ" là một thể hiện của MKMapView, và đường dẫn các mảng đại diện cho các thiết lập đã được giải mã điểm.

Tôi nghĩ rằng với dòng [map addOverlay:polyLine]; nó sẽ được rút ra. Tôi đã thấy trong một số trang phương pháp này:

- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay { 
    MKPolylineView *polylineView = [[MKPolylineView alloc] initWithPolyline:overlay]; 
    polylineView.strokeColor = [UIColor redColor]; 
    polylineView.lineWidth = 1.0; 

    return polylineView; 
} 

PolylineView có thực sự được vẽ trên bản đồ không? Tôi cũng đã cố gắng để vượt qua MKPolyline (từ phương pháp trên) đến đối số "< MKOverlay> overlay" của phương pháp cuối cùng này, nhưng ném một ngoại lệ.

Tôi nghĩ tôi thân thiết, nhưng tôi không biết phải làm gì bây giờ.

Vui lòng trợ giúp! Cảm ơn bạn rất nhiều trước.

+2

Lưu ý rằng mã này hiện đã lỗi thời trên iOS7. –

Trả lời

13

Done.

Là một điều rất ngu ngốc, tôi đã không đặt đại biểu cho MapView. Chỉ cần thêm [map setDelegate: self]; đã làm các trick.

Cảm ơn bạn!

4

Chỉ cần tạo MKPolyline với tọa độ & thêm polyLine đó vào chế độ xem bản đồ.

MKPolyline *polyLine = [MKPolyline polylineWithCoordinates:coordinates count:numberOfSteps]; 
    [map addOverlay:polyLine]; 

Bạn sẽ tìm thấy hướng dẫn here về cách vẽ polyline trên một số tọa độ. Chỉnh sửa: Url không có vẻ hợp lệ nữa. Bạn có thể tìm thấy phiên bản được lưu trữ của url này here.

+0

Đây là liên kết đến phiên bản được lưu trữ của nó: http://web.archive.org/web/20120906022305/http://navarra.ca/?p=786 – Brian

Các vấn đề liên quan