1
votes

I am seeing a rather odd behavior with the MKMapView where the center coordinate is being moved when I move the center frame.

I have a UIScrollView on top of a MKMapView and the map view is revealed as the user scrolls down. I have nested UIScrollViews so one pages horizontally while another pages vertically (both sit above the MKMapView). Whenever the user pages horizontally to the next page (and while the map is not visible) I change the coordinates of the MKMapView to match the new content the user is viewing.

When the user scrolls down (vertically) on any page the MKMapView is revealed below however the center coordinate that I set is not correct. As the MKMapView is revealed I move the MKMapView frame inside the scrollViewDidScroll method to make the view appear as if it is scrolling with the UIScrollView sitting above it. Logging out the coordinate center it is correct when I first set it but the latitude moves when I move the MKMapView frame in the scrollViewDidScroll method

Here is the weirdest thing. The center coordinate on the MKMapView is always correct the first time (I use the same method every time to set the center coordinate). Any time after that the center point is always off by the same amount... and it only off in the latitude.

//Code setting the map view center point

- (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];

}

//Code in the scrollViewDidScroll Method

        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;

    }

Here is an example when I log out the starting latitude (both scrolling... which is correct) and then checking the center coordinate after moving the MKMapView frame in the scrollViewDidScroll method

        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