在谷歌地图iOS中刷新内容

在谷歌地图iOS中刷新内容,ios,objective-c,google-maps,google-maps-markers,Ios,Objective C,Google Maps,Google Maps Markers,我目前正在使用iOS版的谷歌地图API。我已经在地图上画了标记,但我不知道在用户从另一个类输入新数据后如何“刷新”(删除标记并重新绘制新标记)。我尝试像这样回忆map view类: 这是在用户输入新数据时执行的另一个类(GetInfoViewController)中的代码 MapViewController *mapVC = [[MapViewController alloc]init]; [mapVC resetMap]; 这是MapViewController中的内容 - (void)vi

我目前正在使用iOS版的谷歌地图API。我已经在地图上画了标记,但我不知道在用户从另一个类输入新数据后如何“刷新”(删除标记并重新绘制新标记)。我尝试像这样回忆map view类:

这是在用户输入新数据时执行的另一个类(GetInfoViewController)中的代码

MapViewController *mapVC = [[MapViewController alloc]init];
[mapVC resetMap];
这是MapViewController中的内容

- (void)viewDidLoad
{
    [super viewDidLoad];
    mapView.myLocationEnabled = YES;
    mapView.settings.myLocationButton = YES;
    getpos = [[NSMutableArray alloc]init];

}

- (void)loadView {

    lat = [[NSMutableArray alloc]init];
    lng = [[NSMutableArray alloc]init];
    markers = [[NSMutableArray alloc]init];
    locationManager = [[CLLocationManager alloc] init];
    locationManager.distanceFilter = kCLDistanceFilterNone;
    locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters; // 100 m


    [locationManager startUpdatingLocation];

    GMSCameraPosition *camera = [GMSCameraPosition   cameraWithLatitude:locationManager.location.coordinate.latitude
                                                            longitude:locationManager.location.coordinate.longitude                                                                 zoom:13.2];

    mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera];
    mapView.delegate = self;

    self.view = mapView;
    [mapView clear];
    [self createMarker];

}


- (void) createMarker {

    [lat removeAllObjects];
    [lng removeAllObjects];
    [markers removeAllObjects];

    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];

    for (int x = 0; x < [appDelegate.geocodedLatArrayGlobal count]; x++) {
        NSNumber *latCoord = [NSNumber numberWithDouble:[[appDelegate.geocodedLatArrayGlobal objectAtIndex:x]doubleValue]];
        [lat addObject: latCoord];
    }

    for (int x = 0; x < [appDelegate.geocodedLngArrayGlobal count]; x++) {
        NSNumber *lngCoord = [NSNumber numberWithDouble:[[appDelegate.geocodedLngArrayGlobal objectAtIndex:x]doubleValue]];
        [lng addObject:lngCoord];
    }

    for (int x = 0; x < [lat count]; x++) {
        double latitude =[[lat objectAtIndex:x]doubleValue];
        double longitude =[[lng objectAtIndex:x]doubleValue];
        CLLocationCoordinate2D position = CLLocationCoordinate2DMake(latitude,longitude) ;
        GMSMarker *marker = [GMSMarker markerWithPosition:position];
        marker.title = @"Title"
        marker.map = mapView;
        [markers addObject:marker];

    }

}

-(void)resetMap{
       NSLog(@"map reset");
       [mapView clear];
       [self createMarker];
}
 MapViewController *viewController1 = [self.storyboard instantiateViewControllerWithIdentifier:@"vc1"];
    viewController1.view.frame = self.container.bounds;

    [viewController1 willMoveToParentViewController:self];
    [self.container addSubview:viewController1.view];
    [self addChildViewController:viewController1];
    [viewController1 didMoveToParentViewController:self];
    [UIView beginAnimations:nil context:nil];

    [UIView setAnimationDuration:1];

    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight  forView:self.container cache:YES];

    [UIView commitAnimations];

要清除Google Maps for ios上的标记,请使用GMSMapView实例的功能。尽管我建议您通过更改现有标记的属性(如其位置)来回收现有标记。

我发现了问题:每次按下按钮时,我都在分配和初始化一个全新的MapViewController

MapViewController *mapVC = [[MapViewController alloc]init];
[mapVC resetMap];

因此,我创建了一个全局变量,只在viewDidLoad中分配和初始化了一次,以便在代码中使用

不要手动调用viewDidLoad。我会调用loadView吗?我已经尝试调用loadView和CreateMarkets,当用户更新某些内容时,地图上没有任何更新。请调用mapView clear,然后再次加载所有标记。这不是最好的方法,但我们可以尝试找出不起作用的地方;标记器没有发生任何变化。代码MapViewController*mapVC=[[MapViewController alloc]init];[mapVC viewDidLoad];如果您想知道“用户已输入新数据”发生在哪里,则在单独的类中调用??我会[mapView clear];在loadView方法中,没有任何内容被清除/更新调用[mapView clear]时没有任何内容需要清除,因为mapView上还没有标记。请记住,loadView只调用一次。您可以将清除标记放在单独的函数上,并根据需要调用它。也不要手动调用viewDidload。好的,我创建了一个单独的方法来清除地图并放置新的标记,但仍然没有清除地图,也没有显示新的标记。(上面的代码已更新)只是一个问题,如何显示MapViewController?是否像调用[[self.navigationController]pushViewController:mapView-animated:YES]一样将其推入导航堆栈中;在GetInfoViewController中?是否检查了从AppDelegate获取的数据是否更改?当用户点击按钮将容器视图的内容从表视图更改为此mapview时,将显示MapViewController(上面编辑的代码显示了显示MapViewController的代码)