将mapview缩放到放置引脚的区域 [英] Zoom mapview to a region where pins are dropped

查看:103
本文介绍了将mapview缩放到放置引脚的区域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我在mapview中有3个引脚,当地图加载时,我将如何缩放到这些引脚。也就是说,当地图加载时我需要有一个缩放视图,但视图应该适应地图中掉落的所有引脚。当地图只有一个引脚时,它正在工作。但我不能用多个针脚处理缩放。需要帮助

If I have 3 pins in a mapview, how will I zoom to these pins, when the map loads. That is, when the map loads I need to have a zoomed view, but the view should accomodate all pins dropped in the map. Its working, when the map has just one pin. But I cant handle zoom with multiple pins. Help needed

推荐答案

我在应用程序中编写了以下代码,它类似于@ElanthiraiyanS答案

I wrote the below code in an App and it is similar to @ElanthiraiyanS Answer

-(void)zoomToFitMapAnnotations:(MKMapView*)mapView insideArray:(NSArray*)anAnnotationArray
{   
    // NSLog(@"%s", __FUNCTION__);
    if([mapView.annotations count] == 0) return;

    CLLocationCoordinate2D topLeftCoord;
    topLeftCoord.latitude = -90;
    topLeftCoord.longitude = 180;

CLLocationCoordinate2D bottomRightCoord;
bottomRightCoord.latitude = 90;
bottomRightCoord.longitude = -180;

for(MKPointAnnotation* annotation in anAnnotationArray)
{
    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;
region.center.latitude = topLeftCoord.latitude - (topLeftCoord.latitude - bottomRightCoord.latitude) * 0.5;
region.center.longitude = topLeftCoord.longitude + (bottomRightCoord.longitude - topLeftCoord.longitude) * 0.5;
region.span.latitudeDelta = fabs(topLeftCoord.latitude - bottomRightCoord.latitude) * 1.1; // Add a little extra space on the sides
region.span.longitudeDelta = fabs(bottomRightCoord.longitude - topLeftCoord.longitude) * 1.1; // Add a little extra space on the sides

region = [mapView regionThatFits:region];
[mapView setRegion:region animated:YES];

}

这篇关于将mapview缩放到放置引脚的区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆