【发布时间】:2012-01-25 11:37:10
【问题描述】:
我正在做一个基于 mapview 的应用程序。我正在获取位置(地点)并将其转换为坐标。使用它,我正在放大指定地点的确切位置。
但我面临的问题是, 如果我只指定街道名称或城市名称或州名称或国家名称,则缩放级别始终相同。 (缩放级别始终与街道的缩放级别相同。) 如果我只是指定国家名称,则缩放级别应该是国家级别,而不是直接放大到该国的任何随机街道。
这是我在缩放中使用的代码。
-(void)zoomToFitMapAnnotations:(MKMapView*)mapViews
{
if([mapViews.annotations count] == 0)
return;
CLLocationCoordinate2D topLeftCoordinate;
topLeftCoordinate.latitude = -90;
topLeftCoordinate.longitude = 180;
CLLocationCoordinate2D bottomRightCoord;
bottomRightCoord.latitude = 90;
bottomRightCoord.longitude = -180;
for(SJAddressAnnotation* annotation in mapViews.annotations)
{
topLeftCoordinate.longitude = fmin(topLeftCoordinate.longitude, annotation.coordinate.longitude);
topLeftCoordinate.latitude = fmax(topLeftCoordinate.latitude, annotation.coordinate.latitude);
bottomRightCoord.longitude = fmax(bottomRightCoord.longitude, annotation.coordinate.longitude);
bottomRightCoord.latitude = fmin(bottomRightCoord.latitude, annotation.coordinate.latitude);
}
MKCoordinateRegion region;
region.center.latitude = topLeftCoordinate.latitude - (topLeftCoordinate.latitude - bottomRightCoord.latitude) * 0.5;
region.center.longitude = topLeftCoordinate.longitude + (bottomRightCoord.longitude - topLeftCoordinate.longitude) * 0.5;
region.span.latitudeDelta = fabs(topLeftCoordinate.latitude - bottomRightCoord.latitude) * 1.1; // Add a little extra space on the sides
region.span.longitudeDelta = fabs(bottomRightCoord.longitude - topLeftCoordinate.longitude) * 1.1; // Add a little extra space on the sides
region = [mapView regionThatFits:region];
[mapView setRegion:region animated:YES];
}
我已尝试更改跨度级别,但未成功。在这种情况下需要帮助。
【问题讨论】:
-
您的最终区域仅由 annotation.coordinate 确定。其中(坐标)不包含有关注释是城市、州还是国家/地区的任何信息。
-
如何根据城市/国家设置坐标?
-
首先注释坐标只是单个位置的坐标。它不能包含有关您尝试通过注释表示的事物大小的信息。如果要存储大小信息,则必须在注释中使用单独的参数。其次,国家、城市或街道没有标准尺寸。例如考虑俄罗斯和摩纳哥等国家。你从哪里得到注释?通过网络服务器还是什么?还是它们是你定义的?
-
那么我现在在做什么,是否被认为是显示地图的标准方式
-
当您有超过 1 个注释要显示时,您可以找到包含它们的 MKMaprect 并将其设置为区域。但是如果您只有一个注释要显示,那么您将不得不手动将区域跨度设置为某个常数值。
标签: iphone ios mkmapview zooming