2013-05-06 74 views
1

开始的状态

iOS6,Apple Maps。我在MKMapView上有两个注释。两个注释的坐标具有相同的值为纬度组件,但不同经度MKMapView setRegion not exact

我想

我现在要放大地图,使一个注解是完全对的MapView的帧的左边缘,并在框架的右边缘的其他注释。为此我尝试了MKMapView的setRegion和setVisibleMapRect方法。

的问题

的问题是,这些方法似乎捕捉到一定的缩放级别,因此没有设置该地区正是我需要它。我看到很多关于堆栈溢出的问题,指出在iOS5及更低版本中这种行为是正常的。但是,由于Apple Maps使用矢量图形,视图不受限于某些缩放级别以适当的分辨率显示图像。

测试在...

我在iPhone 4,4S,5,iPad的3和ipad赠送(所有iOS6.1),并在上iOS6.1模拟器进行测试。

我的问题

那么,为什么setRegion和setVisibleMapRect捕捉到一定的缩放级别和精确的区域不会调整到我通过它的价值?

示例代码

鉴于确实出现我定义4个不同的位置作为实例变量,并设置了地图视图:

// define coords 
_coord1 = CLLocationCoordinate2DMake(46.0, 13.0); 
_coord2 = CLLocationCoordinate2DMake(46.0, 13.1); 
_coord3 = CLLocationCoordinate2DMake(46.0, 13.2); 
_coord4 = CLLocationCoordinate2DMake(46.0, 13.3); 

// define frame for map in landscape mode 
CGRect mainScreen = [[UIScreen mainScreen] bounds]; 
CGRect newSRect = mainScreen; 
newSRect.size.width = mainScreen.size.height; 
newSRect.size.height = mainScreen.size.width; 

// setup map view 
customMapView = [[MKMapView alloc] initWithFrame:newSRect]; 
[customMapView setDelegate:self]; 
[customMapView setShowsUserLocation:NO]; 
[self.view addSubview:customMapView]; 

然后,添加3个按键。那些触发所有相同的方法addAnnotationsWithCoord1:coord2。第一个按钮通过_coord1和_coord2,第二个按钮通过_coord1和_coord3,第三个按钮通过_coord1和_coord4。该方法看起来像这样(TapAnnotation是我MKAnnotation的子类):

-(void)addAnnotationsWithCoord1:(CLLocationCoordinate2D)coord1 coord2:(CLLocationCoordinate2D)coord2{ 
    // Make 2 new annotations with the passed coordinates 
    TapAnnotation *annot1 = [[TapAnnotation alloc] initWithNumber:0 coordinate:coord1]; 
    TapAnnotation *annot2 = [[TapAnnotation alloc] initWithNumber:0 coordinate:coord2]; 

    // Remove all existing annotations 
    for(id<MKAnnotation> annotation in customMapView.annotations){ 
     [customMapView removeAnnotation:annotation]; 
    } 

    // Add annotations to map 
    [customMapView addAnnotation:annot1]; 
    [customMapView addAnnotation:annot2]; 
} 

之后我确定西南,东北点,这将决定其包含我2个注解RECT。

// get northEast and southWest 
CLLocationCoordinate2D northEast; 
CLLocationCoordinate2D southWest; 
northEast.latitude = MAX(coord1.latitude, coord2.latitude); 
northEast.longitude = MAX(coord1.longitude, coord2.longitude); 
southWest.latitude = MIN(coord1.latitude, coord2.latitude); 
southWest.longitude = MIN(coord1.longitude, coord2.longitude); 

然后,我计算了两个坐标之间的中点,并设置地图给它的中心坐标(记住,因为所有的坐标具有相同的纬度下面的计算应该是正确的):

// determine center coordinate and set to map 
double centerLon = ((coord1.longitude + coord2.longitude)/2.0f); 
CLLocationCoordinate2D center = CLLocationCoordinate2DMake(southWest.latitude, centerLon); 
[customMapView setCenterCoordinate:center animated:NO]; 

现在我尝试设置地图的区域,使其适合像我想:

CLLocation *loc1 = [[CLLocation alloc] initWithLatitude:southWest.latitude longitude:southWest.longitude]; 
CLLocation *loc2 = [[CLLocation alloc] initWithLatitude:northEast.latitude longitude:northEast.longitude]; 
CLLocationDistance meterSpanLong = [loc1 distanceFromLocation:loc2]; 
CLLocationDistance meterSpanLat = 0.1; 
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(center, meterSpanLat, meterSpanLong); 
[customMapView setRegion:region animated:NO]; 

这并不像预期的那样,所以我试试这个:

MKCoordinateSpan span = MKCoordinateSpanMake(0.01, northEast.longitude-southWest.longitude); 
MKCoordinateRegion region = MKCoordinateRegionMake(center, span); 
[customMapView setRegion:region animated:NO]; 

这仍然没有表现不如预期,所以我尽量将其与setVisibleMapRect:

MKMapPoint westPoint = MKMapPointForCoordinate(southWest); 
MKMapPoint eastPoint = MKMapPointForCoordinate(northEast); 
MKMapRect mapRect = MKMapRectMake(westPoint.x, westPoint.y,eastPoint.x-westPoint.x,1); 
[customMapView setVisibleMapRect:mapRect animated:NO]; 

而且还在,它不表现得像我想要的。作为验证的,我计算从左边的注释点的距离的MapView的帧的左边缘:

// log the distance from the soutwest point to the left edge of the mapFrame 
CGPoint tappedWestPoint = [customMapView convertCoordinate:southWest toPointToView:customMapView]; 
NSLog(@"distance: %f", tappedWestPoint.x); 
  • 对于_coord1和_coord2它表明:138
  • 对于_coord1和_coord3它表明:138
  • 对于_coord1和_coord4它显示:65

那么,为什么我得到这些价值观? 如果任何事情按预期工作,这些应该都是0.

感谢您的任何帮助,现在一周这个问题挣扎。

+0

你解决了吗?我已经解决了它,但现在它不再工作了。 – AlexWien 2013-07-27 22:11:27

回答

-1

阅读setRegion文档:

当设置该属性,所以它适合在地图的可视区域精确的地图可能会调整新的区域价值。这是正常的,可以确保此属性中的值始终反映地图的可见部分。但是,这意味着如果您在设置该属性后立即获取该属性的值,则返回的值可能与您设置的值不匹配。 (您可以使用regionThatFits:方法来确定地图实际设置的区域。)

如果您想精确缩放,则必须自己计算数学。

+0

谢谢@incanus。我在文档中阅读过这个内容,对不起,在我的描述中没有提到这一点。问题是,在完成数学后,如何应用计算的区域而不使mapView捕捉到缩放级别?再次感谢。 – 2013-05-07 13:53:51

+1

@icanus文档是如此错误。如上所述,setRegion无法在iphone4上工作,它仍会捕捉到提供的区域适合的下一个缩放级别。但它不是提供的确切区域。文件声称只有长宽比被纠正了,这是不正确的。捕捉到下一个缩放级别仍然适用(虽然现在有了矢量地图时有点不那么流行) – AlexWien 2013-08-12 00:55:17