2013-11-15 78 views
0

我的mapview有問題。當我去視圖它的作品,但是當我去另一個視圖,回到地圖視圖,它不再工作。當我使用NSLog()時,我得到了正確的經緯度,所以這不是問題。Objective-C Mapview只能使用一次

代碼:

- (void) viewDidAppear:(BOOL)animated { 
if(IS_IPHONE5) { 
    mapView = [[MKMapView alloc] initWithFrame:CGRectMake(0,300,320,300)]; 
} else { 
    mapView = [[MKMapView alloc] initWithFrame:CGRectMake(0,250,320,250)]; 
} 

mapView.mapType = MKMapTypeStandard; 

CLLocationCoordinate2D coord = {.latitude = [testLatitude floatValue], .longitude = [testLongitude floatValue]}; 
MKCoordinateSpan span = {.latitudeDelta = 0.05, .longitudeDelta = 0.05}; 
MKCoordinateRegion region = {coord, span}; 

[mapView setRegion:region]; 
[self.view addSubview:mapView]; 

CLLocationCoordinate2D annotationCoord; 
annotationCoord.latitude = [testLatitude floatValue]; 
annotationCoord.longitude = [testLongitude floatValue]; 

MKPointAnnotation *annotationPoint = [[MKPointAnnotation alloc] init]; 
annotationPoint.coordinate = annotationCoord; 
annotationPoint.title = stringTitle; 
[mapView addAnnotation:annotationPoint]; 

} 
+0

創建並添加在viewDidLoad中的地圖視圖。如果這是您想要的行爲,您可以重置區域並在viewDidAppear中添加註釋。 – Anna

回答

0

基於你已經證明你保持(每個VC時出現一個新的) 加入地圖視圖,你似乎忘了刪除它!?

+0

好的謝謝,但你能告訴我如何我可以刪除一個mapview? –

0

你爲什麼要在viewdidAppear中分配你的mapview?它會增加你映射在屏幕上查看多次,如果不是viewdidDisAppear

+0

感謝您的反饋,但如果viewdidAppear不好,我必須把它放在哪裏? –

0

你的情況,你是不是刪除一個已添加的地圖視圖中刪除,你可以做的是使用 - (空)viewWillAppear中的方法添加新地圖之前,刪除這樣的

- (void)viewWillAppear:(BOOL)animated 
{ 
    if(map != nil) 
    { 
    [map removeFromSuperview]; 
    } 
} 

地圖就會刪除以前的地圖視圖

+0

謝謝,但它並沒有工作,不幸的 –

0

將此代碼添加到您的視圖:

-(void)viewDidDisappear:(BOOL)animated { 
    if(mapView) 
    { 
    [mapView removeFromSuperview]; 
    } 
} 

} 

也刪除所有子視圖:

-(void)viewWillAppear:(BOOL)animated { 
    for (UIView *view in self.subviews) { 
     [view removeFromSuperview]; 
    } 
} 
+0

謝謝,但它不工作不幸 –

+0

嘗試添加這個viewWillAppear –

+0

沒有,也沒有工作也 –

0

嘗試這樣它可以幫助

- (void) viewDidAppear:(BOOL)animated { 

    [super viewDidAppear:(BOOL)animated]; 

    if(IS_IPHONE5) { 

     if (mapView == nil){ 

      mapView = [[MKMapView alloc] initWithFrame:CGRectMake(0,300,320,300)]; 
      [mapview setDelegate:self]; 

     } 

    } else { 

     if (mapView == nil){ 

      mapView = [[MKMapView alloc] initWithFrame:CGRectMake(0,250,320,250)]; 
      [mapview setDelegate:self]; 
     } 
    } 


//Add ramaining Code 

} 

viewDidDisappear代碼

- (void)viewDidDisappear:(BOOL)animated { 

    [super viewDidDisappear:(BOOL)animated]; 
    [mapView removeFromSuperview]; 
    mapView = nil; 

} 

ViewDidAppear:如果MapView的零,我們正在創建的mapview否則不是

viewDidDisappear:如果我們到外面去查看我們刪除的地圖