I am currently working with the Google Maps API for iOS. I have drawn markers on the maps, but I do not know how to "refresh" (delete markers and redraw new ones) the markers once the user has entered in new data from another class. I try to recall the map view class like this:
This is the code, in another class (GetInfoViewController) that is executed when the user inputs 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 container content to 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];
To clear markers on Google Maps for ios use the clear function of your GMSMapView instance. Although I suggest you recycle existing markers by changing its properties like its position.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With