การตั้งค่าระดับการซูมใน MapKit Xcode

ฉันจะตั้งค่าระดับการซูมของ Google MapKit ใน SDK ได้อย่างไร ตัวอย่างเช่น ฉันอาศัยอยู่ในนิวยอร์ก และฉันได้ตั้งค่าตำแหน่งพิกัด 4 ตำแหน่งใน ProjectName-info.plist ของฉัน (ประเภท URL > รายการ 0 > แบบแผน URL > รายการ 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;
    }

นี่คือทั้งหมดที่ฉันมีตอนนี้ในโค้ด ซึ่งทำงานได้อย่างสมบูรณ์แบบเมื่อฉันเรียกใช้และดีบักแอป และแสดงตำแหน่งของทั้ง 4 พื้นที่ที่ฉันตั้งค่าไว้ในระดับการซูมที่ดี แต่ตอนนี้ฉันต้องการเพียง 1 พื้นที่ในการตั้งค่า ดังนั้นเมื่อฉันลบพื้นที่อีก 3 แห่งและเมื่อเรียกใช้และให้ฉันพื้นที่เดียว ระดับการซูมจึงดูใกล้เคียงกับแผนที่มาก:

ป้อนคำอธิบายรูปภาพที่นี่

ดังนั้น เมื่อโค้ดได้รับการแก้ไขแล้ว (สมมติว่ามีคนช่วยแก้ไขโค้ดในระดับการซูม) ครั้งต่อไปที่เรียกใช้งาน เมื่อฉันคลิก "ค้นหาเราบน Google Map" ระดับการซูมหน้าเปิด Google Map ของฉันควรมีลักษณะดังนี้ นี้: ป้อนคำอธิบายรูปภาพที่นี่

ใช่แล้ว หวังว่าจะมีคนช่วยฉันแก้ไขระดับการซูมนี้เมื่อฉันเริ่มเปิด Google Map ขอบคุณ

(เกี่ยวกับชื่อตัวแปร New York ลืมไปเถอะ ฉันไม่ได้อาศัยอยู่ใน New York 555)


person PatrickGamboa    schedule 22.03.2011    source แหล่งที่มา


คำตอบ (1)


ตั้งค่าขั้นต่ำสำหรับ 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);

บรรทัดแรกจะสร้างพื้นที่ที่มีสี่เหลี่ยมผืนผ้าขนาด 1 กม. x 1 กม. และบรรทัดต่อไปนี้จะขยายสี่เหลี่ยมผืนผ้าหากจุดกระจายออกไป

person Anomie    schedule 23.03.2011
comment
สวัสดี ขอบคุณสำหรับความช่วยเหลือของคุณ เมื่อฉันเรียกใช้ มันทำให้ฉันมี _CLLocationCoordinate2DMake อ้างอิงจาก: -[GoogleMap viewDidLoad] ในสัญลักษณ์ GoogleMap.o ไม่พบ collect2: ld ส่งกลับ 1 สถานะการออก บางทีคุณอาจช่วยฉันด้วยข้อผิดพลาดนี้ ? ขอบคุณ - person PatrickGamboa; 23.03.2011
comment
คุณจะต้องมีกรอบงาน Core Location สำหรับฟังก์ชันนั้น หากคุณไม่ต้องการรวมเฟรมเวิร์กไว้สำหรับสิ่งนั้น ให้ลองแทนที่ CLLocationCoordinate2DMake((maxLat + minLat) / 2.0, (maxLon + minLon) / 2.0) ด้วย (CLLocationCoordinate2D){ (maxLat + minLat) / 2.0, (maxLon + minLon) / 2.0 } - person Anomie; 23.03.2011
comment
ว้าว ขอบคุณมาก! มันได้ผลจริงๆ! ตอนนี้ฉันสามารถเพิ่มอีกนิดได้แล้ว! อีกอย่าง ฉันมีคำถามอีกข้อหนึ่ง เมื่อผู้ใช้อยู่ที่อื่นในพื้นที่ใดพื้นที่หนึ่ง และเมื่อพวกเขาแตะปุ่ม Google เพื่อเปิดหน้า Google ของฉัน ฉันต้องการให้พล็อตเส้นทางระหว่างตำแหน่งปัจจุบันของผู้ใช้กับที่ตั้งธุรกิจของฉัน ใช่ไหม เป็นไปได้ไหม หากคุณสามารถช่วยฉันในเรื่องนั้นได้? ขอบคุณอีกครั้ง! - person PatrickGamboa; 23.03.2011
comment
นั่นจะเป็นเรื่องของคำถามแยกต่างหาก - person Anomie; 23.03.2011
comment
ในตอนแรกมันใช้งานได้โดยใช้ตัวอย่างของคุณ แต่ตอนนี้ภูมิภาคกำลังเปลี่ยนแปลง แต่ไม่ได้ทำการซูมไปที่ภูมิภาคนั้น - person ; 05.07.2011