2012-12-19 3 views
5

폴리선을 사용하여 ios6의지도에서 두 지점을 연결하는 방법을 배우려고합니다. 먼저,이 주제에 대한 모든 자습서를 읽었습니다. 간단한 Google 검색이 나타나고 폴리 라인이 한 가지 이유로 작동하지 않습니다. 제가 본 모든 튜토리얼은 항상 폴리 라인을 맵에 추가하고 전체 라인에 맞게 맵의 줌을 조정합니다. 일정한 거리에서 확대 된지도를 유지하고 현재보기보다 큰 경우에만 폴리 라인의 끝만 표시하려면 ios6의지도에 폴리선을 추가하고 추가하는 방법은 무엇입니까?MapKit Polyline 사용자 정의 줌?

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

가 어떻게 이렇게 가겠어요 : 예를 들어 내가 마일 긴했다 폴리 라인을했고에 constand의 distacne의 equivelent에서 확대 머물 수있는지도를 원한다고? 다운로드 할 수있는 전체 코드 예제 또는 샘플 프로젝트를 제공하십시오!

+0

현재 위치와 polylineView를 보여주는 줌을 유지하고 싶습니까? – james075

답변

0

MKMapPoint *의 malloc/할당 :

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

MKPolyline가에 초기화해야 당신의 필요 :

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

이 MKPolyline을 표시하기 위해, 당신은 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; 
} 

이 방법을 사용하려면 포인트를 CLLocation으로 변환해야하며 MKCoordinateR을 반환합니다. 당신이지도로 설정하는 egion :

- (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; 
} 

희망이 있습니다.

관련 문제