开始的状态
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.
感谢您的任何帮助,现在一周这个问题挣扎。
你解决了吗?我已经解决了它,但现在它不再工作了。 – AlexWien 2013-07-27 22:11:27