简体   繁体   English

iOS 7 - 使用MKOverlayRenderer在MapKit上绘制一组coords

[英]iOS 7 - Drawing a set of coords on MapKit with MKOverlayRenderer

I am trying to take a set of GPS coordinates and draw them onto an overlay using a custom MKOverlay and MKOverlayRenderer. 我正在尝试使用一组GPS坐标并使用自定义MKOverlay和MKOverlayRenderer将它们绘制到叠加层上。 I haven't been able to get the points to show. 我无法得到要点。 I know it might not be necessary do this in this way but this is the first step of a more complex overlay. 我知道可能没有必要这样做,但这是更复杂的叠加的第一步。

Code in the overlayrenderer here. 这里的overlayrenderer中的代码。 The points array holds NSValue objects which hold my MKMapPoint structs. points数组包含保存我的MKMapPoint结构的NSValue对象。 I've confirmed those MKMapPoints are correct inside of the for loop. 我已经确认那些MKMapPoints在for循环中是正确的。

-(instancetype)initWithOverlay:(Heatmap*)overlay {
    self = [super initWithOverlay:overlay];
    if (self){
        self.points = overlay.testCoordinates;
    }
    return self;
}

- (void)drawMapRect:(MKMapRect)mapRect
          zoomScale:(MKZoomScale)zoomScale
          inContext:(CGContextRef)context
{

    CGContextSetRGBFillColor(context, 1.0, 0.0, 0.0, 1.0);

    for (NSValue *point in self.points){
        MKMapPoint mapPoint;
        [point getValue:&mapPoint];
        CGPoint datpoint = [self pointForMapPoint:mapPoint];
        CGRect rect = CGRectMake(datpoint.x/zoomScale, datpoint.y/zoomScale, 1.0/zoomScale, 1.0/zoomScale);
        CGContextFillRect(context, rect);
    }

In the view controller that is my MapView delegate: 在作为我的MapView委托的视图控制器中:

- (void)viewDidLoad
{
    Heatmap *heatmap = [[Heatmap alloc] init];
    [self.mapView setVisibleMapRect:heatmap.boundingMapRect];
    [self.mapView addOverlay:heatmap];
}

- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay
{
    HeatmapOverlayRenderer *renderer = [[HeatmapOverlayRenderer alloc] initWithOverlay:overlay];
    return renderer;
}

The boundingMapRect is set correctly. boundingMapRect设置正确。 So the app should load and I should see 100 points within the screen bounds. 所以app应该加载,我应该在屏幕边界内看到100个点。 Any idea what I might have wrong? 知道我可能有什么错吗? This is all new to me. 这对我来说都是新的。

In drawMapRect , this line is probably the cause of the point rects not showing: drawMapRect ,这一行可能是点rects未显示的原因:

CGRect rect = CGRectMake(datpoint.x/zoomScale, datpoint.y/zoomScale, 
                           1.0/zoomScale, 1.0/zoomScale);

You probably do not want to change the position of the point's rect based on the zoomScale. 您可能不希望根据zoomScale更改点的rect的位置。
The rect's apparent position based on the zoom will be done automatically by the map view. 基于缩放的rect的视在位置将由地图视图自动完成。

The current code is changing its absolute position and size on the map based on the zoom. 当前代码基于缩放在地图上更改其绝对位置和大小。
The resulting position is most likely not on the map. 结果位置很可能不在地图上。

Instead, draw the point's rect without any scaling. 相反,绘制点的矩形而不进行任何缩放。

However, to calculate the right width to use in CGPoints (from meters), you need to do some calculation: 但是,要计算在CGPoints中使用的正确宽度(从米),您需要进行一些计算:

for (NSValue *point in self.points)
{
    MKMapPoint mapPoint;
    [point getValue:&mapPoint];

    CGPoint datpoint = [self pointForMapPoint:mapPoint];


    CLLocationDistance rectWidthMeters = 100000.0;
    //above sets rect width to 100 km, adjust as needed

    CLLocationCoordinate2D mpc = MKCoordinateForMapPoint(mapPoint);

    double rectWidthMapPoints = MKMapPointsPerMeterAtLatitude(mpc.latitude);

    MKMapPoint mpRight = 
      MKMapPointMake (mapPoint.x + (rectWidthMapPoints * rectWidthMeters), 
                      mapPoint.y);

    CGPoint datpointRight = [self pointForMapPoint:mpRight];

    CGFloat rectWidth = datpointRight.x - datpoint.x;


    CGRect rect = CGRectMake(datpoint.x, datpoint.y, rectWidth, rectWidth);

    CGContextFillRect(context, rect);
}

Make sure you are zoomed in enough at the right location to see the squares. 确保在适当的位置放大,以便看到正方形。


You may be interested in looking at Apple's sample app HazardMap which demonstrates something similar showing color-coded earthquake hazard levels based on a grid of locations. 您可能有兴趣查看Apple的示例应用程序HazardMap ,该应用程序演示了基于位置网格的类似颜色编码的地震危险等级。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM