我的mapview有问题。当我进入视图时,它可以工作,但当我转到另一个视图并返回到mapview时,它不再起作用了。我在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];
}
答案 0 :(得分:0)
根据您显示的内容,您不断添加地图视图(每次出现VC时都会添加一个新视图) 你似乎忘记了删除它!?
答案 1 :(得分:0)
为什么要在viewdidAppear中分配mapview?如果未在viewdidDisAppear
上删除地图视图,它会在屏幕上多次添加地图视图答案 2 :(得分:0)
在您的情况下,您没有删除已添加的地图视图,您可以使用的方法 - (void)viewWillAppear删除地图的方法
- (void)viewWillAppear:(BOOL)animated
{
if(map != nil)
{
[map removeFromSuperview];
}
}
它会在添加新地图之前删除上一个地图视图
答案 3 :(得分:0)
将此代码添加到您的视图中:
-(void)viewDidDisappear:(BOOL)animated {
if(mapView)
{
[mapView removeFromSuperview];
}
}
}
同时删除所有子视图:
-(void)viewWillAppear:(BOOL)animated {
for (UIView *view in self.subviews) {
[view removeFromSuperview];
}
}
答案 4 :(得分: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 :如果我们去外面视图我们正在删除地图