MKPolyline仅在移动地图时显示 [英] MKPolyline only shown when map is moved

查看:160
本文介绍了MKPolyline仅在移动地图时显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个MKMapView,上面有一些引脚.我将销钉连接到MKPolyline视图.但是MKPolyline仅在我移动地图时才显示(更新MapView时?).我想从一开始就看到MKPolyline.

I have a MKMapView with some pins on it. I connect the pins with a MKPolyline view. But the MKPolyline is only shown when I move the map (when the MapView is updated?). I want to see the MKPolyline from the beginning on.

请检查以下代码:

-(void)plotSnapPosition {
    for (id<MKAnnotation> annotation in myMapView.annotations) {
        [myMapView removeAnnotation:annotation];
    }
    for (id<MKOverlay> overlay in myMapView.overlays) {
        [myMapView removeOverlay:overlay];
    }
    NSArray *snaps = self.entry.snapsArray;
    CLLocationCoordinate2D *locations = malloc(sizeof(CLLocationCoordinate2D) * snaps.count);
    NSInteger counter = 0;
    for (Snap *snap in snaps) {
        locations[counter] = [snap coordinates];
        CLLocationCoordinate2D c = [snap coordinates];
        CAHAnnotation *annotation = [[CAHAnnotation alloc] initWithDate:snap.timeAsString coordinate:c counter:counter];
        [myMapView addAnnotation:annotation];
        counter++;
    }
    MKPolyline *polyline = [MKPolyline polylineWithCoordinates:locations count:snaps.count];
    MKPolylineView *routeLineView = [[MKPolylineView alloc] initWithPolyline:polyline];
    routeLineView.fillColor = [UIColor redColor];
    routeLineView.strokeColor = [UIColor redColor];
    routeLineView.lineWidth = 5;

    [myMapView setVisibleMapRect:polyline.boundingMapRect];
    [self.myMapView addOverlay:polyline];
}

-(MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id<MKOverlay>)overlay {
    if ([overlay isKindOfClass:[MKPolyline class]]) {
        MKPolylineView *routeLineView = [[MKPolylineView alloc] initWithPolyline:overlay];
        routeLineView.fillColor = [UIColor blueColor];
        routeLineView.strokeColor = [UIColor blueColor];
        routeLineView.lineWidth = 3;
        return routeLineView;
    }

    return nil;
}

对于测试问题,我已将方法-(void)plotSnapPosition中的MKPolyline的颜色设置为红色.在代表中,我将其设置为蓝色.在四处移动地图后,仅显示蓝色的一个.

For testing issues I have set the color of the MKPolyline in the method -(void)plotSnapPosition to red. In the delegate I set it to blue. Only the blue one is shown, after moving the map around.

有人可以帮我吗?我认为这只是一个小错误.谢谢.

can someone help me out of this? I think it is only a small mistake. Thank you.

以下是屏幕截图:

两个引脚

移动地图后:

移动地图后的路径

推荐答案

请确保在添加叠加层之前,先设置mapView的委托.所以,就您而言

Make sure you set the mapView's delegate before adding the overlay. So, in your case

mapView.delegate = self;
[self plotSnapPosition];

这篇关于MKPolyline仅在移动地图时显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆