2012-12-19 52 views
5

Tôi đang cố gắng tìm hiểu cách sử dụng polyline để kết nối hai điểm trên bản đồ trong ios6. Trước hết, tôi đã đọc qua mọi hướng dẫn về chủ đề này mà một tìm kiếm Google đơn giản quay lên và không thể có được các polylines để làm việc vì một lý do. Mỗi hướng dẫn mà tôi đã thấy luôn luôn thêm polyline vào bản đồ và điều chỉnh độ phóng đại của bản đồ để phù hợp với toàn bộ dòng. Làm thế nào tôi sẽ đi về việc thực hiện và thêm một polyline vào một bản đồ trong ios6 nếu tôi muốn bản đồ ở lại phóng to tại một khoảng cách liên tục và chỉ hiển thị kết thúc của polyline nếu nó lớn hơn thì xem hiện tại? Ví dụ nói rằng tôi đã có một polyline đó là một dặm dài và muốn bản đồ để ở lại phóng to tại một constand distacne equivelent tới:MapKit Polyline tùy chỉnh phóng to?

MKCoordinateRegion userRegion = MKCoordinateRegionMakeWithDistance(self.currentLocation.coordinate, 1000, 1000); 
    [self.mainMap setRegion:[self.mainMap regionThatFits:userRegion] animated:YES]; 

Làm thế nào tôi sẽ đi về việc này? Vui lòng cung cấp các ví dụ về mã đầy đủ hoặc một dự án mẫu mà tôi có thể tải xuống!

+0

Điều bạn muốn là thu phóng người cho biết vị trí hiện tại của bạn và polylineView của bạn? – james075

Trả lời

0

MKMapPoint * malloc/assign:

MKMapPoint *newPoints = malloc((sizeof (MKMapPoint) * nbPoints)); 
newPoints[index] = varMKMapPoint; 
free(newPoints); 

MKPolyline phải được khởi tạo trong bạn cần:

MKPolyline *polyline = [MKPolyline polylineWithPoints:newPoints count:nbPoints]; 
[self.mapView addOverlay:polyline]; 

để hiển thị MKPolyline của bạn, bạn phải sử dụng viewForOverlay:

- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay 
{ 
    MKOverlayView* overlayView = [[MKOverlayView alloc] initWithOverlay:overlay];   
    if([overlay isKindOfClass:[MKPolyline class]]) { 
     MKPolylineView *backgroundView = [[MKPolylineView alloc] initWithPolyline:overlay]; 
     backgroundView.fillColor = [UIColor blackColor]; 
     backgroundView.strokeColor = backgroundView.fillColor; 
     backgroundView.lineWidth = 10; 
     backgroundView.lineCap = kCGLineCapSquare; 
     overlayView = backgroundView; 
    } 
    return overlayView; 
} 

Để sử dụng phương pháp này, bạn phải chuyển đổi điểm của bạn thành CLLocation, nó sẽ trả về một MKCoordinateR egion mà bạn sẽ đặt thành mapView:

- (MKCoordinateRegion)getCenterRegionFromPoints:(NSArray *)points 
{ 
    CLLocationCoordinate2D topLeftCoordinate; 
    topLeftCoordinate.latitude = -90; 
    topLeftCoordinate.longitude = 180; 
    CLLocationCoordinate2D bottomRightCoordinate; 
    bottomRightCoordinate.latitude = 90; 
    bottomRightCoordinate.longitude = -180; 
    for (CLLocation *location in points) { 
     topLeftCoordinate.longitude = fmin(topLeftCoordinate.longitude, location.coordinate.longitude); 
     topLeftCoordinate.latitude = fmax(topLeftCoordinate.latitude, location.coordinate.latitude); 
     bottomRightCoordinate.longitude = fmax(bottomRightCoordinate.longitude, location.coordinate.longitude); 
     bottomRightCoordinate.latitude = fmin(bottomRightCoordinate.latitude, location.coordinate.latitude); 
    } 
    MKCoordinateRegion region; 
    region.center.latitude = topLeftCoordinate.latitude - (topLeftCoordinate.latitude - bottomRightCoordinate.latitude) * 0.5; 
    region.center.longitude = topLeftCoordinate.longitude + (bottomRightCoordinate.longitude - topLeftCoordinate.longitude) * 0.5; 
    region.span.latitudeDelta = fabs(topLeftCoordinate.latitude - bottomRightCoordinate.latitude) * 1.2; //2 
    region.span.longitudeDelta = fabs(bottomRightCoordinate.longitude - topLeftCoordinate.longitude) * 1.2; //2 
// NSLog(@"zoom lvl : %f, %f", region.span.latitudeDelta, region.span.latitudeDelta); 
    return region; 
} 

Hy vọng điều này sẽ hữu ích.

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