MapKit Polyline自定义缩放?

Shr*_*794 5 iphone mapkit polyline ipad ios

我正在尝试学习如何使用折线连接ios6中地图上的两个点.首先,我已经阅读了关于这个主题的每个教程,简单的Google搜索出现了,并且由于一个原因无法使折线工作.我看过的每个教程都会将折线添加到地图中,并调整地图的缩放以适应整条线.如果我希望地图在恒定距离内保持放大并且只显示折线的末端(如果它比当前视图大),我将如何在ios6中制作和添加折线?例如,假设我有一条长达一英里的折线,并希望地图保持在一个constand distacne equoomlent放大:

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

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

jam*_*075 0

MKMapPoint * malloc /分配:

MKMapPoint *newPoints = malloc((sizeof (MKMapPoint) * nbPoints));
newPoints[index] = varMKMapPoint;
free(newPoints);
Run Code Online (Sandbox Code Playgroud)

MKPolyline 必须在您需要的情况下初始化:

MKPolyline *polyline  = [MKPolyline polylineWithPoints:newPoints count:nbPoints];
[self.mapView addOverlay:polyline];
Run Code Online (Sandbox Code Playgroud)

要显示 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;
}
Run Code Online (Sandbox Code Playgroud)

要使用此方法,您必须将点转换为 CLLocation,它将返回您将设置为 mapView 的 MKCooperativeRegion :

- (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;
}
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助。