2013-07-30 55 views
4

我目前正在使用Google Maps API for iOS。我在地图上绘制了标记,但是一旦用户输入了另一个类的新数据,我不知道如何“刷新”(删除标记并重新绘制标记)标记。我回想一下这样的地图视图类:刷新Google地图iOS中的内容

这是代码,在另一个类(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]; 
} 

在GetInfoViewController:改变容器的内容的MapViewController

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]; 
+0

不要手动调用viewDidLoad。 – satheeshwaran

+0

然后我会调用loadView吗?我已经尝试同时调用loadView和createMarkers,并且在地图上没有任何更新。 – Alexyuiop

+0

当用户更新一些调用mapView的东西时清除,然后再次加载所有标记。这不是最好的方法,但我们可以尝试找到什么不起作用。 – satheeshwaran

回答

2

我发现这个问题:我被分配和每个按钮被按下

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

所以我创建一个全局变量,只分配和viewDidLoad中初始化一次时间初始化一个全新的MapViewController,在用我的代码

1

要清除Google Maps for ios上的标记,请使用GMSMapView实例的clear函数。虽然我建议你通过改变它的属性像位置来回收现有的标记。

+2

我[mapView clear];在loadView方法中,并且没有任何东西被清除/更新 – Alexyuiop

+2

当您调用[mapView清除]时没有任何东西需要清除,因为mapview上没有标记。请记住loadView只会被调用一次。您可以将清晰的标记放在单独的函数上并根据需要调用它。也不要手动调用viewDidload。 – capecrawler

+1

好的,我创建了一个单独的方法来清除地图并放置新的标记,但仍然没有清除地图并且没有显示新的标记。 (上面的代码已更新) – Alexyuiop