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

后端 未结 3 1583
迷失自我
迷失自我 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:54

    I found something that works. I ended up going with this:

    - (MKMapRect) mapRectThatFitsBoundsSW:(CLLocationCoordinate2D)sw 
                                       NE:(CLLocationCoordinate2D)ne {
        MKMapPoint nePoint = MKMapPointForCoordinate(ne);
        MKMapPoint swPoint = MKMapPointForCoordinate(sw);
        CGFloat width = ABS(nePoint.x - swPoint.x);
        CGFloat height = ABS(nePoint.y - swPoint.y);
        MKMapRect newMapRect = MKMapRectMake(
            MIN(swPoint.x, nePoint.x), 
            MIN(swPoint.y, nePoint.y), 
            width, 
            height
        );
    
        // if (!MKMapRectSpans180thMeridian(newMapRect)) {
            return newMapRect;
        // } else {
            // ????
        // }
    }
    

提交回复
热议问题