ios - MKMapView zoom to fit annotations while locking center -
i'm trying zoom fit annotations on map while locking center , providing insets.
- (void)fitannotations:(nsarray *)annotations edgeinsets:(uiedgeinsets)insets { cllocationcoordinate2d originalcenter = self.centercoordinate; mkmaprect maprect = mkmaprectnull; (id<mkannotation> annotation in annotations) { mkmappoint p = mkmappointforcoordinate([annotation coordinate]); maprect = mkmaprectunion(maprect, mkmaprectmake(p.x, p.y, 0, 0)); } maprect = [self maprectthatfits:maprect edgepadding:insets]; mkcoordinateregion mapregion = mkcoordinateregionformaprect(maprect); // try go original center, while increasing span neccessary amount mkcoordinatespan centeringdelta = mkcoordinatespanmake(fabs(mapregion.center.latitude - originalcenter.latitude), fabs(mapregion.center.longitude - originalcenter.longitude)); mapregion.center = originalcenter; mapregion.span.latitudedelta += centeringdelta.latitudedelta * 2.0; mapregion.span.longitudedelta += centeringdelta.longitudedelta * 2.0; mapregion = [self regionthatfits:mapregion]; [self setregion:mapregion animated:yes]; }
the first part of code here works expected: zooms fit while respecting insets. however, shifts center.
i try re-adjust center after that, fails. i'm not sure if math on re-centering correct.
try this, use calculated maprect create new region originalcenter via mkcoordinateregionmake
method
mkcoordinateregion mapregion = mkcoordinateregionformaprect(maprect); mapregion = mkcoordinateregionmake(originalcenter, mapregion.span); mapview.region = mapregion;
Comments
Post a Comment