iPhone地图区域怪异

时间:2010-09-08 01:29:43

标签: ios objective-c mapkit

alt text

当我尝试调整MKMapView以适合一个或多个地标时,有时会出现附图。它是间歇性的,但显示器始终处于完全相同的位置。

以下是代码:

 // loc1 is always non-null, and is equal to one of the annotation locations

CLLocationCoordinate2D topLeftCoord = loc1.coordinate;
CLLocationCoordinate2D bottomRightCoord = loc1.coordinate;
for(UserPlacemark* annotation in self.mapView.annotations)
{
    topLeftCoord.longitude = fmin(topLeftCoord.longitude, annotation.coordinate.longitude);
    topLeftCoord.latitude = fmax(topLeftCoord.latitude, annotation.coordinate.latitude);     
    bottomRightCoord.longitude = fmax(bottomRightCoord.longitude, annotation.coordinate.longitude);
    bottomRightCoord.latitude = fmin(bottomRightCoord.latitude, annotation.coordinate.latitude);
}

MKCoordinateRegion region;
double k = 0.01;
region.center.latitude = topLeftCoord.latitude - (topLeftCoord.latitude - bottomRightCoord.latitude) * 0.25;
region.center.longitude = topLeftCoord.longitude + (bottomRightCoord.longitude - topLeftCoord.longitude) * 0.5;
region.span.latitudeDelta = k + fabs(topLeftCoord.latitude - bottomRightCoord.latitude) * 1.25; // Add a little extra space on the sides
region.span.longitudeDelta = k + fabs(bottomRightCoord.longitude - topLeftCoord.longitude) * 1.5; // Add a little extra space on the sides

 // only zoom if region doesn't fit, or too small
 CGRect newRect = [mapView convertRegion:region toRectToView:mapView];
 double MIN_SIZE_PIXELS = 50.0;
 double rectSizePixels = newRect.size.width+newRect.size.height;
 if (!CGRectContainsRect(mapView.bounds, newRect) || rectSizePixels < MIN_SIZE_PIXELS)
 {
      region = [mapView regionThatFits:region];
      [mapView setRegion:region animated:TRUE];
 }

1 个答案:

答案 0 :(得分:0)

上述代码中的问题是Map框架将MKUserAnnotation实例放置在地标列表中。这并不总是与用户的实际位置相对应,至少在模拟器中是这样。

最好避免迭代注释列表,而是直接使用对象计算地标的最小/最大边界。