2011-03-22 142 views
3

如何設置我的Google MapKit在SDK中的縮放級別?比如說,我住在紐約,並且在我的ProjectName-info.plist中設置了4個座標位置(URL類型> Item 0> URL Schemes> Item 0),然後我在我的代碼中設置了我的代碼UIViewController子類文件:在MapKit中設置縮放級別Xcode

#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區和運行它,只給了我一個區域時,變焦水平似乎非常接近的地圖:

enter image description here

因此,當我修改代碼時(假設有人幫我修復了縮放級別的代碼),下一次我運行它時,當我點擊「在Google地圖上查找我們」時,我的Google地圖打開頁面縮放級別應該看起來像這樣: enter image description here

因此,希望有人能幫我修復這個縮放級別,當我開始打開谷歌地圖,謝謝!

(關於紐約的變量名,算了吧,我不住在紐約笑)

回答

3

設置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公里矩形創建了一公里的區域,以及以下行放大矩形如果點傳播得更遠。

+0

嘿感謝您的幫助,當我跑它,它給了我一個「_CLLocationCoordinate2DMake」,從引用: - [GoogleMap的viewDidLoad中在GoogleMap.o 符號(S)沒有發現 collect2:LD返回1個退出狀態 也許你可以幫我解決這個錯誤?謝謝 – PatrickGamboa 2011-03-23 14:33:18

+1

您需要該功能的核心位置框架。如果你不想僅僅爲此包含框架,可以用'(CLLocationCoordinate2D){(maxLat + minLat)/ 2.0替換CLLocationCoordinate2DMake((maxLat + minLat)/ 2.0,(maxLon + minLon)/ 2.0) (maxLon + minLon)/ 2.0}'。 – Anomie 2011-03-23 14:52:21

+0

哇,非常感謝你!真的行!現在我可以添加更多東西了!順便說一下,我還有一個問題,當用戶在特定區域的其他地方,並且當他們點擊我的谷歌按鈕打開我的谷歌頁面時,我想要在用戶的當前位置和我的商業位置之間繪製一條路線,是嗎? posible,如果你能幫助我呢?再次感謝! – PatrickGamboa 2011-03-23 14:59:13