Is there an API to find out what's the line width for 1 pixel in an overlay?

When you draw lines or shapes in an overlay, the values for the CGRect and CGPoint are scaled based on the zoomLevel (and some other incantations?).


Is there an API to figure out which line width should be set to draw a 1 pixel (or 2 pixels on a retina display) segment from the


- (void)drawMapRect:zoomScale:inContext:;


callback of a MKOverlayRendered.


Other example, it's easy to draw a shape, but what if you want to draw the outline of the shape with a 1-pixel wide stroke whatever the zoom level is?


Is it something that needs to be done entirely on our side for instance by figuring out what the AffineTransform is for the current graphic context?

Replies

You can use MKRoadWidthAtZoomScale. You may need to multiply by an empirically determined constant to get your desired line width visually. I don't know how to do it if you need *precisely* 1 point; I've always just used the road width.

I need precisely 1 point because I'm experimenting something: rendering POIs via the overlay instead of using annotation views. And I would like to render a POI as a badge (at a certain zoom level) that looks like the one used by MapKit for the standard POIs.


I'm currently using MKRoadWidthAtZoomScale to get a value big enough to draw a visible disc.

About points, I thought is was one pixel on non-retina, four on retina?


But given non-square pixels, etc., I guess converting between points and pixels doesn't make a whole lot of sense anymore.

I've finally figured out that the zoomScale parameter is what I'm looking for. It's the same value as the ones in the CGAffineTransform "matrix" for the context.

Good info, thanks for the followup and good luck w/your app(s).

Reviving an old question here looking for further details:


I've observed some behavior I can't explain with regards to line widths in overlays. I am overriding

MKOverlayPathRenderer.applyStrokeProperties(to context: CGContext, atZoomScale zoomScale: MKZoomScale)


If I zoom in all the way this gets called with zoomScale = 1.0 and I've checked that self.contentScaleFactor = 2.0 (same as UIScreen.main.scale). I am setting context.setLineWidth(10.0) and then I take a screen capture of the resulting map and inspect it in Photoshop. The lines are exactly 10 pixels wide, even though I would expect them to be 20 pixels wide. In a simple CGContext drawing situation you would expect the drawn size in pixels to be numPoints * scaleFactor.


To cross-check I've also tried converting the overlay's mapRect into the CGContext rect and the sizes are the same, indicating 1:1 ratio between MKMapPoint and CGContext points.


Any ideas? It seems like Apple is factoring the screen scale into the zoom level some how, which I can't find any documentation for.