MKPolyline仅在移动地图时显示 [英] MKPolyline only shown when map is moved
问题描述
我有一个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屋!