【问题标题】:Zooming the mapview缩放地图视图
【发布时间】: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


【解决方案1】:

正如我之前所说,一个注释存在问题。如果您从谷歌搜索中检索地点,那么每个地点都会给出大致的缩放级别(取决于它是否是国家/城市)。您可以在浏览器中输入http://maps.google.com/maps/geo?q=asia&output=json 进行尝试。您可以通过将 asia 替换为 china 或 newyork 等来尝试不同的条目。查看浏览器中响应的 Accuracy 属性。大陆为 0,国家为 1 等。

如果您自己创建了注释,那么您可以附加一个与缩放级别相关的参数。

@interface AddressAnnotation : NSObject<MKAnnotation> {
double zoomLevel;
}
@property(readwrite,nonatomic) double zoomLevel ;
@end




 @implementation AddressAnnotation
    @synthesize zoomLevel;
    -(void)setZoomLevel (double) parameter
    {
    self.zoomLevel = parameter;
    }
@end

最后假设 annot 是你的注释然后

  [annot setZoomLevel: .1]; //instead of .1 you can set different values

当您显示此注释时,将区域中心设置为注释坐标并将跨度设置为 annotation.zoomLevel。

MKCoordinateRegion region;
region.center.latitude = annotation.coordinate.latitude;
region.center.longitude = annotation.coordinate.longitude;
region.span.latitudeDelta = annotation.zoomLevel;
region.span.longitudeDelta = annotation.zoomLevel;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-04-30
    • 2012-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-23
    • 1970-01-01
    相关资源
    最近更新 更多