Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>According to that comment in <code>MKOverlay.h</code>, if the nw and sw corners were specified as negative <code>MKMapPoint</code> values, the overlay should be "drawn correctly".</p> <p>If we try this:</p> <pre><code>//calculation of the nw, ne, se, and sw coordinates goes here MKMapPoint points[4]; if (nw.longitude &gt; ne.longitude) //does it cross 180th? { //Get the mappoint for equivalent distance on //the "positive" side of the dateline... points[0] = MKMapPointForCoordinate( CLLocationCoordinate2DMake(nw.latitude, -nw.longitude)); //Reset the mappoint to the correct side of the dateline, //now it will be negative (as per Apple comments)... points[0].x = - points[0].x; } else { points[0] = MKMapPointForCoordinate(nw); } points[1] = MKMapPointForCoordinate(ne); points[2] = MKMapPointForCoordinate(se); points[3] = MKMapPointForCoordinate(sw); points[3].x = points[0].x; //set to same as NW's whether + or - MKPolygon *p = [MKPolygon polygonWithPoints:points count:4]; [mapView addOverlay:p]; </code></pre> <p>The resulting <code>p.boundingMapRect</code> does return <code>YES</code> for <code>MKMapRectSpans180thMeridian</code> (but the code already figured that out from the coordinates since it didn't have the maprect to begin with).</p> <p>Unfortunately, however, creating the maprect with the negative values fixes only half the problem. The half of the polygon that is east of the dateline is now drawn correctly. However, the other half on the west of the dateline does not get drawn at all.</p> <p>Apparently, the built-in <code>MKPolygonView</code> does not call <code>MKMapRectSpans180thMeridian</code> and draw the polygon in two parts.</p> <p>You can create a custom overlay view and do this drawing yourself (you'd create one overlay but the view would draw two polygons).</p> <p>Or, you could just create two <code>MKPolygon</code> overlays and let the map view draw them by adding the following after the above code:</p> <pre><code>if (MKMapRectSpans180thMeridian(p.boundingMapRect)) { MKMapRect remainderRect = MKMapRectRemainder(p.boundingMapRect); MKMapPoint remPoints[4]; remPoints[0] = remainderRect.origin; remPoints[1] = MKMapPointMake(remainderRect.origin.x + remainderRect.size.width, remainderRect.origin.y); remPoints[2] = MKMapPointMake(remainderRect.origin.x + remainderRect.size.width, remainderRect.origin.y + remainderRect.size.height); remPoints[3] = MKMapPointMake(remainderRect.origin.x, remainderRect.origin.y + remainderRect.size.height); MKPolygon *remPoly = [MKPolygon polygonWithPoints:remPoints count:4]; [mapView addOverlay:remPoly]; } </code></pre> <p>By the way, there is a similar issue with drawing <code>MKPolyline</code> overlays that cross +/-180 (see <a href="https://stackoverflow.com/questions/5840548/unable-to-draw-mkpolylineview-crossing-longitude-180">this question</a>).</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload