Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7168495
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T14:47:31+00:00 2026-05-28T14:47:31+00:00

I am working with viewports and bounds returned from Google Geocoding API . When

  • 0

I am working with viewports and bounds returned from Google Geocoding API. 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.

I’ve settled on algorithm that compares the ratios of areas (in MKMapPoint²) of the location viewport, current map viewport and their intersection (using MKMapRectIntersection function). This works very well as long as the location viewport does not span the 180 meridian. In that case their intersection is 0.

I’ve started to investigate the cause and as debugging aid I do display MKPolygon 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 MKMapRect, I have constructed the polygon overlay using original coordinates from Google results like this:

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];

For example of problematic location, here is the viewport returned for United States, last result of type country, when geocoding coordinates somewhere in Virginia:

Southwest: 18.9110643, 172.4546967  
Northeast: 71.3898880, -66.9453948

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):

Viewport of USA overlayed on map
Viewport of Russia overlayed on map

Similarily, displaying location viewport for Russia shows the rectangle positioned incorrectly to the left of the border of Russia.

This visually confirms there is similar problem going on, when I convert the location viewport to MKMapPoints and MKMapRect and find no intersection between the map viewport (white rectangle in the picture above) and the location viewport.

The way I compute the map rect is similar to answers in this SO question:
How to fit a certain bounds consisting of NE and SW coordinates into the visible map view?
…which works fine unless the coordinates span the 180th meridian. Testing the MKMapRect with MKMapRectSpans180thMeridian return false, so that construction method is incorrect.

Apple documentation is not helpful in this regards. Only hint I’ve found is in MKOverlay.h:

// 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;

What is the correct way to display the polygon overlay that span the 180th meridian?
How to correctly construct MKMapRect that spans 180th meridian?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T14:47:33+00:00Added an answer on May 28, 2026 at 2:47 pm

    According to that comment in MKOverlay.h, if the nw and sw corners were specified as negative MKMapPoint values, the overlay should be “drawn correctly”.

    If we try this:

    //calculation of the nw, ne, se, and sw coordinates goes here
    
    MKMapPoint points[4];
    if (nw.longitude > 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];
    

    The resulting p.boundingMapRect does return YES for MKMapRectSpans180thMeridian (but the code already figured that out from the coordinates since it didn’t have the maprect to begin with).

    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.

    Apparently, the built-in MKPolygonView does not call MKMapRectSpans180thMeridian and draw the polygon in two parts.

    You can create a custom overlay view and do this drawing yourself (you’d create one overlay but the view would draw two polygons).

    Or, you could just create two MKPolygon overlays and let the map view draw them by adding the following after the above 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];
    }
    

    By the way, there is a similar issue with drawing MKPolyline overlays that cross +/-180 (see this question).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Working with an Oracle 9i database from an ASP.NET 2.0 (VB) application using OLEDB.
I'm working on a project using Google Maps v3 that will allow users to
I've been working on this for the past several days and have been stuck.
I am working on one iPhone app which needs latitude and longitude from address.
I'm working on some Google Web Toolkit Code that places an AbsolutePanel on top
I am working on phonegap with android. i want to download a file from
I am working with some larger vertice values which I have parsed from a
I am working with Google Maps and I want to load the map in
I'm working on a website and have been using absolute positioning, since the viewport
Working with dates in ruby and rails on windows, I'm having problems with pre-epoch

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.