MapKit折线自定义缩放?

5
我正在学习如何在iOS6中使用折线将地图上的两个点连接起来。首先,我已经阅读了谷歌搜索到的所有关于这个主题的教程,但由于一个原因,我无法使折线起作用。我看到的每个教程总是将折线添加到地图上,并调整地图的缩放级别以适应整条线路。如果我想要将折线制作并添加到地图上,并且希望地图始终保持缩放距离不变,仅在当前视图范围内显示折线的末端,该怎么做呢?
例如,假设我有一条长度为一英里的折线,希望地图始终保持缩放距离的等效值:
MKCoordinateRegion userRegion = MKCoordinateRegionMakeWithDistance(self.currentLocation.coordinate, 1000, 1000);
    [self.mainMap setRegion:[self.mainMap regionThatFits:userRegion] animated:YES];

我该如何操作呢?请提供完整的示例代码或可下载的项目样例!

你想要的是保持一个缩放级别,显示你当前的位置和你的折线视图吗? - james075
1个回答

0

MKMapPoint * 分配内存 / 赋值:

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,它将返回一个MKCoordinateRegion,您将设置为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;
}

希望这能有所帮助。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接