gpt4 book ai didi

ios - MKOverlayRenderer 不渲染

转载 作者:塔克拉玛干 更新时间:2023-11-02 10:05:37 28 4
gpt4 key购买 nike

我无法让 MKPolygonRenderer 出现在我的 map 上。我有一个包含 MKMapViewMapViewController 类,并创建 CustomMapOverlay 实例以在 MKMapView 上呈现。

MapViewController.m:

- (void)viewDidLoad {
[super viewDidLoad];
self.mapView.delegate = self;
self.mapView.showsUserLocation = YES;
}

// ...

// Later, I retrieve some models and generate CustomMapOverlay instances from them...
for (Model *model in models) {
CustomMapOverlay *customMapOverlay = [[CustomMapOverlay alloc] initWithModel:model];
[self.mapView addOverlay:customMapOverlay];
}

// ...

// Implement MKMapViewDelegate protocol
- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay {
MKPolygonRenderer *polygonRenderer = [[MKPolygonRenderer alloc] initWithOverlay:overlay];
polygonRenderer.lineWidth = 2;
polygonRenderer.strokeColor = [UIColor colorWithRed:0.0 green:0.5 blue:1.0 alpha:1.0];
polygonRenderer.fillColor = [UIColor colorWithRed:0.0 green:0.5 blue:1.0 alpha:0.5];
return polygonRenderer;
}

CustomMapOverlay.m:

@implementation CustomMapOverlay

// Required by MKOverlay protocol
@synthesize coordinate,
boundingMapRect;

- (instancetype)initWithModel:(Model *)model {
coordinate = CLLocationCoordinate2DMake(model.latitude, model.longitude);
double radiusInPoints = MKMapPointsPerMeterAtLatitude(model.latitude) * model.radius;
boundingMapRect = MKMapRectMake(model.latitude, model.longitude, radiusInPoints, radiusInPoints);
return self;
}

@end

mapView:rendererForOverlay 被调用,在调试器中检查 overlay 我看到 map 当前屏幕边界内的 coordinate以及看起来合理的 boundingMapRect(虽然我不确定“ map 点”是什么,但我相信 MKMapPointsPerMeterAtLatitude 方法会按照它所说的去做) .

但是, map 上没有出现多边形。


更新:

我现在意识到我正在尝试渲染多边形而不创建它们。然后,我现在生成 MKPolygon 叠加层,而不是 CustomMapOverlay:

CLLocationCoordinate2D centerCoordinate = CLLocationCoordinate2DMake(model.latitude, model.longitude);
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(centerCoordinate, model.radius, model.radius);

int numCoords = 4;
CLLocationCoordinate2D *coords = malloc(sizeof(CLLocationCoordinate2D) * numCoords);
coords[0] = CLLocationCoordinate2DMake((region.center.longitude - 0.5*region.span.longitudeDelta), (region.center.latitude + 0.5*region.span.latitudeDelta));
coords[1] = CLLocationCoordinate2DMake((region.center.longitude + 0.5*region.span.longitudeDelta), (region.center.latitude + 0.5*region.span.latitudeDelta));
coords[2] = CLLocationCoordinate2DMake((region.center.longitude + 0.5*region.span.longitudeDelta), (region.center.latitude - 0.5*region.span.latitudeDelta));
coords[3] = CLLocationCoordinate2DMake((region.center.longitude - 0.5*region.span.longitudeDelta), (region.center.latitude - 0.5*region.span.latitudeDelta));
MKPolygon *polygon = [MKPolygon polygonWithCoordinates:coords count:numCoords];
free(coords);

[self.mapView addOverlay:polygon];

但是,现在根本不再调用 mapView:rendererForOverlay

最佳答案

在创建 MKPolygon 的更新代码中,coords 数组中的坐标是反向的。例如,这一行:

coords[0] = CLLocationCoordinate2DMake(
(region.center.longitude - 0.5*region.span.longitudeDelta),
(region.center.latitude + 0.5*region.span.latitudeDelta));

应该是:

coords[0] = CLLocationCoordinate2DMake(
(region.center.latitude + 0.5*region.span.latitudeDelta,
(region.center.longitude - 0.5*region.span.longitudeDelta));

CLLocationCoordinate2DMake函数中,第一个参数是latitude,然后是longitude


因为坐标是向后的,所以它们可能完全无效或位于错误的位置。

rendererForOverlay 委托(delegate)方法只会在覆盖层的 boundingMapRect(MKPolygon 将根据给定坐标自动定义)位于当前显示的 map 区域。但如果坐标无效或位置错误,boundingMapRect 也将无效。



顺便说一句,在使用 CustomMapOverlay的原始代码中,至少存在以下两个问题:

  1. initWithModel 方法不调用 [super init](假设它是一个 NSObject 子类)。
  2. boundingMapRect 计算不正确。 MKMapRectMake 函数采用 MKMapPoint 值,但代码以度为单位传递纬度和经度。 MKMapPointCLLocationCoordinate2D 不同。您可以使用 MKMapPointForCoordinate 函数将 CLLocationCoordinate2D 转换为 MKMapPoint。参见 MKMapPointForCoordinate returning invalid coordinatesMap Coordinate Systems in the documentation了解更多信息。

关于ios - MKOverlayRenderer 不渲染,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23103165/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com