2012-09-27 24 views
0

我想製作一張地圖,顯示我的位置並在地圖上顯示其他位置的別針。MapKit的幫助。無法讓我的位置先顯示

我已經讓地圖顯示出來,並且引腳也顯示出來,它也定位了我,但它並沒有首先放大我的位置。我怎樣才能讓它首先顯示我的位置?當我打開mapview時,它只顯示了我放入代碼的最後一個引腳。 這是我的代碼。

mapview.h

#import <UIKit/UIKit.h> 
#import <MapKit/MapKit.h> 




@interface mapview : UIViewController { 

    MKMapView *mapview1; 


} 


@property (nonatomic, retain) IBOutletMKMapView *mapview1; 

-(IBAction)setMap:(id)sender; 

-(IBAction)getlocation; 

@end 

mapview.m

#import "mapview.h" 
#import "NewClass.h" 

@implementation mapview 

@synthesize mapview1; 

-(IBAction)getlocation { 

    mapview1.showsUserLocation = YES; 

} 



-(IBAction)setMap:(id)sender { 
    switch (((UISegmentedControl *) sender).selectedSegmentIndex) { 
     case 0: 
      mapview1.mapType = MKMapTypeStandard; 
      break; 
     case 1: 
      mapview1.mapType = MKMapTypeSatellite; 
      break; 
     case 2: 
      mapview1.mapType = MKMapTypeHybrid; 
      break; 
     default: 
      break; 
    } 

} 

-(void)viewDidLoad { 


    [super viewDidLoad]; 

     [mapview1 setMapType:MKMapTypeStandard]; 
     [mapview1 setZoomEnabled:YES]; 
     [mapview1 setScrollEnabled:YES]; 

     MKCoordinateRegion region = { {0.0, 0.0 }, {0.0, 0.0 } }; 
     region.center.latitude = 39.956907; 
     region.center.longitude = -75.610229; 
     region.span.longitudeDelta = 0.01f; 
     region.span.latitudeDelta = 0.01f; 
     [mapview1 setRegion:region animated:YES]; 


     NewClass *ann = [[NewClass alloc] init]; 
     ann.title = @"Vigil Location"; 
     ann.subtitle = @"Planned Parenthood of Chester County"; 
     ann.coordinate = region.center; 
     [mapview1 addAnnotation:ann]; 


    MKCoordinateRegion region1 = { {0.0, 0.0 }, {0.0, 0.0 } }; 
    region1.center.latitude = 40.042819; 
    region1.center.longitude = -75.373776; 
    region1.span.longitudeDelta = 0.01f; 
    region1.span.latitudeDelta = 0.01f; 
    [mapview1 setRegion:region1 animated:YES]; 

    NewClass *ann1 = [[NewClass alloc] init]; 
    ann1.title = @"Vigil Location"; 
    ann1.subtitle = @"Planned Parenthood of Bryn Mawr"; 
    ann1.coordinate = region1.center; 
    [mapview1 addAnnotation:ann1]; 

    MKCoordinateRegion region2 = { {0.0, 0.0 }, {0.0, 0.0 } }; 
    region2.center.latitude = 40.18295; 
    region2.center.longitude = -75.450163; 
    region2.span.longitudeDelta = 0.01f; 
    region2.span.latitudeDelta = 0.01f; 
    [mapview1 setRegion:region2 animated:YES]; 

    NewClass *ann2 = [[NewClass alloc] init]; 
    ann2.title = @"Vigil Location"; 
    ann2.subtitle = @"Planned Parenthood Collegeville"; 
    ann2.coordinate = region2.center; 
    [mapview1 addAnnotation:ann2]; 

    MKCoordinateRegion region3 = { {0.0, 0.0 }, {0.0, 0.0 } }; 
    region3.center.latitude = 40.120772; 
    region3.center.longitude = -75.118181; 
    region3.span.longitudeDelta = 0.01f; 
    region3.span.latitudeDelta = 0.01f; 
    [mapview1 setRegion:region3 animated:YES]; 

    NewClass *ann3 = [[NewClass alloc] init]; 
    ann3.title = @"Vigil Location"; 
    ann3.subtitle = @"Abington Memorial Hospital"; 
    ann3.coordinate = region3.center; 
    [mapview1 addAnnotation:ann3]; 

    MKCoordinateRegion region4 = { {0.0, 0.0 }, {0.0, 0.0 } }; 
    region4.center.latitude = 39.95361; 
    region4.center.longitude = -75.15267; 
    region4.span.longitudeDelta = 0.01f; 
    region4.span.latitudeDelta = 0.01f; 
    [mapview1 setRegion:region4 animated:YES]; 

    NewClass *ann4 = [[NewClass alloc] init]; 
    ann4.title = @"Vigil Location"; 
    ann4.subtitle = @"Philadelphia Women's Center"; 
    ann4.coordinate = region4.center; 
    [mapview1 addAnnotation:ann4]; 

    MKCoordinateRegion region5 = { {0.0, 0.0 }, {0.0, 0.0 } }; 
    region5.center.latitude = 39.910566; 
    region5.center.longitude = -75.014111; 
    region5.span.longitudeDelta = 0.01f; 
    region5.span.latitudeDelta = 0.01f; 
    [mapview1 setRegion:region5 animated:YES]; 

    NewClass *ann5 = [[NewClass alloc] init]; 
    ann5.title = @"Vigil Location"; 
    ann5.subtitle = @"Cherry Hill Women's Center"; 
    ann5.coordinate = region5.center; 
    [mapview1 addAnnotation:ann5]; 

    MKCoordinateRegion region6 = { {0.0, 0.0 }, {0.0, 0.0 } }; 
    region6.center.latitude = 39.74285; 
    region6.center.longitude = -75.550838; 
    region6.span.longitudeDelta = 0.01f; 
    region6.span.latitudeDelta = 0.01f; 
    [mapview1 setRegion:region6 animated:YES]; 

    NewClass *ann6 = [[NewClass alloc] init]; 
    ann6.title = @"Vigil Location"; 
    ann6.subtitle = @"Planned Parenthood of Wilmington"; 
    ann6.coordinate = region6.center; 
    [mapview1 addAnnotation:ann6]; 

    MKCoordinateRegion region7 = { {0.0, 0.0 }, {0.0, 0.0 } }; 
    region7.center.latitude = 40.03754; 
    region7.center.longitude = -76.300828; 
    region7.span.longitudeDelta = 0.01f; 
    region7.span.latitudeDelta = 0.01f; 
    [mapview1 setRegion:region7 animated:YES]; 

    NewClass *ann7 = [[NewClass alloc] init]; 
    ann7.title = @"Vigil Location"; 
    ann7.subtitle = @"Planned Parenthood of Lancaster"; 
    ann7.coordinate = region7.center; 
    [mapview1 addAnnotation:ann7]; 

    MKCoordinateRegion region8 = { {0.0, 0.0 }, {0.0, 0.0 } }; 
    region8.center.latitude = 40.333819; 
    region8.center.longitude = -75.93019; 
    region8.span.longitudeDelta = 0.01f; 
    region8.span.latitudeDelta = 0.01f; 
    [mapview1 setRegion:region8 animated:YES]; 

    NewClass *ann8 = [[NewClass alloc] init]; 
    ann8.title = @"Vigil Location"; 
    ann8.subtitle = @"Planned Parenthood of Reading"; 
    ann8.coordinate = region8.center; 
    [mapview1 addAnnotation:ann8]; 

    MKCoordinateRegion region9 = { {0.0, 0.0 }, {0.0, 0.0 } }; 
    region9.center.latitude = 40.67246; 
    region9.center.longitude = -75.375397; 
    region9.span.longitudeDelta = 0.01f; 
    region9.span.latitudeDelta = 0.01f; 
    [mapview1 setRegion:region9 animated:YES]; 

    NewClass *ann9 = [[NewClass alloc] init]; 
    ann9.title = @"Vigil Location"; 
    ann9.subtitle = @"Planned Parenthood of Allentown"; 
    ann9.coordinate = region9.center; 
    [mapview1 addAnnotation:ann9]; 

    MKCoordinateRegion region10 = { {0.0, 0.0 }, {0.0, 0.0 } }; 
    region10.center.latitude = 39.952384; 
    region10.center.longitude = -76.7253; 
    region10.span.longitudeDelta = 0.01f; 
    region10.span.latitudeDelta = 0.01f; 
    [mapview1 setRegion:region10 animated:YES]; 

    NewClass *ann10 = [[NewClass alloc] init]; 
    ann10.title = @"Vigil Location"; 
    ann10.subtitle = @"Planned Parenthood of York"; 
    ann10.coordinate = region10.center; 
    [mapview1 addAnnotation:ann10]; 
} 


- (void)didReceiveMemoryWarning { 
    // Releases the view if it doesn't have a superview. 
    [super didReceiveMemoryWarning]; 

    // Release any cached data, images, etc that aren't in use. 
} 

- (void)viewDidUnload { 
    // Release any retained subviews of the main view. 
    // e.g. self.myOutlet = nil; 
} 


- (void)dealloc { 
    [super dealloc]; 
} 


@end 
+1

請儘量不要粘貼代碼牆。它會關閉許多不想閱讀你整個代碼塊的潛在答覆者。請儘量將問題縮小到代碼爲 – psubsee2003

回答

2

你已經設置showsUserLocation但如果你看過蘋果的文檔,你會看到,該功能只開啓了藍點,它不縮放或平移視圖。 「http://developer.apple.com/library/ios/documentation/MapKit/Reference/MKMapView_Class/MKMapView/MKMapView.html#//apple_ref/occ/instp/MKMapView/showsUserLocation

你想要做的是用戶跟蹤模式設置爲userfollows

其他代碼提示:

  • 放入數組和循環在他們那些地方,不要創建它們一次一個像
  • 別。對於你製作的每個區域,在地圖上都設置了setRegion。你試圖讓地圖放大用戶,每個setRegion改變ma p正在尋找。無論如何,只有最後一個會有影響。
  • 你並不需要創建一個區域剛剛拿到座標註釋
+0

的較小代碼段。編碼如何查找跟蹤用戶的位置? 對不起,我真的不識字xcode –

+0

這幾乎是我上面說'mapview1.userTrackingMode = MKUserTrackingModeFollow' ref:http://developer.apple.com/library/ios/documentation/MapKit/Reference/MKMapView_Class/MKMapView/MKMapView。 HTML#// apple_ref/OCC/instp /的MKMapView/userTrackingMode – Craig