Note that there are some explanatory texts on larger screens.

plurals
  1. POMKMapRect and displaying map overlays that span 180th meridian
    primarykey
    data
    text
    <p>I am working with viewports and bounds returned from <em>Google Geocoding API</em>. When doing reverse geocoding for a given coordinate, the service returns several results with various granularity (country, administrative area, locality, sublocality, route, etc.). I want to select the most appropriate on the results given the current visible area on the map.</p> <p>I've settled on algorithm that compares the ratios of areas (in <code>MKMapPoint</code>²) of the location viewport, current map viewport and their intersection (using <code>MKMapRectIntersection</code> function). This works very well as long as the location viewport does not span the 180 meridian. In that case their intersection is 0.</p> <p>I've started to investigate the cause and as debugging aid I do display <code>MKPolygon</code> overlays on the map to give me visual clues as to what is going on. To avoid possible errors introduced by my code that does conversion between geo-coordinates and <code>MKMapRect</code>, I have constructed the polygon overlay using original coordinates from Google results like this:</p> <pre><code>CLLocationCoordinate2D sw, ne, nw, se; sw = location.viewportSouthWest.coordinate; ne = location.viewportNorthEast.coordinate; nw = CLLocationCoordinate2DMake(ne.latitude, sw.longitude); se = CLLocationCoordinate2DMake(sw.latitude, ne.longitude); CLLocationCoordinate2D coords[] = {nw, ne, se, sw}; MKPolygon *p = [MKPolygon polygonWithCoordinates:coords count:4]; </code></pre> <p>For example of problematic location, here is the viewport returned for United States, last result of type <em>country</em>, when <a href="http://maps.googleapis.com/maps/api/geocode/xml?latlng=37.439974,-78.574219&amp;sensor=false" rel="nofollow noreferrer">geocoding coordinates somewhere in Virginia</a>: </p> <pre><code>Southwest: 18.9110643, 172.4546967 Northeast: 71.3898880, -66.9453948 </code></pre> <p>Notice how the southwest coordinate, which is in the lower left corner of the location viewport lies across the 180 meridian. When displaying this location overlayed as polygon on the map it displays incorrectly to the right of USA borders (big brown rectangle, only lower left corner visible): </p> <p><img src="https://i.stack.imgur.com/iVOML.png" alt="Viewport of USA overlayed on map"> <img src="https://i.stack.imgur.com/GG6k8.png" alt="Viewport of Russia overlayed on map"></p> <p>Similarily, displaying <a href="http://maps.googleapis.com/maps/api/geocode/xml?latlng=58.447733,31.640625&amp;sensor=false" rel="nofollow noreferrer">location viewport for Russia</a> shows the rectangle positioned incorrectly to the left of the border of Russia.</p> <p>This visually confirms there is similar problem going on, when I convert the location viewport to <code>MKMapPoint</code>s and <code>MKMapRect</code> and find no intersection between the map viewport (white rectangle in the picture above) and the location viewport.</p> <p>The way I compute the map rect is similar to answers in this SO question:<br> <a href="https://stackoverflow.com/questions/8496551/how-to-fit-a-certain-bounds-consisting-of-ne-and-sw-coordinates-into-the-visible">How to fit a certain bounds consisting of NE and SW coordinates into the visible map view?</a><br> ...which works fine <em>unless</em> the coordinates span the 180th meridian. Testing the <code>MKMapRect</code> with <code>MKMapRectSpans180thMeridian</code> return <code>false</code>, so that construction method is incorrect.</p> <p>Apple documentation is not helpful in this regards. Only hint I've found is in <code>MKOverlay.h</code>:</p> <pre><code>// boundingMapRect should be the smallest rectangle that completely contains // the overlay. // For overlays that span the 180th meridian, boundingMapRect should have // either a negative MinX or a MaxX that is greater than MKMapSizeWorld.width. @property (nonatomic, readonly) MKMapRect boundingMapRect; </code></pre> <p>What is the correct way to display the polygon overlay that span the 180th meridian?<br> How to correctly construct <code>MKMapRect</code> that spans 180th meridian?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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