【问题标题】:MKOverlayRenderer not renderingMKOverlayRenderer 不渲染
【发布时间】:2014-05-30 22:55:49
【问题描述】:

我无法让 MKPolygonRenderer 出现在我的地图上。我有一个包含MKMapView 的类MapViewController,并创建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,我在地图当前的屏幕范围内看到coordinate,看起来合理的boundingMapRect(虽然我不确定是什么“地图点”是,我相信 MKMapPointsPerMeterAtLatitude 方法会按照它所说的去做)。

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


更新:

我现在意识到我正在尝试渲染多边形而不创建它们。那么,我现在生成的不是CustomMapOverlay,而是像这样的MKPolygon 叠加层:

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 根本不再被调用。

【问题讨论】:

    标签: ios mkmapview mkoverlay mkpolygon


    【解决方案1】:

    在创建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


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

    只有当覆盖层的boundingMapRectMKPolygon 将根据给定坐标自动定义)位于地图的当前显示区域时,才会调用rendererForOverlay 委托方法。但如果坐标无效或位置错误,boundingMapRect 也会无效。



    顺便说一句,在使用CustomMapOverlay的原始代码中,至少存在以下两个问题:
    1. initWithModel 方法不调用 [super init](假设它是 NSObject 子类)。
    2. boundingMapRect 计算不正确。 MKMapRectMake 函数采用 MKMapPoint 值,但代码以度为单位传递纬度和经度。 MKMapPointCLLocationCoordinate2D 不同。您可以使用MKMapPointForCoordinate 函数将CLLocationCoordinate2D 转换为MKMapPoint。有关更多信息,请参阅 MKMapPointForCoordinate returning invalid coordinatesMap Coordinate Systems in the documentation

    【讨论】:

    • 我一定很累才犯了这个经纬度错误。也很高兴了解 boundingMapRect 的用途。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-03-20
    • 1970-01-01
    • 2020-09-17
    • 1970-01-01
    • 2017-09-17
    • 2011-03-19
    • 2014-10-06
    相关资源
    最近更新 更多