在 MapKit Xcode 中设置缩放级别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5398096/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 20:37:08  来源:igfitidea点击:

Setting the zoom level in MapKit Xcode

iphoneobjective-cxcodemapkit

提问by PatrickGamboa

How can I set the zoom level of my Google MapKit in SDK? Like for example, I live in New York, and I've set up 4 coordinate positions in my ProjectName-info.plist (URL Types > Item 0 > URL Schemes > Item 0), then I've set up my code in my UIViewController subclass file:

如何在 SDK 中设置我的 Google MapKit 的缩放级别?例如,我住在纽约,我在 ProjectName-info.plist 中设置了 4 个坐标位置(URL 类型 > Item 0 > URL Schemes > Item 0),然后我在我的代码中设置了代码UIViewController 子类文件:

#import "GoogleMap.h"
#import "MyAnnotation.h"

@implementation GoogleMap

        - (void)viewDidLoad {
        [super viewDidLoad];

        variable1 = [[NSArray alloc] initWithContentsOfFile:[[NSBundle mainBundle] 
                                                             pathForResource:@"NewYorkAreas" 
                                                             ofType:@"plist"]];

        double minLat = [[variable1 valueForKeyPath:@"@min.latitude"] doubleValue];
        double maxLat = [[variable1 valueForKeyPath:@"@max.latitude"] doubleValue];
        double minLon = [[variable1 valueForKeyPath:@"@min.longitude"] doubleValue];
        double maxLon = [[variable1 valueForKeyPath:@"@max.longitude"] doubleValue];

        MKCoordinateRegion region;
        region.center.latitude = (maxLat + minLat) / 2.0;
        region.center.longitude = (maxLon + minLon) / 2.0;
        region.span.latitudeDelta = (maxLat - minLat) * 1.05;
        region.span.longitudeDelta = (maxLon - minLon) * 1.05;
        map.region = region;

        for (NSDictionary *newYorkAreasDict in variable1){
            MyAnnotation *annotation = [[MyAnnotation alloc] initWithDictionary:newYorkAreasDict];
            [map addAnnotation:annotation];
            [annotation release];
        }
    }

    - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation{

        if (map.userLocation == annotation){
            return nil;
        }

        NSString *identifier = @"MY_IDENTIFIER";

        MKAnnotationView *annotationView = [map dequeueReusableAnnotationViewWithIdentifier:identifier];
        if (annotationView == nil){
            annotationView = [[[MKAnnotationView alloc] initWithAnnotation:annotation 
                                                           reuseIdentifier:identifier] 
                              autorelease];
            annotationView.image = [UIImage imageNamed:@"logo.png"];
            annotationView.canShowCallout = YES;

            annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
        }
        return annotationView;
    }

So this is all I have for now in my code, which works perfectly when I run and debug the app, and shows me the location of all 4 areas that I set in a good zooming level, but now I want only 1 area to set, so when I erased the other 3 area and when running it and only giving me one area, the zooming level seems very close to the map:

所以这就是我现在在我的代码中所拥有的全部内容,当我运行和调试应用程序时它可以完美运行,并向我显示我在良好缩放级别中设置的所有 4 个区域的位置,但现在我只想设置 1 个区域,所以当我擦除其他 3 个区域并运行它并且只给我一个区域时,缩放级别似乎非常接近地图:

enter image description here

在此处输入图片说明

So I when the code is fixed (assuming someone helped me fix the code for the zooming level), the next time I run it, when I click on "Find us on Google Map", my Google Map opening page zoom level should look like this: enter image description here

所以当代码修复后(假设有人帮我修复了缩放级别的代码),下次我运行它时,当我点击“在谷歌地图上找到我们”时,我的谷歌地图打开页面缩放级别应该看起来像这个: 在此处输入图片说明

So yea hopefully someone can help me fix this zooming level when I begin opening Google Map, thanks!

所以是的,希望有人能在我开始打开谷歌地图时帮助我修复这个缩放级别,谢谢!

(About the New York variable names, forget it, I don't live in New York lol)

(关于纽约变量名,算了,我不住在纽约哈哈)

采纳答案by Anomie

Set a minimum value for latitudeDelta and longitudeDelta. I would do it something like this:

为 latitudeDelta 和 longitudeDelta 设置最小值。我会这样做:

MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(CLLocationCoordinate2DMake((maxLat + minLat) / 2.0, (maxLon + minLon) / 2.0), 1000.0, 1000.0);
region.span.latitudeDelta = MAX(region.span.latitudeDelta, (maxLat - minLat) * 1.05);
region.span.longitudeDelta = MAX(region.span.longitudeDelta, (maxLon - minLon) * 1.05);

The first line creates a region with a 1km by 1km rectangle, and the following lines enlarge the rectangle if the points are spread farther.

第一行创建了一个 1km x 1km 矩形的区域,如果点分布得更远,下面的行会放大矩形。