5

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

这是在用户输入新数据时执行的另一个类 (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];
4

2 回答 2

1

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

于 2013-07-30T03:51:00.650 回答
1

I found the problem: I was allocating and initializing a whole new MapViewController each time the button was pressed

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

So i created a global variable, only allocated and initialized once in viewDidLoad, to use in my code

于 2013-07-31T07:47:30.467 回答