How to fit a certain bounds consisting of NE and SW coordinates into the visible map view?

后端 未结 3 1581
迷失自我
迷失自我 2021-02-09 23:52

I need to fit a certain bounds within a map. I get the bounds from calling the google geocoder and reading the viewport property which looks like:

{
    northeas         


        
3条回答
  •  生来不讨喜
    2021-02-10 00:45

    If your bounds can span the 180th meridian, you have to account for that in the conversion:

    - (MKMapRect) mapRectThatFitsBoundsSW:(CLLocationCoordinate2D)sw 
                                       NE:(CLLocationCoordinate2D)ne 
    {
        MKMapPoint pSW = MKMapPointForCoordinate(sw);
        MKMapPoint pNE = MKMapPointForCoordinate(ne);
    
        double antimeridianOveflow = 
          (ne.longitude > sw.longitude) ? 0 : MKMapSizeWorld.width;    
    
        return MKMapRectMake(pSW.x, pNE.y, 
                            (pNE.x - pSW.x) + antimeridianOveflow, 
                            (pSW.y - pNE.y));
    }
    

    But beware those MKMapRects that span the anitmeridian, because they come from the land where the dragons live. If you want to learn about some of the dangers that lie there, have a look at MKMapRect and displaying map overlays that span 180th meridian. You have been warned!

提交回复
热议问题