Update content on iOS Google Maps iOS

I am currently working with the Google Maps API for iOS. I drew markers on maps, but I don’t know how to “update” (delete markers and redraw new) markers after the user enters new data from another class. I am trying to call the map display class as follows:

This is code in another class (GetInfoViewController) that runs when a user enters new data

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

This is what is inside the 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]; } 

In GetInfoViewController: changing the contents of a container in a 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
source share
2 answers

I found the problem: I selected and initialized the whole new MapViewController every time the button was clicked

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

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

+2
source

To clear markers on Google Maps for ios, use the clear function of your GMSMapView instance. Although I suggest you recycle existing markers, changing its properties as its position.

+1
source

Source: https://habr.com/ru/post/1494180/


All Articles