我正在寻找一些帮助完成基于当前位置注释和我设置的注释在MKMapView上设置区域的一些代码。将MKMapView区域设置为以两个注释为中心
我想计算两者之间的距离并设置两者之间的中心,然后缩小以便两者都可以看到。它似乎在我的模拟器中工作正常,但不幸的是userLocation.coordinate固定到Apple总部。当我在设备上测试时,我看到了奇怪的行为。如果两个注释在相同的纬度上有些水平,通常会缩小并设置合适的区域,但如果垂直距离较大,则不会正确缩小。
我用代码中发现here,和编辑一点点适合我的需要:
是困惑我CLLocationCoordinate2D southWest = mapView.userLocation.coordinate;
CLLocationCoordinate2D northEast = southWest;
southWest.latitude = MIN(southWest.latitude, annotation.coordinate.latitude);
southWest.longitude = MIN(southWest.longitude, annotation.coordinate.longitude);
northEast.latitude = MAX(northEast.latitude, annotation.coordinate.latitude);
northEast.longitude = MAX(northEast.longitude, annotation.coordinate.longitude);
CLLocation *locSouthWest = [[CLLocation alloc] initWithLatitude:southWest.latitude longitude:southWest.longitude];
CLLocation *locNorthEast = [[CLLocation alloc] initWithLatitude:northEast.latitude longitude:northEast.longitude];
// This is a diag distance (if you wanted tighter you could do NE-NW or NE-SE)
CLLocationDistance meters = [locSouthWest distanceFromLocation:locNorthEast];
MKCoordinateRegion region;
region.center.latitude = (southWest.latitude + northEast.latitude)/2.0;
region.center.longitude = (southWest.longitude + northEast.longitude)/2.0;
region.span.latitudeDelta = meters/111319.5;
region.span.longitudeDelta = 0.0;
MKCoordinateRegion savedRegion = [mapView regionThatFits:region];
[mapView setRegion:savedRegion animated:YES];
[locSouthWest release];
[locNorthEast release];
一件事是,他说:northEast = southWest
......
在此先感谢谁谁得到了一些帮助,输入:)
完美!德尔塔是我能看到的问题。非常感谢! – runmad 2010-10-29 14:47:34
不幸的是,这个页面似乎没有回应。有没有人有页面的缓存版本?谢谢! – jowie 2011-08-21 22:17:34
@jowie我能够使用回机器找到页面的缓存版本。我希望这有帮助。 http://web.archive.org/web/20100612181520/http://codisllc。com/blog/zoom-mkmapview-to-fit-annotations – avelis 2013-04-04 21:00:52