我有一个MKMapView
和一个MKTileOverlay
,因此我可以显示“开放街道地图”图块:
NSString *templateURL = @"http://tile.openstreetmap.org/{z}/{x}/{y}.png";
self.tileOverlay = [[MKTileOverlay alloc] initWithURLTemplate:templateURL];
self.tileOverlay.canreplaceMapContent = YES;
[self.mapView addOverlay:self.tileOverlay level:MKOverlayLevelAboveLabels];
我还想显示一个从我当前位置到库比蒂诺苹果公园的MKPolyline
。此折线需要在移动时进行更新,并且由于MKPolyline
对象是不可变的,因此我必须将其删除并为每次位置更新添加它:
- (void)locationmanager:(CLLocationmanager *)manager didUpdateLocations:(NSArray*)locations {
self.currentLocation = userLocation;
// Update polyline
CLLocationCoordinate2D applePark = CLLocationCoordinate2DMake(37.334626,-122.008895);
[self buildPolylineWithDestinationLocation:applePark];
}
- (void)buildPolylineWithDestinationLocation:(CLLocationCoordinate2D)coordinate {
// Remove the polyline each time so we can redraw it
if (self.polylineApple) {
[self.mapView removeOverlay:self.polylineApple];
}
// Get current location
CLLocation *location = self.currentLocation;
CLLocationCoordinate2D currentLocation = location.coordinate;
CLLocationCoordinate2D points[2];
points[0] = currentLocation;
points[1] = coordinate;
// Remove all route polylines
MKPolyline *oldPolyline = self.polylineApple;
// Draw a line
self.polylineApple = [MKPolyline polylineWithCoordinates:points count:2];
[self.mapView addOverlay:self.polylineApple];
if (oldPolyline) {
[self.mapView removeOverlay:oldPolyline];
oldPolyline = nil;
}
}
问题是,这过去在旧版本的iOS中效果很好,但是自iOS 13以来,这导致每次MKPolyline
删除和添加时都会重新绘制图块:
这仅仅是iOS 13的错误,还是我需要在代码中修复某些问题才能使之不再发生?