2012-12-19 41 views
5

我想了解如何使用折线连接ios6中地图上的两个点。首先,我已经阅读了关于这个主题的每个教程,一个简单的Google搜索就会出现,并且由于某个原因无法获得多段线。我看过的每个教程总是将折线添加到地图并调整地图的缩放以适合整条线。如果我想让地图以一个恒定的距离保持放大状态,并且只显示折线的结束(如果它大于当前视图),那么我将如何在ios6中制作和添加折线到地图? 例如说我有一个折线,这是一英里长,并希望在constand distacne equivelent对保持放大地图:MapKit折线自定义缩放?

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

我怎么会去这样做呢?请提供完整的代码示例或我可以下载的示例项目!

+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,你将设置为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; 
} 

希望这会有所帮助。