0

我的地图视图有问题。当我转到视图时,它可以工作,但是当我转到另一个视图并返回地图视图时,它就不再起作用了。当我使用 NSLog() 时,我得到了正确的纬度和经度,所以这不是问题。

代码:

- (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];

}
4

5 回答 5

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 nil 我们正在创建 mapview 否则不是

viewDidDisappear:如果我们走出视野,我们将移除地图

于 2013-11-16T06:45:18.657 回答
0

将此代码添加到您的视图中:

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

}

还要删除所有子视图:

-(void)viewWillAppear:(BOOL)animated {
    for (UIView *view in self.subviews) {
        [view removeFromSuperview];
     }
}
于 2013-11-15T09:25:46.580 回答
0

为什么要在 viewdidAppear 中分配地图视图?如果未在 viewdidDisAppear 上删除,它将在屏幕上多次添加地图视图

于 2013-11-15T08:40:48.250 回答
0

根据您所展示的内容,您不断添加地图视图(每次出现 VC 时都会添加一个新视图),您似乎忘记将其删除!?

于 2013-11-15T08:33:08.960 回答
0

在您的情况下,您没有删除已添加的地图视图,您可以做的是使用 - (void)viewWillAppear 方法像这样删除地图

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

它将在添加新地图之前删除以前的地图视图

于 2013-11-15T09:11:57.487 回答