调整帧中心时MKMapView中心坐标更改

时间:2015-04-06 23:16:56

标签: ios uiscrollview annotations mkmapview uiscrollviewdelegate

我看到MKMapView的一个奇怪的行为,当我移动中心框架时,中心坐标被移动。

我在UIScrollView之上有一个MKMapView,当用户向下滚动时会显示地图视图。我嵌套了UIScrollViews,因此水平翻页,而另一页垂直翻页(均位于MKMapView上方)。每当用户水平翻页到下一页(并且地图不可见)时,我会更改MKMapView的坐标以匹配用户正在查看的新内容。

当用户在任何页面上向下(垂直)向下显示MKMapView时,但我设置的中心坐标不正确。在显示MKMapView时,我会移动MKMapView方法中的scrollViewDidScroll框架,以使视图看起来好像滚动时UIScrollView位于其上方。在我第一次设置时,记录坐标中心是正确的,但是当我在MKMapView方法中移动scrollViewDidScroll帧时,纬度会移动

这是最奇怪的事情。第一次MKMapView上的中心坐标始终是正确的(每次使用相同的方法设置中心坐标)。在此之后的任何时候,中心点总是偏离相同的量...并且它仅在纬度中关闭。

//代码设置地图视图中心点

- (void) setMapToNewLocationForIndex:(int)index {

//Create coordinate
PhotoData *mapData = (PhotoData *)[detailDataArray objectAtIndex:index];
CLLocation *location = [[CLLocation alloc]initWithLatitude:mapData.latitude longitude:mapData.longitude];

//Reset Center Frame
mapView.center = CGPointMake(self.view.frame.size.width/2, self.view.frame.size.height);

//Remove all annotations
[mapView removeAnnotations:mapView.annotations];

//Add in current annotaion
MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];
[annotation setCoordinate:location.coordinate];
[mapView addAnnotation:annotation];

//Set Map Region
MKCoordinateRegion region = MKCoordinateRegionMake(location.coordinate, MKCoordinateSpanMake(.01, (.01 *(mapView.frame.size.height / mapView.frame.size.width))));
[mapView setRegion:[mapView regionThatFits:region]];
[mapView setCenterCoordinate:location.coordinate];

}

// scrollViewDidScroll方法中的代码

        if (onScreenScrollView.contentOffset.y > 411 && onScreenScrollView.contentOffset.y < self.view.frame.size.height) {//Page 2 movement

        float yCenter = self.view.frame.size.height - ((onScreenScrollView.contentOffset.y - 411)/2);

        CGPoint newCenter = mapView.center;
        newCenter.y = yCenter;
        mapView.center = newCenter;

    }

    if (onScreenScrollView.contentOffset.y > self.view.frame.size.height) {//Page Three Map Movement

        float start = self.view.frame.size.height - ((self.view.frame.size.height - 411)/2);
        float heightDiff = start - (self.view.frame.size.height/2);
        float ratio = heightDiff/self.view.frame.size.height;
        float yCenter = start - ((onScreenScrollView.contentOffset.y - self.view.frame.size.height) * ratio);

        if (yCenter <  (self.view.frame.size.height/2)) {
            yCenter = (self.view.frame.size.height/2);
        }

        CGPoint newCenter = mapView.center;
        newCenter.y = yCenter;
        mapView.center = newCenter;

    }

这是一个示例,当我注销起始纬度(滚动...都正确),然后在scrollViewDidScroll方法中移动MKMapView帧后检查中心坐标

        NSLog(@"SCROLL LAT %f",[mapView centerCoordinate].latitude);


START LAT 37.720378
SCROLL LAT 37.720170
SCROLL LAT 37.719586
SCROLL LAT 37.719080
SCROLL LAT 37.718652
SCROLL LAT 37.718295
SCROLL LAT 37.718003
SCROLL LAT 37.717763
SCROLL LAT 37.717574
SCROLL LAT 37.717425
SCROLL LAT 37.717315
SCROLL LAT 37.717230
SCROLL LAT 37.717165
SCROLL LAT 37.717120
SCROLL LAT 37.717088
SCROLL LAT 37.717068

0 个答案:

没有答案