Domanda

I'm just overriding a MKOverlay so I'm looking for good practice in Apple developers sample code ... When I see this :

    // bite off up to 1/4 of the world to draw into.
    MKMapPoint origin = points[0];
    origin.x -= MKMapSizeWorld.width / 8.0;
    origin.y -= MKMapSizeWorld.height / 8.0;
    MKMapSize size = MKMapSizeWorld;
    size.width /= 4.0;
    size.height /= 4.0;
    boundingMapRect = (MKMapRect) { origin, size };
    MKMapRect worldRect = MKMapRectMake(0, 0, MKMapSizeWorld.width, MKMapSizeWorld.height);
    boundingMapRect = MKMapRectIntersection(boundingMapRect, worldRect);

Many questions

  • Why Apple do the intersection with worldRect after calculate the good boundingMapRect ?
  • By Security ?
  • boundingMapRect inter worldRect isn't always boundingMapRect ?
  • If I have two MKMapPoint represent my CGRect, I haven't to do it, right ?

Regards,


EDIT

so I have just to do like this ?

// set the smallest rectangle which able to contain this overlay
MKMapPoint leftTopPoint = MKMapPointForCoordinate(leftTopCorner);
MKMapPoint rightBottomPoint = MKMapPointForCoordinate([self bottomRightCoord]);

boundingMapRect = MKMapRectMake(leftTopPoint.x, leftTopPoint.y,
                                fabs(leftTopPoint.x - rightBottomPoint.x),
                                fabs(leftTopPoint.y - rightBottomPoint.y));
È stato utile?

Soluzione

That code is from the Breadcrumb sample which is perhaps a little unusual compared to most overlays in that it's dynamically updated. They are actually setting the bounding area to be a quarter of the world area which is a bit unusual in itself, normally you would expect it to be much smaller. I think the intersection is just to make sure that the resulting rect is definitely inside the full world rect. I'm not sure I'd recommend this code as best practice unless you are doing something similar (i.e. creating a dynamic overlay). Presumably even this code could be caught out if you were to run the Breadcrumb app while flying around the world non-stop, or maybe even just crossing the international dateline...

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top