경로를 생성하여 오버레이로 추가하는 방법은 MKMapView
입니다. 나는 MKPolylineView
을 사용하려고합니다. 이것은 MKOverlayPathView
의 하위 클래스이며, (경로 데이터 포함)을 만들고 MKPolylineView
(시각적 표현을 만드는 데 사용하기 때문에 CGPath
을 참조하지 않아도됩니다. 지도에있는 데이터의).
MKPolyline
은 C 배열의 점 (MKMapPoint
) 또는 C 배열의 좌표 (CLLocationCoordinate2D
)로 만들어야합니다. MapKit이 NSArray
과 같은 고급 데이터 구조를 사용하지 않는다는 것은 유감이지만 그렇게해야합니다! NSArray
또는 NSMutableArray
이 CLLocation
인 것으로 가정하여 MKPolyline
에 적합한 데이터의 C 배열로 변환하는 방법을 보여줄 것입니다. 이 배열의 이름은 locations
이며 입력 방법은 앱에 의해 결정됩니다 (예 : self.polyline인지
int numPoints = [locations count];
if (numPoints > 1)
{
CLLocationCoordinate2D* coords = malloc(numPoints * sizeof(CLLocationCoordinate2D));
for (int i = 0; i < numPoints; i++)
{
CLLocation* current = [locations objectAtIndex:i];
coords[i] = current.coordinate;
}
self.polyline = [MKPolyline polylineWithCoordinates:coords count:numPoints];
free(coords);
[mapView addOverlay:self.polyline];
[mapView setNeedsDisplay];
}
참고 등 웹 서비스 요청 MKMapView
담당하고 뷰 컨트롤러
다운로드 데이터와 사용자에 의해 터치 위치를 가공하여 기입 또는 가득 .H에 선언하는 것은 같이
- (MKOverlayView*)mapView:(MKMapView*)theMapView viewForOverlay:(id <MKOverlay>)overlay
{
MKPolylineView* lineView = [[[MKPolylineView alloc] initWithPolyline:self.polyline] autorelease];
lineView.fillColor = [UIColor whiteColor];
lineView.strokeColor = [UIColor whiteColor];
lineView.lineWidth = 4;
return lineView;
}
:
@property (nonatomic, retain) MKPolyline* polyline;
이보기 컨트롤러는 MKMapViewDelegate
방법을 구현해야합니다
fillColor, strokeColor 및 lineWidth 속성으로 재생하여 앱에 적합하도록 할 수 있습니다. 나는 방금 단순하고 적당히 넓은 평범한 흰 선을 들고 나갔다.
지도에서 경로를 삭제하려는 경우.
[mapView removeOverlay:self.polyline];
self.polyline = nil;
을 한 다음 새 MKPolyline를 만들고지도에 추가 할 수있는 위의 과정을 반복 : 새로운 좌표로 업데이트하기 위해, 당신은 할 것이다.
MapKit은 조금 복잡하고 보잘 것없는 것처럼 보일 수 있지만이 예제에서 설명한 것처럼 쉽게 할 수 있습니다. 적어도 C 프로그래머가 아닌 사람에게는 무서운 유일한 비트는 malloc을 사용하여 버퍼를 만들고 배열 구문을 사용하여 CLLocationCoordinates를 복사 한 다음 나중에 메모리 버퍼를 해제하는 것입니다.
MKOverlayPathView에는 CGPathRef로 직접 설정하는'-path' 속성이 있습니다. CGPathRef를 만드는 방법에 대해 혼란 스럽습니까? –