How to set certain boundaries, consisting of NE and SW coordinates, in the visible map view?

I need to fit certain borders inside the map. I get the bounds from calling google geocoder and reading the viewport property, which looks like this:

{
    northeast =     {
        lat = "30.4212235";
        lng = "-97.486942";
    };
    southwest =     {
        lat = "30.1128403";
        lng = "-97.99917959999999";
    };
}

Then I convert them to CLLocationCoordinate2D

NSDictionary *viewport = [[[results objectAtIndex:0] objectForKey:@"geometry"] 
                                                     objectForKey:@"viewport"];
NSDictionary *NEDictionary = [viewport objectForKey:@"northeast"];
NSDictionary *SWDictionary = [viewport objectForKey:@"southwest"];

CLLocationCoordinate2D SWCoordinate = 
    CLLocationCoordinate2DMake(
        [[SWDictionary objectForKey:@"lat"] floatValue], 
        [[SWDictionary objectForKey:@"lng"] floatValue]
    );
CLLocationCoordinate2D NECoordinate = 
    CLLocationCoordinate2DMake(
        [[NEDictionary objectForKey:@"lat"] floatValue], 
        [[NEDictionary objectForKey:@"lng"] floatValue]
    );    

I know that I need to create MKMapRect (or MKMapRegion, whichever is easier) from these coordinates, and then [mapView setVisibleRect: newRect animated: YES] (or [mapView setRegion: newRegion animated: YES], but I not quite sure how to get there, I need a method to transform the borders into the correct data structure, for example:

- (MKMapRect) mapRectThatFitsBoundsSW:(CLLocationCoordinate2D)sw 
                                   NE:(CLLocationCoordinate2D)ne {
    // CGFloat x = ??
    // CGFloat y = ??
    // CGFloat width = ??
    // CGFloat height = ??
    MKMapRect mapRectFromBounds = MKMapRectMake(x,y,width,height);
    return mapRectFromBounds;
}

Any thoughts?

+5
source share
3

180- , :

- (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));
}

MKMapRect, anitmeridian, , . , MKMapRect , 180- . !

+2

.

MKCoordinateRegion, center, span - .

MKMapRect MapKit MKMapPointForCoordinate. origin, - MKMapPoint. width height, mappoints ( MKMapPoint ).

- MKMapRectUnion. MKMapRect , , :

MKMapPoint swPoint = MKMapPointForCoordinate(SWCoordinate);
MKMapRect swRect = MKMapRectMake(swPoint.x, swPoint.y, 0, 0);

MKMapPoint nePoint = MKMapPointForCoordinate(NECoordinate);
MKMapRect neRect = MKMapRectMake(nePoint.x, nePoint.y, 0, 0);

MKMapRect rect = MKMapRectUnion(swRect, neRect);

, - . ( , , mapRectThatFits:.)

+8

I found something that works. I ended up 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 {
        // ????
    // }
}
0
source

All Articles