繁体   English   中英

MapKit Polyline自定义缩放?

[英]MapKit Polyline custom zoom?

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

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

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

MKMapPoint * malloc / assign:

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,它将返回您将设置为mapView的MKCoordinateRegion:

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

希望这可以帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM